-
Notifications
You must be signed in to change notification settings - Fork 2
/
overflow-scroller.js
121 lines (99 loc) · 3.89 KB
/
overflow-scroller.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
116
117
118
119
/*
$.overflowScroller();
Dependencies:
jquery;
jquery-throttle-debounce
Modernizr.hiddenscroll property
adds scrolling arrows to elements with overflow scroll,
all elements must be the same width
*/
(function ($) {
$.fn.overflowScroller = function (args) {
var options = {
scrollPercentage: 1,
scrollSpeed: 400,
snapPoints: true
};
var arrowHiddenClass = 'overflow-scroller-arrow-hide';
$.extend( options, args );
this.each(function(){
var $overflowScroller = $(this),
scrollContainerWidth = $overflowScroller.width(),
scrollElementWidth = $overflowScroller.children(':eq(0)').width(),
scrollElementTotalWidth = scrollElementWidth * $overflowScroller.children().length,
scrollPosition = 0,
isClicked = false;
$overflowScroller.toggleClass('snap-points', options.snapPoints);
$overflowScroller.wrap('<div class="overflow-scroller-outer"></div>');
var $overflowScrollerOuter = $overflowScroller.parent('.overflow-scroller-outer');
$overflowScrollerOuter.append('<div class="overflow-scroller-arrow overflow-scroller-arrow-previous overflow-scroller-arrow-hide"></div><div class="overflow-scroller-arrow overflow-scroller-arrow-next overflow-scroller-arrow-hide"></div>');
var $arrows = $overflowScrollerOuter.find('.overflow-scroller-arrow'),
$arrowPrevious = $overflowScrollerOuter.find('.overflow-scroller-arrow-previous'),
$arrowNext = $overflowScrollerOuter.find('.overflow-scroller-arrow-next');
var setScrollArrows = function(){
scrollPosition = $overflowScroller.scrollLeft();
if(scrollPosition >= (scrollElementTotalWidth - scrollContainerWidth)) {
$arrowNext.addClass(arrowHiddenClass);
}
else if (scrollPosition === 0) {
$arrowPrevious.addClass(arrowHiddenClass);
}
else {
$arrows.removeClass(arrowHiddenClass);
}
};
if(scrollElementTotalWidth > scrollContainerWidth) {
//if the total width is greater than the container show the arrows and trigger functionality
scrollPosition = $overflowScroller.scrollLeft();
//detect initial scroll postion and show/hide arrows.
if(scrollPosition === 0) {
$arrowNext.removeClass(arrowHiddenClass);
}
else {
$arrows.removeClass(arrowHiddenClass);
}
//bind events
$arrowNext.on('click',function(){
if(!isClicked) {
$arrowPrevious.removeClass(arrowHiddenClass);
isClicked = true; //block double clicking
$overflowScroller.animate({
scrollLeft: $overflowScroller.scrollLeft() + scrollContainerWidth * options.scrollPercentage
},
options.scrollSpeed,
function(){
isClicked = false;
});
}
});
$arrowPrevious.on('click',function(){
if(!isClicked) {
$arrowNext.removeClass(arrowHiddenClass);
isClicked = true; //block double clicking
$overflowScroller.animate({
scrollLeft: $overflowScroller.scrollLeft() - scrollContainerWidth * options.scrollPercentage
},
options.scrollSpeed,
function(){
isClicked = false;
});
}
});
if($.debounce !== undefined) {
$overflowScroller.scroll($.debounce( 100, function(){
setScrollArrows();
}));
} else {
//debounce is not loaded.
$overflowScroller.scroll(setScrollArrows);
}
//reset total values on resize
$(window).on('resize.overflowScroller',function(){
scrollContainerWidth = $overflowScroller.width();
scrollElementWidth = $overflowScroller.children(':eq(0)').width();
scrollElementTotalWidth = scrollElementWidth * $overflowScroller.children().length;
});
}
});
};
})( jQuery );