首页 > 技术文章 > JQuery 实现锚点链接之间的平滑滚动

duowen 2016-03-11 16:43 原文

24. 解决链接锚点的生硬问题

 $('.nav .btn[href*=#],.icon2,.icon3').click(function() {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[id=' + this.hash.slice(1) + ']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({
                    scrollTop: targetOffset
                },
                1000);
                return false;
            }
        }
  })

推荐阅读