Justin's Words

Anchor 平滑滚动

原代码出自 CSS-Tricks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$(function () {
$('a[href*=#]:not([herf=#])').click(function(event) {
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;
}
}
});
})