Created
July 23, 2010 22:07
-
-
Save ryanflorence/488108 to your computer and use it in GitHub Desktop.
What it took to port SlideShow to a jQuery moo4q plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/slideshow.js b/slideshow.js | |
| index 44d4e1d..72c21e4 100644 | |
| --- a/slideshow.js | |
| +++ b/slideshow.js | |
| @@ -37,12 +37,14 @@ var SlideShow = new Class({ | |
| autoplay: false | |
| }, | |
| + jQuery: 'slideshow', | |
| + | |
| initialize: function(element, options){ | |
| this.setOptions(options); | |
| this.setLoop(this.showNext, this.options.delay); | |
| - this.element = document.id(element); | |
| - this.slides = this.element.getChildren(); | |
| - this.current = this.slides[0]; | |
| + this.element = jQuery(element); | |
| + this.slides = this.element.children(); | |
| + this.current = jQuery(this.slides[0]); | |
| this.transitioning = false; | |
| this.setup(); | |
| if (this.options.autoplay) this.play(); | |
| @@ -55,26 +57,28 @@ var SlideShow = new Class({ | |
| setupElement: function(){ | |
| var el = this.element; | |
| - if (el.getStyle('position') != 'absolute' && el != document.body) el.setStyle('position','relative'); | |
| + if (el.css('position') != 'absolute' && el != document.body) el.css('position','relative'); | |
| return this; | |
| }, | |
| setupSlides: function(hideFirst){ | |
| var self = this; | |
| - this.slides.each(function(slide, index){ | |
| + this.slides.each(function(index, slide){ | |
| + slide = jQuery(slide); | |
| self.storeTransition(slide).reset(slide); | |
| - if (hideFirst && index != 0) slide.setStyle('display','none'); | |
| - }, this); | |
| + if (hideFirst && index != 0) slide.css('display','none'); | |
| + }); | |
| return this; | |
| }, | |
| storeTransition: function(slide){ | |
| - var classes = slide.get('class'); | |
| + var classes = slide.attr('class'); | |
| var transitionRegex = /transition:[a-zA-Z]+/; | |
| var durationRegex = /duration:[0-9]+/; | |
| - var transition = (classes.match(transitionRegex)) ? classes.match(transitionRegex)[0].split(':')[1].toInt() : this.options.transition; | |
| - var duration = (classes.match(durationRegex)) ? classes.match(durationRegex)[0].split(':')[1] : this.options.duration; | |
| - slide.store('ssTransition', transition).store('ssDuration', duration); | |
| + var transition = (classes.match(transitionRegex)) ? classes.match(transitionRegex)[0].split(':')[1] : this.options.transition; | |
| + var duration = (classes.match(durationRegex)) ? classes.match(durationRegex)[0].split(':')[1].toInt() : this.options.duration; | |
| + slide.data('ssTransition', transition); | |
| + slide.data('ssDuration', duration); | |
| return this; | |
| }, | |
| @@ -85,35 +89,35 @@ var SlideShow = new Class({ | |
| }, | |
| getTransition: function(slide){ | |
| - return slide.retrieve('ssTransition'); | |
| + return slide.data('ssTransition'); | |
| }, | |
| getDuration: function(slide){ | |
| - return slide.retrieve('ssDuration'); | |
| + return slide.data('ssDuration'); | |
| }, | |
| show: function(slide, options){ | |
| - slide = (typeof slide == 'number') ? this.slides[slide] : slide; | |
| - if (slide != this.current && !this.transitioning){ | |
| + slide = (typeof slide == 'number') ? jQuery(this.slides[slide]) : jQuery(slide); | |
| + if (slide[0] != this.current && !this.transitioning){ | |
| this.transitioning = true; | |
| var transition = (options && options.transition) ? options.transition: this.getTransition(slide), | |
| duration = (options && options.duration) ? options.duration: this.getDuration(slide), | |
| - previous = this.current.setStyle('z-index', 1), | |
| + previous = this.current.css('z-index', 1), | |
| next = this.reset(slide); | |
| var slideData = { | |
| previous: { | |
| element: previous, | |
| - index: this.slides.indexOf(previous) | |
| + index: this.slides.index(previous) | |
| }, | |
| next: { | |
| element: next, | |
| - index: this.slides.indexOf(next) | |
| + index: this.slides.index(next) | |
| } | |
| }; | |
| this.fireEvent('show', slideData); | |
| this.transitions[transition](previous, next, duration, this); | |
| (function() { | |
| - previous.setStyle('display','none'); | |
| + previous.css('display','none'); | |
| this.fireEvent('showComplete', slideData); | |
| this.transitioning = false; | |
| }).bind(this).delay(duration); | |
| @@ -123,23 +127,23 @@ var SlideShow = new Class({ | |
| }, | |
| reset: function(slide){ | |
| - return slide.setStyles({ | |
| + return jQuery(slide).css({ | |
| 'position': 'absolute', | |
| 'z-index': 0, | |
| 'display': 'block', | |
| 'left': 0, | |
| 'top': 0 | |
| - }).fade('show'); | |
| + }).show(); | |
| }, | |
| nextSlide: function(){ | |
| - var next = this.current.getNext(); | |
| - return (next) ? next : this.slides[0]; | |
| + var next = this.current.next(); | |
| + return (next.length > 0) ? next : this.slides[0]; | |
| }, | |
| previousSlide: function(){ | |
| - var previous = this.current.getPrevious(); | |
| - return (previous) ? previous : this.slides.getLast(); | |
| + var previous = this.current.prev(); | |
| + return (previous.length > 0) ? previous : this.slides.last(); | |
| }, | |
| showNext: function(options){ | |
| @@ -169,10 +173,6 @@ var SlideShow = new Class({ | |
| this.setLoop(fn, this.options.delay); | |
| this.fireEvent('reverse'); | |
| return this; | |
| - }, | |
| - | |
| - toElement: function(){ | |
| - return this.element; | |
| } | |
| }); | |
| @@ -201,107 +201,112 @@ $extend(SlideShow, SlideShow.adders); | |
| SlideShow.implement(SlideShow.adders); | |
| SlideShow.add('fade', function(previous, next, duration, instance){ | |
| - previous.set('tween',{duration: duration}).fade('out'); | |
| + previous.fadeOut(duration); | |
| return this; | |
| }); | |
| SlideShow.addAllThese([ | |
| ['none', function(previous, next, duration, instance){ | |
| - previous.setStyle('display','none'); | |
| + previous.css('display','none'); | |
| return this; | |
| }], | |
| ['crossFade', function(previous, next, duration, instance){ | |
| - previous.set('tween',{duration: duration}).fade('out'); | |
| - next.set('tween',{duration: duration}).fade('in'); | |
| + previous.fadeOut(duration); | |
| + next.fadeIn(duration); | |
| return this; | |
| }], | |
| - | |
| + | |
| ['fadeThroughBackground', function(previous, next, duration, instance){ | |
| var half = duration/2; | |
| - next.set('tween',{ duration: half }).fade('hide'); | |
| - previous.set('tween',{ | |
| - duration: half, | |
| - onComplete: function(){ | |
| - next.fade('in'); | |
| - } | |
| - }).fade('out'); | |
| + next.hide(); | |
| + previous.fadeOut(half, function(){ | |
| + next.fadeIn(half); | |
| + }); | |
| }], | |
| - | |
| + | |
| ['pushLeft', function(previous, next, duration, instance){ | |
| - var distance = instance.element.getStyle('width').toInt(); | |
| - next.setStyle('left', distance); | |
| - new Fx.Elements([previous,next],{duration: duration}).start({ | |
| - 0: { left: [-distance] }, | |
| - 1: { left: [0] } | |
| + var distance = instance.element.css('width').toInt(); | |
| + next.css('left', distance); | |
| + [next, previous].each(function(slide){ | |
| + var to = slide.css('left').toInt() - distance; | |
| + slide.animate({'left': to}, duration); | |
| }); | |
| return this; | |
| }], | |
| - | |
| - ['pushRight', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('width').toInt(); | |
| - n.setStyle('left', -distance); | |
| - new Fx.Elements([p,n],{duration: d}).start({ | |
| - 0: { left: [distance] }, | |
| - 1: { left: [0] } | |
| + | |
| + ['pushRight', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('width').toInt(); | |
| + next.css('left', -distance); | |
| + [next, previous].each(function(slide){ | |
| + var to = slide.css('left').toInt() + distance; | |
| + slide.animate({'left': to}, duration); | |
| }); | |
| return this; | |
| }], | |
| - | |
| - ['pushUp', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('height').toInt(); | |
| - n.setStyle('top', distance); | |
| - new Fx.Elements([p,n],{duration: d}).start({ | |
| - 0: { top: [-distance] }, | |
| - 1: { top: [0] } | |
| + | |
| + ['pushDown', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('height').toInt(); | |
| + next.css('top', -distance); | |
| + [next, previous].each(function(slide){ | |
| + var to = slide.css('top').toInt() + distance; | |
| + slide.animate({'top': to}, duration); | |
| }); | |
| return this; | |
| }], | |
| - | |
| - ['pushDown', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('height').toInt(); | |
| - n.setStyle('top', -distance); | |
| - new Fx.Elements([p,n],{duration: d}).start({ | |
| - 0: { top: [distance] }, | |
| - 1: { top: [0] } | |
| + | |
| + ['pushUp', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('height').toInt(); | |
| + next.css('top', distance); | |
| + [next, previous].each(function(slide){ | |
| + var to = slide.css('top').toInt() - distance; | |
| + slide.animate({'top': to}, duration); | |
| }); | |
| return this; | |
| }], | |
| - | |
| - ['blindRight', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('width').toInt(); | |
| - n.setStyles({ | |
| - left: -distance, | |
| - 'z-index': 2 | |
| - }).set('tween',{duration: d}).tween('left',0); | |
| + | |
| + ['blindLeft', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('width').toInt(); | |
| + next | |
| + .css({ | |
| + 'left': distance, | |
| + 'z-index': 1 | |
| + }) | |
| + .animate({'left': 0}, duration); | |
| return this; | |
| }], | |
| - ['blindLeft', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('width').toInt(); | |
| - n.setStyles({ | |
| - left: distance, | |
| - 'z-index': 2 | |
| - }).set('tween',{duration: d}).tween('left',0); | |
| + ['blindRight', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('width').toInt(); | |
| + next | |
| + .css({ | |
| + 'left': -distance, | |
| + 'z-index': 1 | |
| + }) | |
| + .animate({'left': 0}, duration); | |
| return this; | |
| }], | |
| - | |
| - ['blindUp', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('height').toInt(); | |
| - n.setStyles({ | |
| - top: distance, | |
| - 'z-index': 2 | |
| - }).set('tween',{duration: d}).tween('top',0); | |
| + | |
| + ['blindUp', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('height').toInt(); | |
| + next | |
| + .css({ | |
| + 'top': distance, | |
| + 'z-index': 1 | |
| + }) | |
| + .animate({'top': 0}, duration); | |
| return this; | |
| }], | |
| - | |
| - ['blindDown', function(p,n,d,i){ | |
| - var distance = i.element.getStyle('height').toInt(); | |
| - n.setStyles({ | |
| - top: -distance, | |
| - 'z-index': 2 | |
| - }).set('tween',{duration: d}).tween('top',0); | |
| + | |
| + ['blindDown', function(previous, next, duration, instance){ | |
| + var distance = instance.element.css('height').toInt(); | |
| + next | |
| + .css({ | |
| + 'top': -distance, | |
| + 'z-index': 1 | |
| + }) | |
| + .animate({'top': 0}, duration); | |
| return this; | |
| }], | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment