/
jquery.simplr.smoothscroll.js
62 lines (49 loc) · 1.38 KB
/
jquery.simplr.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* jquery.simplr.smoothscroll
* version 1.0.1
* copyright (c) 2012 https://github.com/simov/simplr-smoothscroll
* licensed under MIT
* requires jquery.mousewheel - https://github.com/jquery/jquery-mousewheel
*/
;(function ($) {
'use strict'
$.srSmoothscroll = function (options) {
// defaults
var self = $.extend({
step: 55,
speed: 400,
ease: 'swing',
target: $('body'),
container: $(window)
}, options || {})
// private fields & init
var container = self.container
, top = 0
, step = self.step
, viewport = container.height()
, wheel = false
var target = (self.target.selector == 'body')
? ((navigator.userAgent.indexOf('AppleWebKit') !== -1) ? self.target : $('html'))
: container
// events
self.target.mousewheel(function (event, delta) {
wheel = true
if (delta < 0) // down
top = (top+viewport) >= self.target.outerHeight(true) ? top : top+=step
else // up
top = top <= 0 ? 0 : top-=step
target.stop().animate({scrollTop: top}, self.speed, self.ease, function () {
wheel = false
})
return false
})
container
.on('resize', function (e) {
viewport = container.height()
})
.on('scroll', function (e) {
if (!wheel)
top = container.scrollTop()
})
}
})(jQuery);