我怎么能搬到

时间:2013-04-22 01:25:48

标签: javascript jquery html

我有这段代码:

<a name="point1"> < /a>   //  at the very beginning   

<a href="link.php#point1"> Link1 < /a>     //    at the very end

当您点击Link1时,浏览器会快速移动到point1
我可以让浏览器慢慢移动吗?

1 个答案:

答案 0 :(得分:2)

通过设置scrollTophtml 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;
        }
    }
});