forked from mathiasbynens/jquery-smooth-scrolling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.smoothscroll.js
44 lines (41 loc) · 1.44 KB
/
jquery.smoothscroll.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*! http://mths.be/smoothscroll v1.5.2 by @mathias */
;(function(document, $) {
var $scrollElement = (function() {
// Find out what to scroll (html or body)
var $html = $(document.documentElement),
$body = $(document.body),
bodyScrollTop;
if ($html.scrollTop()) {
return $html;
} else {
bodyScrollTop = $body.scrollTop();
// If scrolling the body doesn’t do anything
if ($body.scrollTop(bodyScrollTop + 1).scrollTop() == bodyScrollTop) {
return $html;
} else {
// We actually scrolled, so undo it
return $body.scrollTop(bodyScrollTop);
}
}
}());
$.fn.smoothScroll = function(speed) {
speed = ~~speed || 400;
// Look for links to anchors (on any page)
return this.find('a[href*="#"]').click(function(event) {
var hash = this.hash,
$hash = $(hash); // The in-document element the link points to
// If it’s a link to an anchor in the same document
if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) {
// If the anchor actually exists…
if ($hash.length) {
// …don’t jump to the link right away…
event.preventDefault();
// …and smoothly scroll to it
$scrollElement.stop().animate({ 'scrollTop': $hash.offset().top }, speed, function() {
location.hash = hash;
});
}
}
}).end();
};
}(document, jQuery));