Sliding marker on link hover and click with jQuerySliding marker on link hover with jQueryHover effects for...

A ​Note ​on ​N!

How can Zone of Truth be defeated without the caster knowing?

What language was spoken in East Asia before Proto-Turkic?

How would one muzzle a full grown polar bear in the 13th century?

Is this homebrew Wind Wave spell balanced?

In order to check if a field is required or not, is the result of isNillable method sufficient?

Why was Germany not as successful as other Europeans in establishing overseas colonies?

How does a program know if stdout is connected to a terminal or a pipe?

Was there a shared-world project before "Thieves World"?

Can someone publish a story that happened to you?

What is Niska's accent?

Do I have an "anti-research" personality?

French for 'It must be my imagination'?

The Defining Moment

How can I practically buy stocks?

TIKZ - changing one block into parallel multiple blocks

What does "vanity" mean in "the creature was made subject to vanity", Romans 8:20?

web3.py web3.isConnected() returns false always

Is the claim "Employers won't employ people with no 'social media presence'" realistic?

Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?

Examples of subgroups where it's nontrivial to show closure under multiplication?

Can i spend a night at Vancouver then take a flight to my college in Toronto as an international student?

Is the 5 MB static resource size limit a 5,242,880 bytes limit or a 5,000,000 bytes limit?

What does the "ep" capability means?



Sliding marker on link hover and click with jQuery


Sliding marker on link hover with jQueryHover effects for red, blue, and green boxesOptimize iterative pixel blending functionCustom slide show scriptInteractive sliderInterupt redirect to catch potential newsletter signupMobile touch menuWrapping my head around these wrappersOn hover, hide text and display optionsPrepending textbox to Wikipedia thumbnail pictureSliding marker on link hover with jQuery






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1












$begingroup$


The code I'm bringing forward is for a vertical menu, where I needed a marker to do the following:




  • When hovering over a link the marker should slide down next to it.


  • When not hovering over a link the marker should return to the side of the current selected link (the default being the first link).


  • When a link is clicked the marker should remain next to the clicked link, unless another link is being hovered over.



What I've done in jQuery is add a class ('.staydown0','.staydown1' or '.staydown2') to the marker div ('#slide') when one one of the links ('#menulink1','#menulink2' or '#menulink3') is clicked, and remove any other classes that would have it be situated next to one of the other links.



Then I've set jQuery to add a class ('.movedown0','.movedown1' or '.movedown2') to the marker div ('#slide') when the mouse hovers over one of the links ('#menulink1','#menulink2' or '#menulink3'), and remove the class when the mouse stops hovering over the link.



The code works, but it is redundant. How could I go about abstracting it?



I've already asked a similar question recently where I only had the "hover" part of the menu set up, and it was solved.



But I am a beginner in javascript and jQuery, and even though I've managed to understand the solution that was offered to me in the previous question, I am at a loss as to how to extrapolate that knowledge to abstracting this more convoluted code.



CODEPEN



HTML(Pug):



.staydown0#slide

#menu
a.menulink#menulink1(href="#")
p RED
a.menulink#menulink2(href="#")
p BLUE
a.menulink#menulink3(href="#")
p GREEN


CSS (Sass)



a
color: white
text-decoration: none

#slide
width: 10px
height: 30px
position: absolute
border-radius: 15px
transition: 0.3s ease-in-out

#slide.staydown0
margin-top: 0px
background-color: red

#slide.staydown1
margin-top: 40px
background-color: blue

#slide.staydown2
margin-top: 80px
background-color: green

#slide.movedown0
margin-top: 0px
background-color: red


#slide.movedown1
margin-top: 40px
background-color: blue

#slide.movedown2
margin-top: 80px
background-color: green

#menu
margin-left: 30px

.menulink
height: 40px
width: 70px
display: block

.menulink p
height: 30px
width: 70px
margin: 0px
border-radius: 15px
display: flex
align-items: center
justify-content: center

#menulink1 p
background: red
#menulink2 p
background: blue
#menulink3 p
background: green



jQuery



$('#menulink1').hover(
function(){
$('#slide').addClass('movedown0');
},
function(){
$('#slide').removeClass('movedown0')

});

$('#menulink2').hover(
function(){
$('#slide').addClass('movedown1');
},
function(){
$('#slide').removeClass('movedown1')

});

$('#menulink3').hover(
function(){
$('#slide').addClass('movedown2');
},
function(){
$('#slide').removeClass('movedown2')

});

///////

$('#menulink1').on(
'click', function(){
$('#slide').addClass('staydown0').removeClass('staydown1 staydown2');

});

$('#menulink2').on(
'click', function(){
$('#slide').addClass('staydown1').removeClass('staydown0 staydown2');

});

$('#menulink3').on(
'click', function(){
$('#slide').addClass('staydown2').removeClass('staydown0 staydown1');

});










share|improve this question











