Javascript jQuery平滑滚动不';不要更改URL
一切都正常工作,但是当jQuery就位时,URL不会改变。有没有办法让它在平滑滚动的同时更改url?在此之前,我尝试了一种不同的方法,但是它不像这样兼容跨浏览器 我的HTML是: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
<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;
});
});