forked from Arwid/jQuery.scrollIntoView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scrollIntoView.js
More file actions
145 lines (125 loc) · 4.45 KB
/
Copy pathjquery.scrollIntoView.js
File metadata and controls
145 lines (125 loc) · 4.45 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @preserve $.fn.scrollIntoView - similar to the default browser scrollIntoView
* The default browser behavior always places the element at the top or bottom of its container.
* This override is smart enough to not scroll if the element is already visible.
*
* Original script was developed by Arwid Bancewicz. Here follows the original licensing:
*
* Copyright 2011 Arwid Bancewicz
* Licensed under the MIT license
* http://www.opensource.org/licenses/mit-license.php
*
* @date 15 Feb 2016
* @author Arwid Bancewicz http://arwid.ca (up to version 0.3)
* @author Leonardo Fernandes <leonardo.monteiro.fernandes@gmail.com>
* @version 0.5
*/
(function($) {
var commonAncestor = function() {
var parents = this.parents();
for (var i = 0; i < parents.length; i++) {
var parent = $(parents[i]);
var containsAll = true;
this.each(function() {
if (!parent.has(this)) {
containsAll = false;
return false;
}
});
if (containsAll) {
return parent;
}
}
return $([]);
}
var scrollableParent = function() {
// start from the common ancestor
var pEl = commonAncestor.call(this);
// go up parents until we find one that scrolls
while (!pEl.is("html")) {
// it wiggles?
var scrollTop = pEl.get(0).scrollTop;
pEl.get(0).scrollTop = scrollTop;
pEl.get(0).scrollTop++;
if (pEl.get(0).scrollTop != scrollTop) {
pEl.get(0).scrollTop = scrollTop;
return pEl;
}
pEl.get(0).scrollTop = scrollTop;
pEl.get(0).scrollTop--;
if (pEl.get(0).scrollTop != scrollTop) {
pEl.get(0).scrollTop = scrollTop;
return pEl;
}
// try next parent
pEl = pEl.parent();
}
return pEl;
};
var scroll = function(el, delta, opts) {
if (opts.smooth) {
el.stop().animate({ scrollTop: el.get(0).scrollTop + delta }, opts);
} else {
el.get(0).scrollTop += delta;
if ($.isFunction(opts.complete)) {
opts.complete.call(el.get(0));
}
}
}
$.fn.scrollIntoView = function(duration, easing, complete) {
// The arguments are optional.
// The first argment can be false for no animation or a duration.
// The first argment could also be a map of options.
// Refer to http://api.jquery.com/animate/.
if (this.length == 0) {
return this;
}
var opts = $.extend({}, $.fn.scrollIntoView.defaults);
// Get options
if ($.type(duration) == "object") {
$.extend(opts, duration);
} else if ($.type(duration) == "number") {
$.extend(opts, { duration: duration, easing: easing, complete: complete });
} else if (duration == false) {
opts.smooth = false;
}
// get enclosing offsets
var elY = Infinity, elH = 0;
this.each(function() {
if ($(this).offset().top < elY) {
elY = $(this).offset().top;
}
if ($(this).offset().top + $(this).outerHeight(false) > elY + elH) {
elH = $(this).offset().top + $(this).outerHeight(false) - elY;
}
});
var pEl = scrollableParent.call(this);
var pY = pEl.offset().top;
var pH = pEl.height();
if (pEl.is("html") || pEl.is("body")) {
pY = pEl.get(0).scrollTop;
pH = $(window).height();
}
if (elY <= pY || elH > pH) {
scroll(pEl, elY - pY, opts); // scroll up
} else if (elY + elH > pY + pH) {
scroll(pEl, elY + elH - pY - pH, opts); // scroll down
} else {
// no scroll
if ($.isFunction(opts.complete)) {
opts.complete.call(el);
}
}
return this;
};
$.fn.scrollIntoView.defaults = {
smooth: true,
duration: null,
easing: $.easing && $.easing.easeOutExpo ? 'easeOutExpo': null,
// Note: easeOutExpo requires jquery.effects.core.js
// otherwise jQuery will default to use 'swing'
complete: $.noop(),
step: null,
specialEasing: {} // cannot be null in jQuery 1.8.3
};
})(jQuery);