Change url when manually scrolled to an anchor?

后端 未结 6 1752
失恋的感觉
失恋的感觉 2020-11-30 01:53

By Default, if I have anchors in my website, then the URL on the address bar is changed, when I click on a link (ie. www.mysite.com/#anchor)

Is it possible to change

6条回答
  •  -上瘾入骨i
    2020-11-30 02:42

    I think you need to do something like this. Not tried in action

    var myElements = $("div.anchor"); // You target anchors
    $(window).scroll(function(e) {
        var scrollTop = $(window).scrollTop();
        myElements.each(function(el,i) {
            if ($(this).offset().top > scrollTop && $(myElements[i+1]).offset().top < scrollTop) {
                 location.hash = this.id;
            }
        });
    });`
    

提交回复
热议问题