我有这段代码:
<a name="point1"> < /a> // at the very beginning
<a href="link.php#point1"> Link1 < /a> // at the very end
当您点击Link1时,浏览器会快速移动到point1
我可以让浏览器慢慢移动吗?
答案 0 :(得分:2)
通过设置scrollTop
和html
body
的动画,可以很容易地使用jQuery:
http://css-tricks.com/snippets/jquery/smooth-scrolling/
以下是该页面上a comment的摘录:
$('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);
return false;
}
}
});