rotater.js
3.66 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
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
/*
---
script: rotater.js
description: MGFX.Rotater, the base class that provides slides and transitions.
authors: Sean McArthur (http://mcarthurgfx.com)
license: MIT-style license
requires:
core/1.2.4: '*'
more/1.2.4.1: [Fx.Elements]
provides: [MGFX.Rotater]
...
*/
//MGFX.Rotater. Copyright (c) 2008-2010 Sean McArthur <http://mcarthurgfx.com/>, MIT Style License.
var MGFX = MGFX || {};
MGFX.Rotater = new Class({
Implements: [Options, Events],
options: {
slideInterval: 5000,
transitionDuration: 1000,
startIndex: 0,
autoplay: true,
hover:true,
hash: true,
onAutoPlay: $empty,
onRotate: $empty,
onShowSlide: $empty,
onStop: $empty,
onPause: $empty,
onResume: $empty
},
initialize: function(slides,options){
this.setOptions(options);
this.slides = $$(slides);
this.createFx();
this.showSlide(this.options.startIndex);
if(this.slides.length < 2) this.options.autoplay = false;
if(this.options.autoplay) this.autoplay();
return this;
},
createFx: function(){
if (!this.slideFx) this.slideFx = new Fx.Elements(this.slides, {duration: this.options.transitionDuration, link: 'cancel'});
this.slides.each(function(slide){
slide.setStyle('opacity',0);
});
}.protect(),
setupHover: function() {
var _timeLastRotate = new Date(),
_timeLastPause,
_timeTillRotate = this.options.slideInterval,
_resumeDelay;
var onRotate = this._onRotate = function() {
if(this.slideshowInt) {
_timeLastRotate = new Date();
_timeTillRotate = this.options.slideInterval;
}
};
var onMouseEnter = this._onMouseEnter = function() {
this.stop();
_timeLastPause = new Date();
$clear(_resumeDelay);
this.fireEvent('onPause');
}.bind(this);
var onMouseLeave = this._onMouseLeave = function() {
var timePassed = (_timeLastPause - _timeLastRotate);
_timeLastRotate = new Date() - timePassed;
_resumeDelay = (function() {
this.autoplay();
this.rotate();
this.fireEvent('onResume');
}).delay(_timeTillRotate - timePassed, this);
}.bind(this);
this.addEvent('onRotate', onRotate);
this.slides.addEvents({
'mouseenter': onMouseEnter,
'mouseleave': onMouseLeave
});
}.protect(),
removeHover: function() {
this.removeEvent('onRotate', this._onRotate);
this.slides.removeEvents({
'mouseenter': this._onMouseEnter,
'mouseleave': this._onMouseLeave
});
},
showSlide: function(slideIndex){
if(slideIndex == this.currentSlide) return this;
var action = {};
this.slides.each(function(slide, index){
if(index == slideIndex && index != this.currentSlide){ //show
action[index.toString()] = {
opacity: 1
};
} else {
action[index.toString()] = {
opacity:0
};
}
}, this);
this.fireEvent('onShowSlide', slideIndex);
this.currentSlide = slideIndex;
this.slideFx.start(action);
return this;
},
autoplay: function(){
if(this.options.hover) this.setupHover();
this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
this.fireEvent('onAutoPlay');
return this;
},
stop: function(not_pause){
$clear(this.slideshowInt);
this.fireEvent('onStop');
if(not_pause && this.options.hover) this.removeHover();
return this;
},
rotate: function(){
var next = this.getNext();
this.showSlide(next);
this.fireEvent('onRotate', next);
return this;
},
random: function() {
var index = Math.floor(Math.random() * this.slides.length);
index = index == this.currentSlide ? this.getNext() : index;
this.showSlide(index);
this.fireEvent('onRandom', index);
return this;
},
getNext: function() {
var current = this.currentSlide;
return (current+1 >= this.slides.length) ? 0 : current+1
}.protect()
});