-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmoothScroll.js
More file actions
31 lines (29 loc) · 992 Bytes
/
smoothScroll.js
File metadata and controls
31 lines (29 loc) · 992 Bytes
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
/**
*
* @authors ryan (543915174@qq.com)
* @date 2016-09-28 13:39:39
* @version 1.0
*/
$.smoothScroll = function(options){
options = $.extend(true, {
scrollTime:400,
scrollDistance:300,
detail:120
}, options);
var $window = jQuery(window);
mobile_ie = -1 !== navigator.userAgent.indexOf("IEMobile");
var bodyAndHtml = $('html,body');
function smoothScrollListener(event){
event.preventDefault();
var delta = event.wheelDelta / options.detail || -event.detail / 3;
var scrollTop = $window.scrollTop();
var finalScroll = scrollTop - parseInt(delta * options.scrollDistance);
bodyAndHtml.stop().animate({scrollTop: finalScroll},options.scrollTime);
}
if (!jQuery('html').hasClass('touch') && !mobile_ie) {
if (window.addEventListener) {
window.addEventListener('mousewheel', smoothScrollListener, false);
window.addEventListener('DOMMouseScroll', smoothScrollListener, false);
}
}
}