search

Home  >  Q&A  >  body text

javascript - How to get the dom element at the top of the current page in js.

Mainly I want to achieve an effect similar to the right navigation bar of Baidu Encyclopedia. As the page scrolls, the navigation bar on the right also changes. Thanks.

http://baike.baidu.com/item/G...

黄舟黄舟2779 days ago666

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:31:38

    Listen to the window.onscroll event. When scrolling, dynamically determine the current scrolling distance. The code is roughly as follows:

    window.onscroll = function(){
        var height = document.documentElement.scrollTop + document.body.scrollTop;
        if(height >= '目录锚点高度'){
            //TODO 给当前锚点加高亮class,并且去掉其他高亮的锚点的样式
        }
    }

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:31:38

    I don’t know if this is a similar effect you want? A small demo written by myself

        https://jsfiddle.net/f59e8enj/

    reply
    0
  • Cancelreply