jQuery的平滑滚动和URL字段
问题描述:
我拿到这个剧本......jQuery的平滑滚动和URL字段
$(function(){
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'#')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({ scrollTop: target.offset().top }, 1000);
location.hash = (this.hash);
return false;
}
}
});
});
...但我与页跳转时,滚动到顶部的问题,但如果我拿出行.. 。
location.hash = (this.hash);
它会正常工作,滚动都很好,但URL不会附加到被点击的锚链接。
基本上我需要的URL显示:http://www.example.com/#home < - 注意散列。
在此先感谢!
答
使用回调.animate
$(function(){
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'#')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
var hash = this.hash; // 'this' will not be in scope in callback
$('html,body').animate({ scrollTop: target.offset().top }, 1000, function() {
location.hash = hash;
});
return false;
}
}
});
});
我从来没有得到它的工作,所以用另一个脚本去。 – Kia 2012-04-22 11:41:51