学海无涯,回头是岸
不务正业
极简老人

对锚链接滑动的优化,废话不多说直接上代码。

JS代码

$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); 
$(document).ready(function() {  
    $('a[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;  
            }  
        }  
    });  
});  

使用前引用jQuery。

 

posted on 2017-07-25 17:33  MirageFireFox  阅读(255)  评论(0)    收藏  举报