highlight menu on scrolling (if reach div)

后端 未结 2 899
逝去的感伤
逝去的感伤 2021-02-05 08:30

i want highlight the menu point if the div is scrolled // or clicked.

http://jsfiddle.net/WeboGraph/vu6hN/2/ (thats an example what i want)

my code: (JS)

<
相关标签:
2条回答
  • 2021-02-05 09:19

    Found this in 2018 and ran into a syntax error trying to replicate with a more recent version of jquery (+1.0). In the last line $('nav a[href=#'+ id +']') the attribute is not properly escaped and needs to be like so $('nav a[href="#'+ id +'"]') (note added "").

    Here's where I found this https://api.jquery.com/attribute-contains-selector/

    0 讨论(0)
  • 2021-02-05 09:22

    Use $(this).offset().top instead of $(this).position().top

    Fiddle

    As .position() get the current coordinates of the first element in the set of matched elements, relative to the offset parent whereas .offset() get the current coordinates of the first element in the set of matched elements, relative to the document.

    In your website all the DIV with class inside .target are inside therefore all the element of class .target are returning the value .position().top equal to 0.

    Decrease the offset value so that the class change when element reach the menu by making the if condition like this:

    if($(window).scrollTop() >= $(this).offset().top - $("#cssmenu").height())
    
    0 讨论(0)
提交回复
热议问题