// using more.js // element.measure : line 84 // author: qianyuan // date: 2011/11 // todo: stop using more.js /** * horizontal slider */ var hslider = new class({ implements: [options, events], options: { duration: 1000, // 动画时间 transition: fx.transitions.quart.easeout, // 动画效果 startindex: 0, // 起始索引 circle: true, // 是否循环 autoplay: true, // 是否自动播放 interval: 5000, // 自动播放间隔 indexbtns: '', // 索引 css selector prebtn: '', // 后退按钮 css selector nextbtn: '', // 前进按钮 css selector container: '', // 指定外层容器(父元素) onchange: function(i) {}, // 开始变换时 onshow: function(i) {} // 变换结束时 }, /** * 初始化方法 */ initialize: function(slides, options) { // 处理参数 this.slides = $$(slides); if (!this.slides.length) { return false; } this.setoptions(options); if (this.options.container) { this.container = getelement(this.options.container); } else { this.container = this.slides[0].getparent(); } this.slidesize = mesuresize(this.slides[0]); // 初始化slides并显示起始项 this.initslides(); // 自动播放 if (this.options.autoplay) { this.startautoplay(); } // 绑定事件 this.attach(); }, /** * 初始化slides */ initslides: function() { this.sildeswrapper = new element('div', { styles: { width: ( this.slidesize.x ) * ( this.slides.length ), height: this.slidesize.y, overflow: 'hidden', zoom: '1' }, tween: { duration: this.options.duration, transition: this.options.transition } }); this.slides.each(function(slide) { // need element.measure if (this.slides[0].getcomputedsize) { var width = this.slides[0].getcomputedsize().width; } slide.setstyles({ 'float': 'left', 'width': width }).inject(this.sildeswrapper); }, this); this.container.setstyle('overflow', 'hidden'); this.sildeswrapper.inject(this.container); // 设置初始位置 this.curindex = this.options.startindex; this.sildeswrapper.setstyle('margin-left', - (this.slidesize.x * this.curindex)); this.fireevent('change', [this.curindex]); this.fireevent('show', [this.curindex]); }, /** * 显示第i项 */ showslide: function(itoshow) { this.sildeswrapper.get('tween').start('margin-left', - (this.slidesize.x * itoshow)).chain(function() { this.fireevent('show', [itoshow]); }.bind(this)); if (this.indexbtns) { this.indexbtns[itoshow].addclass('active'); } }, /** * 隐藏第i项 */ hideslide: function(itohide) { // do hide if (this.indexbtns) { this.indexbtns[itohide].removeclass('active'); } }, /** * 切换slide */ changetoslide: function(itochange) { this.hideslide(this.curindex); this.showslide(itochange); this.fireevent('change', [itochange]); this.curindex = itochange; }, /** * 前进 */ cycleforward: function() { if (this.curindex < this.slides.length - 1) { this.changetoslide(this.curindex + 1); } else if (this.options.circle) { this.changetoslide(0); } }, /** * 后退 */ cycleback: function() { if (this.curindex > 0) { this.changetoslide(this.curindex - 1); } else if (this.options.circle) { this.changetoslide(this.slides.length - 1); } }, /** * 开始自动播放 */ startautoplay: function() { this.autoplay = this.cycleforward.periodical(this.options.interval, this); }, /** * 停止自动播放 */ stopautoplay: function() { clearinterval(this.autoplay); }, /** * 绑定事件 */ attach: function() { if (this.options.autoplay) { this.slides.addevents({ 'mouseenter': function() { this.stopautoplay(); }.bind(this), 'mouseleave': function() { this.startautoplay(); }.bind(this) }); } if (this.options.indexbtns) { this.indexbtns = getelements(this.options.indexbtns); this.indexbtns[this.curindex].addclass('active'); this.indexbtns.each(function(indexbtn, i) { indexbtn.addevent('click', function(e) { e.stop(); this.changetoslide(i); if (this.options.autoplay) { this.stopautoplay(); this.startautoplay(); } // for anchor return false; }.bind(this)); }, this); } if (this.options.prebtn) { this.prebtn = document.getelement(this.options.prebtn); this.prebtn.addevent('click', function() { this.cycleback(); if (this.options.autoplay) { this.stopautoplay(); this.startautoplay(); } // for anchor return false; }.bind(this)); } if (this.options.nextbtn) { this.nextbtn = document.getelement(this.options.nextbtn); this.nextbtn.addevent('click', function() { this.cycleforward(); if (this.options.autoplay) { this.stopautoplay(); this.startautoplay(); } // for anchor return false; }.bind(this)); } } }); /** * one-direaction horizontal slider */ var ohslider = new class({ extends: hslider, initialize: function(slides, options) { this.parent(slides, options); }, /** * 切换slide */ changetoslide: function(itochange) { if (this.ignore) return; this.parent(itochange); }, /** * 前进 */ cycleforward: function() { if (this.ignore) return; if (this.curindex < this.slides.length - 1) { this.changetoslide(this.curindex + 1); } else if (this.options.circle) { this.cycletofirst(); } }, /** * 后退 */ cycleback: function() { if (this.ignore) return; if (this.curindex > 0) { this.changetoslide(this.curindex - 1); } else if (this.options.circle) { this.cycletolast(); } }, /** * 向左滚动切换回第一条 */ cycletofirst: function() { var left = this.sildeswrapper.getstyle('margin-left').toint(); var slidewidth = this.slidesize.x.toint(); this.slides[0].inject(this.sildeswrapper); this.sildeswrapper.setstyle('margin-left', left + slidewidth); // fixed the quick click bug this.ignore = true; this.sildeswrapper.get('tween').start('margin-left', left).chain(function() { this.slides[0].inject(this.sildeswrapper, 'top'); this.sildeswrapper.setstyle('margin-left', 0); this.fireevent('show', [0]); this.ignore = false; }.bind(this)); this.fireevent('change', [0]); if (this.indexbtns) { this.indexbtns[this.slides.length - 1].removeclass('active'); this.indexbtns[0].addclass('active'); } this.curindex = 0; } , /** * 向右滚动切换至最后一条 */ cycletolast: function() { var lastindex = this.slides.length - 1; var slidewidth = this.slidesize.x.toint(); this.slides[lastindex].inject(this.sildeswrapper, 'top'); this.sildeswrapper.setstyle('margin-left', -slidewidth); // fixed the quick click bug this.ignore = true; this.sildeswrapper.get('tween').start('margin-left', 0).chain(function() { this.slides[lastindex].inject(this.sildeswrapper); this.sildeswrapper.setstyle('margin-left', - (slidewidth * lastindex)) ; this.fireevent('show', [lastindex]); this.ignore = false; }.bind(this)); this.fireevent('change', [lastindex]); if (this.indexbtns) { this.indexbtns[0].removeclass('active'); this.indexbtns[lastindex].addclass('active'); } this.curindex = lastindex; } }); /** * vertical slider */ var vslider = new class({ extends: hslider, initialize: function(slides, options) { this.parent(slides, options); }, /** * 初始化slides */ initslides: function() { this.sildeswrapper = new element('div', { styles: { width: this.slidesize.x, height: ( this.slidesize.y ) * ( this.slides.length ), overflow: 'hidden', zoom: '1' }, tween: { duration: this.options.duration, transition: this.options.transition } }); this.slides.each(function(slide) { slide.inject(this.sildeswrapper); }, this); this.container.setstyle('overflow', 'hidden'); this.sildeswrapper.inject(this.container); this.curindex = this.options.startindex; this.sildeswrapper.setstyle('margin-top', - (this.slidesize.y * this.curindex)); this.fireevent('change', [this.curindex]); this.fireevent('show', [this.curindex]); }, /** * 显示第i项 */ showslide: function(itoshow) { this.sildeswrapper.get('tween').start('margin-top', - (this.slidesize.y * itoshow)).chain(function() { this.fireevent('show', [itoshow]); }.bind(this)); if (this.indexbtns) { this.indexbtns[itoshow].addclass('active'); } } }); /** * one-direaction vertical slider */ var ovslider = new class({ extends: vslider, initialize: function(slides, options) { this.parent(slides, options); }, /** * 切换slide */ changetoslide: function(itochange) { if (this.ignore) return; this.parent(itochange); }, /** * 前进 */ cycleforward: function() { if (this.ignore) return; if (this.curindex < this.slides.length - 1) { this.changetoslide(this.curindex + 1); } else if (this.options.circle) { this.cycletofirst(); } }, /** * 后退 */ cycleback: function() { if (this.ignore) return; if (this.curindex > 0) { this.changetoslide(this.curindex - 1); } else if (this.options.circle) { this.cycletolast(); } }, /** * 向上滚动切换回第一条 */ cycletofirst: function() { var top = this.sildeswrapper.getstyle('margin-top').toint(); var slideheight = this.slidesize.y.toint(); this.slides[0].inject(this.sildeswrapper); this.sildeswrapper.setstyle('margin-top', top + slideheight); // fixed the quick click bug this.ignore = true; this.sildeswrapper.get('tween').start('margin-top', top).chain(function() { this.slides[0].inject(this.sildeswrapper, 'top'); this.sildeswrapper.setstyle('margin-top', 0); this.fireevent('show', [0]); this.ignore = false; }.bind(this)); this.fireevent('change', [0]); if (this.indexbtns) { this.indexbtns[this.slides.length - 1].removeclass('active'); this.indexbtns[0].addclass('active'); } this.curindex = 0; }, /** * 向下滚动切换至最后一条 */ cycletolast: function() { var lastindex = this.slides.length - 1; var slideheight = this.slidesize.y.toint(); this.slides[lastindex].inject(this.sildeswrapper, 'top'); this.sildeswrapper.setstyle('margin-top', -slideheight); // fixed the quick click bug this.ignore = true; this.sildeswrapper.get('tween').start('margin-top', 0).chain(function() { this.slides[lastindex].inject(this.sildeswrapper); this.sildeswrapper.setstyle('margin-top', - (slideheight * lastindex)) ; this.fireevent('show', [lastindex]); this.ignore = false; }.bind(this)); this.fireevent('change', [lastindex]); if (this.indexbtns) { this.indexbtns[0].removeclass('active'); this.indexbtns[lastindex].addclass('active'); } this.curindex = lastindex; } }); /** * fadeslider */ var fadeslider = new class({ extends: hslider, initialize: function(slides, options) { this.parent(slides, options); }, /** * 初始化slides */ initslides: function() { this.sildeswrapper = new element('div', { styles: { width: this.slidesize.x, height: this.slidesize.y, position: 'relative' } }); this.slides.setstyles({ 'display': 'none', 'position': 'absolute', 'top': 0, 'left': 0, 'opacity': 0 }); this.slides.set('tween', { duration: this.options.duration, transition: this.options.transition }); this.slides.inject(this.sildeswrapper); this.sildeswrapper.inject(this.container); this.curindex = this.options.startindex; this.slides[this.curindex].setstyles({ 'display': 'block', 'opacity': 1 }); this.fireevent('change', [this.curindex]); this.fireevent('show', [this.curindex]); }, /** * 显示第i项 */ showslide: function(itoshow) { this.slides[itoshow].setstyle('display', 'block').get('tween').start('opacity', 1).chain(function() { this.fireevent('show', [itoshow]); }.bind(this)); if (this.indexbtns) { this.indexbtns[itoshow].addclass('active'); } }, /** * 隐藏第i项 */ hideslide: function(itohide) { this.slides[itohide].get('tween').start('opacity', 0).chain(function() { this.slides[itohide].setstyle('display', 'none'); }.bind(this)); if (this.indexbtns) { this.indexbtns[itohide].removeclass('active'); } } }); /** * mover */ var mover = new class({ extends: hslider, options: { duration: 500, // 动画时间 transition: fx.transitions.quart.easeout, // 动画效果 startindex: 0, // 起始索引 viewlength: 1, // 可见的条目数 circle: true, // 是否循环 autoplay: false, // 是否自动播放 interval: 5000, // 自动播放间隔 indexbtns: '', // 索引 css selector prebtn: '', // 后退按钮 css selector nextbtn: '', // 前进按钮 css selector container: '', // 指定外层容器(父元素) onchange: function(i) {}, // 开始变换时 onshow: function(i) {} // 变换结束时 }, initialize: function(slides, options) { // 处理参数 this.slides = $$(slides); if (!this.slides.length) { return false; } this.setoptions(options); this.slidesize = mesuresize(this.slides[0]); this.container = this.slides[0].getparent(); if (this.container.getstyle('width').toint() > (this.slidesize.outerx * this.slides.length)) { if (this.options.prebtn) { document.getelement(this.options.prebtn).setstyle('display', 'none'); } if (this.options.nextbtn) { document.getelement(this.options.nextbtn).setstyle('display', 'none'); } this.active = false; } else { if (this.options.prebtn) { document.getelement(this.options.prebtn).setstyle('display', 'block'); } if (this.options.nextbtn) { document.getelement(this.options.nextbtn).setstyle('display', 'block'); } this.active = true; // 初始化slides并显示起始项 this.initslides(); // 自动播放 if (this.options.autoplay) { this.startautoplay(); } } this.curindex = this.options.startindex; // 绑定事件 this.attach(); }, /** * 初始化slides */ initslides: function() { this.container = this.slides[0].getparent(); this.container.setstyle('width', this.slidesize.outerx * (this.slides.length + 1)); this.container.set('tween', { duration: this.options.duration, transition: this.options.transition }); this.curindex = this.options.startindex; this.slides[this.curindex].addclass('active'); this.container.setstyles({ 'margin-left': - this.slidesize.outerx * this.curindex }); this.fireevent('change', [this.curindex]); this.fireevent('show', [this.curindex]); }, /** * 显示第i项 */ showslide: function(itoshow) { if (!this.active) { return false; } this.container.get('tween').start('margin-left', - this.slidesize.outerx * itoshow).chain(function() { this.fireevent('show', [itoshow]); }.bind(this)); /* this.slides.removeclass('active'); this.slides[itoshow].addclass('active'); if (this.indexbtns) { this.indexbtns[itoshow].addclass('active'); }; */ }, /** * 前进 */ cycleforward: function() { if (!this.active) { return false; } if (this.curindex < this.slides.length - this.options.viewlength) { this.changetoslide(this.curindex + 1); } else if (this.options.circle) { this.changetoslide(0); } }, /** * 后退 */ cycleback: function() { if (!this.active) { return false; } if (this.curindex > 0) { this.changetoslide(this.curindex - 1); } else if (this.options.circle) { this.changetoslide(this.slides.length - this.options.viewlength); } } }); /** * mover_b * 每个slide长度不一样的mover */ var mover_b = new class({ extends: mover, initialize: function(slides, options) { // 处理参数 this.slides = $$(slides); if (!this.slides.length) { return false; } this.setoptions(options); this.slidesize = []; var totalwidth = 0; this.slides.each(function(slide) { var size = mesuresize(slide); this.slidesize.push(size); totalwidth += size.outerx; }, this); this.container = this.slides[0].getparent(); if (this.container.getstyle('width').toint() > totalwidth) { if (this.options.prebtn) { document.getelement(this.options.prebtn).setstyle('display', 'none'); } if (this.options.nextbtn) { document.getelement(this.options.nextbtn).setstyle('display', 'none'); } this.active = false; } else { if (this.options.prebtn) { document.getelement(this.options.prebtn).setstyle('display', 'block'); } if (this.options.nextbtn) { document.getelement(this.options.nextbtn).setstyle('display', 'block'); } this.active = true; this.container.setstyle('width', totalwidth); // 初始化slides并显示起始项 this.initslides(); // 自动播放 if (this.options.autoplay) { this.startautoplay(); } } this.curindex = this.options.startindex; // 绑定事件 this.attach(); }, /** * 初始化slides */ initslides: function() { this.container.set('tween', { duration: this.options.duration, transition: this.options.transition }); this.curindex = this.options.startindex; this.slides[this.curindex].addclass('active'); this.container.setstyles({ 'margin-left': - this.slides[this.curindex].getposition(this.container).x }); this.fireevent('change', [this.curindex]); this.fireevent('show', [this.curindex]); }, /** * 显示第i项 */ showslide: function(itoshow) { if (!this.active) { return false; } var left = this.slides[itoshow].getposition(this.container).x; this.container.get('tween').start('margin-left', - left).chain(function() { this.fireevent('show', [itoshow]); }.bind(this)); }, /** * 前进 */ cycleforward: function() { if (!this.active) { return false; } if (this.curindex < this.slides.length - this.options.viewlength) { this.changetoslide(this.curindex + 1); } else if (this.options.circle) { this.changetoslide(0); } }, /** * 后退 */ cycleback: function() { if (!this.active) { return false; } if (this.curindex > 0) { this.changetoslide(this.curindex - 1); } else if (this.options.circle) { this.changetoslide(this.slides.length - this.options.viewlength); } } }); /** * getelement */ function getelement(obj) { if (typeof(obj) == 'string') { return document.getelement(obj); } else { return document.id(obj); } } /** * getelements */ function getelements(obj) { return $$(obj); } /** * mesuresize */ function mesuresize(elem) { var size = elem.getsize(); size.outerx = size.x + elem.getstyle('margin-left').toint() + elem.getstyle('margin-right').toint(); size.outery = size.y + elem.getstyle('margin-top').toint() + elem.getstyle('margin-bottom').toint(); if (size.x == 0 || size.y == 0) { // clone for mesure var clone = elem.clone(); clone.setstyle('opacity', '0').inject(document.getelement('body')); size = clone.getsize(); size.outerx = size.x + clone.getstyle('margin-left').toint() + clone.getstyle('margin-right').toint(); size.outery = size.y + clone.getstyle('margin-top').toint() + clone.getstyle('margin-bottom').toint(); clone.destroy(); } return size; }