-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.scrollToChild.js
More file actions
56 lines (53 loc) · 2.46 KB
/
jquery.scrollToChild.js
File metadata and controls
56 lines (53 loc) · 2.46 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
/**
* jQuery scrollToChild plugin v0.1
* @author: Barnabas Tarkovacs
* TODO: implement different scrollPane-position cases
*/
(function ($) {
var defaults = {
scrollDuration: 200,
noAnim: false,
scrollMargin: 0,
scrollEase: 'linear',
visibilityBased: false, // if true then scrolls only if element is not already visible in the scrollPane
onNoElementFound: $.noop, // callback if no target element found in scrollPane
onAnimationComplete: $.noop
};
$.fn.scrollToChild = function scrollToChild(childElement, options) {
options = $.extend({}, defaults, options);
// normalize animation options
if (typeof options.scrollDuration !== 'number') {
options.scrollDuration = defaults.scrollDuration;
}
if (typeof options.scrollEase !== 'string' || options.scrollEase === '') {
options.scrollEase = defaults.scrollEase;
}
$(this).each(function () {
var $scrollPane = $(this),
$element = $scrollPane.find(childElement).eq(0),
paneHeight, currentTopPos, currentBottomPos, elemHeight, elemTopPos, elemBottomPos, newPos;
if (!$element.length) {
options.onNoElementFound.call(this, childElement);
return;
}
paneHeight = $scrollPane.height();
currentTopPos = $scrollPane.scrollTop();
currentBottomPos = currentTopPos + paneHeight;
elemHeight = $element.height();
elemTopPos = $element.offset().top - $scrollPane.offset().top + currentTopPos;
elemBottomPos = elemTopPos + elemHeight;
// Scroll only if animation is forced (visibilityBased == false), or if target element is not visible from top to bottom
if (!options.visibilityBased || elemBottomPos < currentTopPos || elemTopPos > currentBottomPos || elemBottomPos > currentBottomPos || elemTopPos < currentTopPos) {
newPos = elemTopPos - options.scrollMargin;
newPos = newPos > 0 ? newPos : 0;
if (options.noAnim) {
$scrollPane.scrollTop(newPos);
options.onAnimationComplete.call($scrollPane);
} else {
$scrollPane.animate({ scrollTop: newPos }, options.scrollDuration, options.scrollEase, options.onAnimationComplete);
}
}
});
return this;
};
}(jQuery));