Skip to content

Commit

Permalink
made hover option betterer
Browse files Browse the repository at this point in the history
if hover is set to true, we keep track of the time of how long a slide has been visible since the user moused over.  After removing the mouse, we continue the paused timer, instead of restarting.
  • Loading branch information
Sean McArthur committed Sep 9, 2009
1 parent 45d2f89 commit 26cf702
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Demos/demo.html
Expand Up @@ -14,7 +14,7 @@
autoplay: true,
transitionDuration:500,
slideInterval:3000,
hover:false
hover:true
});
});
</script>
Expand Down
40 changes: 37 additions & 3 deletions Source/rotater.js
Expand Up @@ -17,6 +17,8 @@ MGFX.Rotater = new Class({
onRotate: $empty,
onShowSlide: $empty,
onStop: $empty,
onPause: $empty,
onResume: $empty
},

initialize: function(slides,options){
Expand All @@ -26,7 +28,7 @@ MGFX.Rotater = new Class({
this.showSlide(this.options.startIndex);
if(this.slides.length < 2) this.options.autoplay = false;
if(this.options.autoplay) this.autoplay();
if(this.options.hover) this.slides.addEvent('mouseenter',function() { this.stop(); }.bind(this));
if(this.options.hover) this.setupHover();
return this;
},

Expand All @@ -37,6 +39,38 @@ MGFX.Rotater = new Class({
});
}.protect(),

setupHover: function() {
var _timeLastRotate = new Date(),
_timeLastPause,
_timeTillRotate = this.options.slideInterval,
_resumeDelay;

this.addEvent('onRotate', function() {
if(this.slideshowInt) {
_timeLastRotate = new Date();
_timeTillRotate = this.options.slideInterval;
}
});
this.slides.addEvent('mouseenter',function() {
this.stop();
_timeLastPause = new Date();
$clear(_resumeDelay);
this.fireEvent('onPause');
}.bind(this));

this.slides.addEvent('mouseleave', function() {
console.log(_timeLastPause - _timeLastRotate);
var timePassed = (_timeLastPause - _timeLastRotate);
_timeLastRotate = new Date() - timePassed;
_resumeDelay = (function() {
this.autoplay();
this.rotate();
this.fireEvent('onResume');
}).delay(_timeTillRotate - timePassed, this);

}.bind(this));
}.protect(),

showSlide: function(slideIndex){
if(slideIndex == this.currentSlide) return this;
var action = {};
Expand Down Expand Up @@ -70,8 +104,8 @@ MGFX.Rotater = new Class({
},

rotate: function(){
current = this.currentSlide;
next = (current+1 >= this.slides.length) ? 0 : current+1;
var current = this.currentSlide;
var next = (current+1 >= this.slides.length) ? 0 : current+1;
this.showSlide(next);
this.fireEvent('onRotate', next);
return this;
Expand Down

0 comments on commit 26cf702

Please sign in to comment.