$endgroup$



















    1












    $begingroup$


    The code I'm bringing forward is for a vertical menu, where I needed a marker to do the following:




    • When hovering over a link the marker should slide down next to it.


    • When not hovering over a link the marker should return to the side of the current selected link (the default being the first link).


    • When a link is clicked the marker should remain next to the clicked link, unless another link is being hovered over.



    What I've done in jQuery is add a class ('.staydown0','.staydown1' or '.staydown2') to the marker div ('#slide') when one one of the links ('#menulink1','#menulink2' or '#menulink3') is clicked, and remove any other classes that would have it be situated next to one of the other links.



    Then I've set jQuery to add a class ('.movedown0','.movedown1' or '.movedown2') to the marker div ('#slide') when the mouse hovers over one of the links ('#menulink1','#menulink2' or '#menulink3'), and remove the class when the mouse stops hovering over the link.



    The code works, but it is redundant. How could I go about abstracting it?



    I've already asked a similar question recently where I only had the "hover" part of the menu set up, and it was solved.



    But I am a beginner in javascript and jQuery, and even though I've managed to understand the solution that was offered to me in the previous question, I am at a loss as to how to extrapolate that knowledge to abstracting this more convoluted code.



    CODEPEN



    HTML(Pug):



    .staydown0#slide

    #menu
    a.menulink#menulink1(href="#")
    p RED
    a.menulink#menulink2(href="#")
    p BLUE
    a.menulink#menulink3(href="#")
    p GREEN


    CSS (Sass)



    a
    color: white
    text-decoration: none

    #slide
    width: 10px
    height: 30px
    position: absolute
    border-radius: 15px
    transition: 0.3s ease-in-out

    #slide.staydown0
    margin-top: 0px
    background-color: red

    #slide.staydown1
    margin-top: 40px
    background-color: blue

    #slide.staydown2
    margin-top: 80px
    background-color: green

    #slide.movedown0
    margin-top: 0px
    background-color: red


    #slide.movedown1
    margin-top: 40px
    background-color: blue

    #slide.movedown2
    margin-top: 80px
    background-color: green

    #menu
    margin-left: 30px

    .menulink
    height: 40px
    width: 70px
    display: block

    .menulink p
    height: 30px
    width: 70px
    margin: 0px
    border-radius: 15px
    display: flex
    align-items: center
    justify-content: center

    #menulink1 p
    background: red
    #menulink2 p
    background: blue
    #menulink3 p
    background: green



    jQuery



    $('#menulink1').hover(
    function(){
    $('#slide').addClass('movedown0');
    },
    function(){
    $('#slide').removeClass('movedown0')

    });

    $('#menulink2').hover(
    function(){
    $('#slide').addClass('movedown1');
    },
    function(){
    $('#slide').removeClass('movedown1')

    });

    $('#menulink3').hover(
    function(){
    $('#slide').addClass('movedown2');
    },
    function(){
    $('#slide').removeClass('movedown2')

    });

    ///////

    $('#menulink1').on(
    'click', function(){
    $('#slide').addClass('staydown0').removeClass('staydown1 staydown2');

    });

    $('#menulink2').on(
    'click', function(){
    $('#slide').addClass('staydown1').removeClass('staydown0 staydown2');

    });

    $('#menulink3').on(
    'click', function(){
    $('#slide').addClass('staydown2').removeClass('staydown0 staydown1');

    });










    share|improve this question











    $endgroup$















      1












      1








      1





      $begingroup$


      The code I'm bringing forward is for a vertical menu, where I needed a marker to do the following:




      • When hovering over a link the marker should slide down next to it.


      • When not hovering over a link the marker should return to the side of the current selected link (the default being the first link).


      • When a link is clicked the marker should remain next to the clicked link, unless another link is being hovered over.



      What I've done in jQuery is add a class ('.staydown0','.staydown1' or '.staydown2') to the marker div ('#slide') when one one of the links ('#menulink1','#menulink2' or '#menulink3') is clicked, and remove any other classes that would have it be situated next to one of the other links.



      Then I've set jQuery to add a class ('.movedown0','.movedown1' or '.movedown2') to the marker div ('#slide') when the mouse hovers over one of the links ('#menulink1','#menulink2' or '#menulink3'), and remove the class when the mouse stops hovering over the link.



      The code works, but it is redundant. How could I go about abstracting it?



      I've already asked a similar question recently where I only had the "hover" part of the menu set up, and it was solved.



      But I am a beginner in javascript and jQuery, and even though I've managed to understand the solution that was offered to me in the previous question, I am at a loss as to how to extrapolate that knowledge to abstracting this more convoluted code.



      CODEPEN



      HTML(Pug):



      .staydown0#slide

      #menu
      a.menulink#menulink1(href="#")
      p RED
      a.menulink#menulink2(href="#")
      p BLUE
      a.menulink#menulink3(href="#")
      p GREEN


      CSS (Sass)



      a
      color: white
      text-decoration: none

      #slide
      width: 10px
      height: 30px
      position: absolute
      border-radius: 15px
      transition: 0.3s ease-in-out

      #slide.staydown0
      margin-top: 0px
      background-color: red

      #slide.staydown1
      margin-top: 40px
      background-color: blue

      #slide.staydown2
      margin-top: 80px
      background-color: green

      #slide.movedown0
      margin-top: 0px
      background-color: red


      #slide.movedown1
      margin-top: 40px
      background-color: blue

      #slide.movedown2
      margin-top: 80px
      background-color: green

      #menu
      margin-left: 30px

      .menulink
      height: 40px
      width: 70px
      display: block

      .menulink p
      height: 30px
      width: 70px
      margin: 0px
      border-radius: 15px
      display: flex
      align-items: center
      justify-content: center

      #menulink1 p
      background: red
      #menulink2 p
      background: blue
      #menulink3 p
      background: green



      jQuery



      $('#menulink1').hover(
      function(){
      $('#slide').addClass('movedown0');
      },
      function(){
      $('#slide').removeClass('movedown0')

      });

      $('#menulink2').hover(
      function(){
      $('#slide').addClass('movedown1');
      },
      function(){
      $('#slide').removeClass('movedown1')

      });

      $('#menulink3').hover(
      function(){
      $('#slide').addClass('movedown2');
      },
      function(){
      $('#slide').removeClass('movedown2')

      });

      ///////

      $('#menulink1').on(
      'click', function(){
      $('#slide').addClass('staydown0').removeClass('staydown1 staydown2');

      });

      $('#menulink2').on(
      'click', function(){
      $('#slide').addClass('staydown1').removeClass('staydown0 staydown2');

      });

      $('#menulink3').on(
      'click', function(){
      $('#slide').addClass('staydown2').removeClass('staydown0 staydown1');

      });










      share|improve this question











      $endgroup$




      The code I'm bringing forward is for a vertical menu, where I needed a marker to do the following:




      • When hovering over a link the marker should slide down next to it.


      • When not hovering over a link the marker should return to the side of the current selected link (the default being the first link).


      • When a link is clicked the marker should remain next to the clicked link, unless another link is being hovered over.



      What I've done in jQuery is add a class ('.staydown0','.staydown1' or '.staydown2') to the marker div ('#slide') when one one of the links ('#menulink1','#menulink2' or '#menulink3') is clicked, and remove any other classes that would have it be situated next to one of the other links.



      Then I've set jQuery to add a class ('.movedown0','.movedown1' or '.movedown2') to the marker div ('#slide') when the mouse hovers over one of the links ('#menulink1','#menulink2' or '#menulink3'), and remove the class when the mouse stops hovering over the link.



      The code works, but it is redundant. How could I go about abstracting it?



      I've already asked a similar question recently where I only had the "hover" part of the menu set up, and it was solved.



      But I am a beginner in javascript and jQuery, and even though I've managed to understand the solution that was offered to me in the previous question, I am at a loss as to how to extrapolate that knowledge to abstracting this more convoluted code.



      CODEPEN



      HTML(Pug):



      .staydown0#slide

      #menu
      a.menulink#menulink1(href="#")
      p RED
      a.menulink#menulink2(href="#")
      p BLUE
      a.menulink#menulink3(href="#")
      p GREEN


      CSS (Sass)



      a
      color: white
      text-decoration: none

      #slide
      width: 10px
      height: 30px
      position: absolute
      border-radius: 15px
      transition: 0.3s ease-in-out

      #slide.staydown0
      margin-top: 0px
      background-color: red

      #slide.staydown1
      margin-top: 40px
      background-color: blue

      #slide.staydown2
      margin-top: 80px
      background-color: green

      #slide.movedown0
      margin-top: 0px
      background-color: red


      #slide.movedown1
      margin-top: 40px
      background-color: blue

      #slide.movedown2
      margin-top: 80px
      background-color: green

      #menu
      margin-left: 30px

      .menulink
      height: 40px
      width: 70px
      display: block

      .menulink p
      height: 30px
      width: 70px
      margin: 0px
      border-radius: 15px
      display: flex
      align-items: center
      justify-content: center

      #menulink1 p
      background: red
      #menulink2 p
      background: blue
      #menulink3 p
      background: green



      jQuery



      $('#menulink1').hover(
      function(){
      $('#slide').addClass('movedown0');
      },
      function(){
      $('#slide').removeClass('movedown0')

      });

      $('#menulink2').hover(
      function(){
      $('#slide').addClass('movedown1');
      },
      function(){
      $('#slide').removeClass('movedown1')

      });

      $('#menulink3').hover(
      function(){
      $('#slide').addClass('movedown2');
      },
      function(){
      $('#slide').removeClass('movedown2')

      });

      ///////

      $('#menulink1').on(
      'click', function(){
      $('#slide').addClass('staydown0').removeClass('staydown1 staydown2');

      });

      $('#menulink2').on(
      'click', function(){
      $('#slide').addClass('staydown1').removeClass('staydown0 staydown2');

      });

      $('#menulink3').on(
      'click', function(){
      $('#slide').addClass('staydown2').removeClass('staydown0 staydown1');

      });







      javascript beginner jquery html sass






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 19 at 5:43









      200_success

      132k20158423




      132k20158423










      asked Apr 18 at 17:47









      Alberto BurritoAlberto Burrito

      233




      233






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217685%2fsliding-marker-on-link-hover-and-click-with-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217685%2fsliding-marker-on-link-hover-and-click-with-jquery%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

          Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...