-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tickster.js
155 lines (126 loc) · 4.37 KB
/
jquery.tickster.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**************************************************************/
/* */
/* Tickster plugin by Queensbridge */
/* */
/**************************************************************/
(function($) {
// Set default values
var defaults = {
timer : null,
forceAnimation : true,
speed: 10,
direction: 'left',
needsAnimation : false
};
var methods = {
// Init method builds the Tickster
init : function(options) {
var obj = $(this);
var settings = $.extend({}, defaults, options);
if( obj.find('ul:first > li').length == 0 ) { return false; }
obj.addClass('tickster-processed');
obj.find('> ul:first').addClass('tickster-' + settings.direction);
obj.find('ul:first > li').addClass('tickster-original');
obj.tickster('setSize', settings);
obj.find('ul:first > li').live('mouseover', function() { obj.tickster('pause', settings); });
obj.find('ul:first > li').live('mouseout', function() { obj.tickster('play', settings); });
$(window).resize( function() {
obj.tickster('setSize', settings);
});
},
// Adjust number of items depending on available width
setSize : function( settings ) {
var obj = $(this);
var list = obj.find('> ul:first');
var ticksterWidth = obj.width();
var itemsWidth = 0;
var firstItemWidth = list.find('> li:first').outerWidth(true);
var lastItemWidth = list.find('> li:last').outerWidth(true);
list.find('li:not(.tickster-original)').remove();
list.find('> li').each(function() {
itemsWidth += $(this).outerWidth(true);
});
if(
( itemsWidth < ticksterWidth && settings.forceAnimation ) ||
( itemsWidth > ticksterWidth )
) {
settings.needsAnimation = true;
var i = 0;
var l = null;
while( itemsWidth < ( ticksterWidth + firstItemWidth + lastItemWidth ) ) {
l = list.find('li:eq(' + i + ')');
if( l.length == 0 ) {
l = list.find('li:first');
i = 0;
}
itemsWidth += l.outerWidth(true);
l = l.clone();
l.removeClass('tickster-original');
l.appendTo(list);
l = null;
++i;
}
obj.find('ul').width( itemsWidth );
obj.tickster('pause', settings);
obj.tickster('animate', settings);
} else {
settings.needsAnimation = false;
obj.tickster('pause', settings);
list.css(settings.direction, 0);
}
},
// Make it move
animate : function( settings ) {
var obj = $(this);
var list = obj.find('> ul:first');
var firstItem = list.find('> li:first');
var lastItem = list.find('> li:last');
var listPos = list.css(settings.direction);
listPos = parseInt( listPos.substring(0, listPos.length-2) );
// Check if the first list item (or last depending on direction) is out of view and should be put last
var ticksterWidth = obj.width();
var listWidth = list.outerWidth();
var firstItemWidth = firstItem.outerWidth(true);
var lastItemWidth = lastItem.outerWidth(true);
if( ( listPos * -1 ) >= firstItemWidth ) {
var cutLi = firstItem.detach();
cutLi.appendTo(list);
cutLi = null;
listPos = 0;
}
if( settings.direction == 'right' ) {
list.css({ right: ( listPos - 1 ) + 'px' });
} else {
list.css({ left: ( listPos - 1 ) + 'px' });
}
obj.data( 'timer', setTimeout(function() { obj.tickster('animate', settings) }, settings.speed) );
},
// Pause
pause : function( settings ) {
var obj = $(this);
clearInterval( obj.data( 'timer') );
obj.data( 'timer', null)
},
// Play
play : function( settings ) {
var obj = $(this);
if( settings.needsAnimation ) {
obj.tickster('animate', settings);
}
}
}
$.fn.tickster = function( method ) {
var args = arguments;
return this.each(function() {
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( args, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, args );
//return methods.init.apply( this, new Array(method) );
} else {
return false;
}
});
}
})( jQuery );