簡體   English   中英

更改autoscroll的URL(pushstate?)

[英]Change URL for autoscroll (pushstate?)

我目前正在使用此代碼,我發現自動滾動到錨點:

$(function() {
    $('nav a[href*=#]').bind('click',function(event){
        var $anchor = $(this);

        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top - 100
        }, 1000);
        event.preventDefault();

    });
});

我可以添加什么來確保在單擊時將#anchor添加到URL中?

我看過這個用過,但我不知道如何添加它..

function() {
        if(history.pushState) {
            history.pushState(null, null, target);
        }
        else {
            location.hash = target;
        }
      });

非常感謝!!!

完成動畫后運行它:( 漂亮的演示在這里

$(function () {
    $('nav a[href*=#]').on('click', function (event) {
        var $anchor = $(this),
            name = $anchor.attr("href").match(/#(.+)/i)[1];

        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top - 100
        }, 1000, function () {
            if (history.pushState) {
                history.pushState(null, null, "#" + name);
            } else {
                location.hash = name;
            }
        });
        event.preventDefault();
    });
});

工作演示: http //jsfiddle.net/DerekL/uPS9W/show

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM