-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anchor-Scroll.js
25 lines (24 loc) · 934 Bytes
/
Anchor-Scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Load jQuery.js firstly
jQuery(function() {
jQuery('a[href*=#]').click(function() {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var $target = jQuery(this.hash);
var $url = this.hash.slice(1);
var $scrollTime = 500;
function updateUrl() {
window.location.hash = encodeURIComponent($url);
}
$target = $target.length && $target || jQuery('[name=' + $url + ']');
if (!$url) {
return false;
} else if ($target.length) {
var targetOffset = $target.offset().top;
jQuery('html,body').animate({
scrollTop: targetOffset
}, $scrollTime);
setTimeout(updateUrl, $scrollTime + 100)
return false;
}
}
});
});