3 * url http://remysharp.com/tag/marquee
7 $.fn.marquee = function (klass) {
11 // works out the left or right hand reset position, based on scroll
12 // behavior, current direction and new direction
13 function getReset(newDir, marqueeRedux, marqueeState) {
14 var behavior = marqueeState.behavior, width = marqueeState.width, dir = marqueeState.dir;
16 if (behavior == 'alternate') {
17 r = newDir == 1 ? marqueeRedux[marqueeState.widthAxis] - (width*2) : width;
18 } else if (behavior == 'slide') {
20 r = dir == -1 ? marqueeRedux[marqueeState.widthAxis] : width;
22 r = dir == -1 ? marqueeRedux[marqueeState.widthAxis] - (width*2) : 0;
25 r = newDir == -1 ? marqueeRedux[marqueeState.widthAxis] : 0;
30 // single "thread" animation
31 function animateMarquee() {
32 var i = newMarquee.length,
40 marqueeRedux = newMarquee[i];
41 $marqueeRedux = $(marqueeRedux);
42 marqueeState = $marqueeRedux.data('marqueeState');
44 if ($marqueeRedux.data('paused') !== true) {
45 // TODO read scrollamount, dir, behavior, loops and last from data
46 marqueeRedux[marqueeState.axis] += (marqueeState.scrollamount * marqueeState.dir);
48 // only true if it's hit the end
49 hitedge = marqueeState.dir == -1 ? marqueeRedux[marqueeState.axis] <= getReset(marqueeState.dir * -1, marqueeRedux, marqueeState) : marqueeRedux[marqueeState.axis] >= getReset(marqueeState.dir * -1, marqueeRedux, marqueeState);
51 if ((marqueeState.behavior == 'scroll' && marqueeState.last == marqueeRedux[marqueeState.axis]) || (marqueeState.behavior == 'alternate' && hitedge && marqueeState.last != -1) || (marqueeState.behavior == 'slide' && hitedge && marqueeState.last != -1)) {
52 if (marqueeState.behavior == 'alternate') {
53 marqueeState.dir *= -1; // flip
55 marqueeState.last = -1;
57 $marqueeRedux.trigger('stop');
60 if (marqueeState.loops === 0) {
61 if (marqueeState.behavior != 'slide') {
62 marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
64 // corrects the position
65 marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir * -1, marqueeRedux, marqueeState);
68 $marqueeRedux.trigger('end');
70 // keep this marquee going
71 newMarqueeList.push(marqueeRedux);
72 $marqueeRedux.trigger('start');
73 marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
76 newMarqueeList.push(marqueeRedux);
78 marqueeState.last = marqueeRedux[marqueeState.axis];
80 // store updated state only if we ran an animation
81 $marqueeRedux.data('marqueeState', marqueeState);
83 // even though it's paused, keep it in the list
84 newMarqueeList.push(marqueeRedux);
88 newMarquee = newMarqueeList;
90 if (newMarquee.length) {
91 setTimeout(animateMarquee, 25);
95 // TODO consider whether using .html() in the wrapping process could lead to loosing predefined events...
96 this.each(function (i) {
97 var $marquee = $(this),
98 width = $marquee.attr('width') || $marquee.width(),
99 height = $marquee.attr('height') || $marquee.height(),
100 $marqueeRedux = $marquee.after('<div ' + (klass ? 'class="' + klass + '" ' : '') + 'style="display: block-inline; width: ' + width + 'px; height: ' + height + 'px; overflow: hidden;"><div style="float: left; white-space: nowrap;">' + $marquee.html() + '</div></div>').next(),
101 marqueeRedux = $marqueeRedux.get(0),
103 direction = ($marquee.attr('direction') || 'left').toLowerCase(),
105 dir : /down|right/.test(direction) ? -1 : 1,
106 axis : /left|right/.test(direction) ? 'scrollLeft' : 'scrollTop',
107 widthAxis : /left|right/.test(direction) ? 'scrollWidth' : 'scrollHeight',
109 loops : $marquee.attr('loop') || -1,
110 scrollamount : $marquee.attr('scrollamount') || this.scrollAmount || 2,
111 behavior : ($marquee.attr('behavior') || 'scroll').toLowerCase(),
112 width : /left|right/.test(direction) ? width : height
115 // corrects a bug in Firefox - the default loops for slide is -1
116 if ($marquee.attr('loop') == -1 && marqueeState.behavior == 'slide') {
117 marqueeState.loops = 1;
123 if (/left|right/.test(direction)) {
124 $marqueeRedux.find('> div').css('padding', '0 ' + width + 'px');
126 $marqueeRedux.find('> div').css('padding', height + 'px 0');
130 $marqueeRedux.bind('stop', function () {
131 $marqueeRedux.data('paused', true);
132 }).bind('pause', function () {
133 $marqueeRedux.data('paused', true);
134 }).bind('start', function () {
135 $marqueeRedux.data('paused', false);
136 }).bind('unpause', function () {
137 $marqueeRedux.data('paused', false);
138 }).data('marqueeState', marqueeState); // finally: store the state
140 // todo - rerender event allowing us to do an ajax hit and redraw the marquee
142 newMarquee.push(marqueeRedux);
144 marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
145 $marqueeRedux.trigger('start');
147 // on the very last marquee, trigger the animation
153 return $(newMarquee);