jquery实现锚点链接平滑效果

来源:互联网 发布:安卓 记账软件 编辑:程序博客网 时间:2024/05/17 08:09

在html页面加上锚点链接后写上以下js代码:

   $(function(){        $('a[href*=#],area[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 targetOffset = $target.offset().top;                    $('html,body').animate({                            scrollTop: targetOffset                        },                        1000);                    return false;                }            }        });    })


原创粉丝点击