-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbannerscroller.js
139 lines (122 loc) · 3.93 KB
/
bannerscroller.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(function( $ ) {
$.fn.bannerScroller = function( options ) {
return this.each(function() {
var settings = $.extend({
leftSelector: '.scroll.left',
rightSelector: '.scroll.right',
viewportSelector: '.viewport',
contentSelector: '.scroller-content',
itemsSelector: '.item',
animationIntervalTime: 2000,
itemNotViewdThreshold: 5,
callback: function () {}
}, options );
var $this = $(this);
var $left = $this.find(settings.leftSelector);
var $right = $this.find(settings.rightSelector);
var $viewport = $this.find(settings.viewportSelector);
var $content = $this.find(settings.contentSelector);
var $items = $this.find(settings.itemsSelector);
var $lastItem = $($items[$items.length-1]);
var firstShownItem = 0;
var overflowWidth = $content.outerWidth() - $viewport.innerWidth();
var lastItemRightMargin = parseInt($lastItem.css('margin-left'))
var lastFirstShownItem = $items.index($items.filter(function(){
var $item = $(this);
return Math.round($item.position().left + parseInt($item.css('margin-left'))) <= overflowWidth;
}).last());
//last possible firstShownItem
if ( lastFirstShownItem < 0 ) {
lastFirstShownItem = $items.length - 1;
}
var animationInterval;
if( overflowWidth > 0 ) {
animationInterval = setInterval(function(){
scroll(1);
}, settings.animationIntervalTime);
}
$right.add($left).add($content).mouseover(function(){
clearInterval(animationInterval);
}).mouseleave(function(){
clearInterval(animationInterval);
if( overflowWidth > 0 ) {
animationInterval = setInterval(function(){
scroll(1);
}, settings.animationIntervalTime);
}
});
$right.click(function(){
scroll(1);
});
$left.click(function(){
scroll(-1);
});
function scroll(direction) {
if( overflowWidth <= 0 ) {
return;
}
var $firstShownItem = $($items[firstShownItem]);
var $nextItem = $($items[firstShownItem+1]);
if( firstShownItem === -1 ) {
if ( direction >= 0 )
{
$nextItem = $($items[1]);
}
else {
$nextItem = $($items[lastFirstShownItem+1]);
}
// $firstShownItem = $($items[firstShownItem]);
}
else if ( direction >= 0 && firstShownItem >= $items.length - 1 ) {
//cycling last→first
firstShownItem = 0;
}
else if ( direction >= 0 && Math.round($content.outerWidth() - $nextItem.position().left + parseInt($nextItem.css('margin-left'))) >= $viewport.innerWidth() ) {
//one more to the right available
firstShownItem++;
}
else if ( direction < 0 && firstShownItem > 0 ) {
//one more to the left available
firstShownItem--;
}
else if ( direction >= 0 ) {
firstShownItem = 0;
}
else {
firstShownItem = -1;
}
var overflowRight = -1;
if ( firstShownItem === -1 ) {
if ( direction >= 0 )
{
firstShownItem = 0;
}
else {
firstShownItem = lastFirstShownItem;
}
}
else {
overflowRight = $content.outerWidth() - $firstShownItem.position().left - parseInt($firstShownItem.css('margin-left')) - $viewport.innerWidth();
}
var $nextShownItem = $($items[firstShownItem]);
if ( ( overflowRight !== -1 && firstShownItem >= 0 && direction >= 0 &&
( overflowRight < $lastItem.outerWidth() && overflowRight > lastItemRightMargin + settings.itemNotViewdThreshold ) ) ||
( direction < 0 && firstShownItem === -1 ) ) {
//binding right border of the last item to the right border of viewport
offset = ( $content.outerWidth() - $viewport.innerWidth() ) * -1;
firstShownItem = -1;
}
else {
offset = ( $nextShownItem.position().left + parseInt($nextShownItem.css('margin-left')) ) * -1;
}
$content.stop().animate({
left: offset
}, 500, function(){
if (typeof settings.callback === 'function') {
settings.callback.call(this);
}
});
}
});
};
}( jQuery ));