Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery平滑滚动不';不要更改URL_Javascript_Jquery - Fatal编程技术网

Javascript jQuery平滑滚动不';不要更改URL

Javascript jQuery平滑滚动不';不要更改URL,javascript,jquery,Javascript,Jquery,一切都正常工作,但是当jQuery就位时,URL不会改变。有没有办法让它在平滑滚动的同时更改url?在此之前,我尝试了一种不同的方法,但是它不像这样兼容跨浏览器 我的HTML是: <li class="representing-you-online"><a href="#representing-you-online">Representing you online</a></li> <li class="developing-your-pe

一切都正常工作,但是当jQuery就位时,URL不会改变。有没有办法让它在平滑滚动的同时更改url?在此之前,我尝试了一种不同的方法,但是它不像这样兼容跨浏览器

我的HTML是:

<li class="representing-you-online"><a href="#representing-you-online">Representing you online</a></li>
<li class="developing-your-people"><a href="#developing-your-people">Developing your people</a></li>

谢谢

替换锚点的点击处理代码,如下所示:

$(document).ready(function() {
    $('a[href^="#"]').click(function() {
        var target = $(this.hash);
        var hash = this.hash;
        if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');
        if (target.length == 0) target = $('html');
        $('html, body').animate({ scrollTop: target.offset().top }, 500, function (){
            location.hash = hash;
        });
        return false;
    });
});
请注意jquery.animate()末尾的完整函数。它会更改URL

请看演示

$(document).ready(function() {
    $('a[href^="#"]').click(function() {
        var target = $(this.hash);
        var hash = this.hash;
        if (target.length == 0) target = $('a[name="' + this.hash.substr(1) + '"]');
        if (target.length == 0) target = $('html');
        $('html, body').animate({ scrollTop: target.offset().top }, 500, function (){
            location.hash = hash;
        });
        return false;
    });
});