Use jQuery to change a class dependent on scroll position

后端 未结 3 1758
失恋的感觉
失恋的感觉 2021-01-01 06:44

I have a single page website which has a fixed floating nav. I want to be able to highlight which section the user is on by adding a class of say \"on\" to the relevant navi

3条回答
  •  臣服心动
    2021-01-01 07:06

    You could grab the window's scrollTop with

    var scrollTop=$(window).scrollTop();
    

    http://api.jquery.com/scrollTop

    Then you go through the content

    s and cumulatively add their height up until you get to a value that is close enough to scrollTop. You probably have to experiment a little with regards to how you handle overlap.

    Once you figured that your are scrolled to the content of, say, your portfolio you add the relevant class to the nav element.

提交回复
热议问题