-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.scrollTop.js
115 lines (97 loc) · 3.39 KB
/
jquery.scrollTop.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*!
* Version 1.0 (14 Jul 2012)
* Requires jQuery 1.4 or newer
*/
(function ($) {
var defaults = {
duration: "fast",
offset: 0
};
var rootrx = /^(?:html)$/i;
var topBorders = function (domElement, styles) {
styles = styles || (document.defaultView && document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(domElement, null) : domElement.currentStyle);
var isStyleExist = document.defaultView && document.defaultView.getComputedStyle ? true : false;
return {
top: (parseFloat(isStyleExist ? styles.borderTopWidth : $.css(domElement, "borderTopWidth")) || 0)
};
};
var dimensions = function ($element) {
var win = $(window);
var isRoot = rootrx.test($element[0].nodeName);
return {
scroll: {
top: (isRoot ? win : $element).scrollTop()
},
border: isRoot ? { top: 0} : topBorders($element[0]),
rect: (function () {
return {
top: isRoot ? 0 : $element[0].getBoundingClientRect().top
};
})()
};
};
$.fn.extend({
ScrollTop: function (options) {
options = $.extend({}, defaults, options);
var el = this.eq(0);
var scroller = el.closest(":scrollable");
// check if there's anything to scroll in the first place
var animOptions = {};
if (scroller.length > 0) {
scroller = scroller.eq(0);
var dim = {
e: dimensions(el),
s: dimensions(scroller)
};
var rel = {
top: dim.e.rect.top - (dim.s.rect.top + dim.s.border.top)
};
animOptions.scrollTop = dim.s.scroll.top + rel.top;
}
else {
animOptions.scrollTop = $(el).scrollTop();
}
if (!$.isEmptyObject(animOptions)) {
if (rootrx.test(scroller[0].nodeName)) {
scroller = $("html,body");
}
scroller
.animate(animOptions, options.duration)
.eq(0)
.queue(function (next) {
$.isFunction(options.complete) && options.complete.call(scroller[0]);
next();
});
}
return this;
}
});
var scrollValue = {
auto: true,
scroll: true,
visible: false,
hidden: false
};
$.extend($.expr[":"], {
scrollable: function (element, index) {
var styles = (document.defaultView && document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(element, null) : element.currentStyle);
var overflow = {
y: scrollValue[styles.overflowY.toLowerCase()] || false,
isRoot: rootrx.test(element.nodeName)
};
if (!overflow.y && !overflow.isRoot) {
return false;
}
var vertical = {
h: {
sh: element.scrollHeight,
ch: element.clientHeight
},
scrollableY: function () {
return (overflow.y || overflow.isRoot) && this.h.sh > this.h.ch;
}
};
return vertical.scrollableY();
}
});
})(jQuery);