/** * @version 1.00 * @author 焦质晔 * @update 2014-08-20 */ var slide = function(){ this.init.apply(this, arguments) } slide.prototype = { version: "1.00", author: "jzy", init: function(options){ this.boxid = options.slideboxid; this.awarr = options.slidearrow || []; this.ctrlsid = options.slidectrlsid; this.event = options.slideevent.substr(2); this.movemode = options.movemode; this.autoplay = options.autoplay || false; this.curindex = options.pageindex || 0; this.callback = options.callback || function() {}; this.autoplaytime = 5; this.state = "ready"; var thistemp = this; if (!this.boxid) { throw new error("slidebox不是正确的对象.(slideboxid = \"" + this.slideboxid + "\")"); return } this.sbox = pg._$(this.boxid); this.sarr = pg.getchildnodes(this.sbox); this.swid = pg.width(this.sbox.parentnode); this.shei = pg.height(this.sbox.parentnode); this.len = this.sarr.length; this.setstyle(this.sbox.parentnode, {position: 'relative', overflow: 'hidden'}); this.setstyle(this.sbox, {position: 'absolute', width: this.swid + 'px', height: this.shei + 'px', left: 0, top: 0, overflow: 'hidden'}); this.otarget = {}; switch (this.movemode){ case 'scrollx': this.otarget = {_scroll: 'left', _mode: 'width', _size: this.swid, _mnext: {left : - this.swid}, _mprev: {left : 0}}; break; case 'scrolly': this.otarget = {_scroll: 'top', _mode: 'height', _size: this.shei, _mnext: {top : - this.shei}, _mprev: {top : 0}}; break; default: break; } for (var i = 0;i < this.len;i++){ this.sarr[i].index = i; this.movemode == 'fade' && pg.addclass(this.sarr[i], 'focus_opacity') } pg.addclass(this.sarr[this.curindex], 'current'); if (this.awarr.length){ pg.addevent(pg._$(this.awarr[0]), 'click', function(){ thistemp.exec('prev') }); pg.addevent(pg._$(this.awarr[1]), 'click', function(){ thistemp.exec('next') }); }; if (this.ctrlsid){ this.obtn = pg._$(this.ctrlsid); this.obtnarr = []; for (var i = 0;i < this.len;i++){ var oli = pg.dc('li'); oli.index = i; oli.innerhtml = i + 1; this.obtnarr.push(oli); this.obtn.appendchild(oli); if (this.curindex == i) pg.attr(oli, 'class', 'current'); pg.addevent(oli, this.event, function(){ thistemp.fncut(this.index) }); } } pg.addevent(this.sbox, "mouseover", function(){ thistemp.stop() }); pg.addevent(this.sbox, "mouseout", function(){ thistemp.play() }); if (this.autoplay) { this.play() } }, exec: function(dir){ if (this.state != 'ready') return; var thistemp = this; var prev, next = new number(); this.state = 'stopping'; for (var k = 0;k < this.len;k++){ if (pg.hasclass(this.sarr[k], 'current')){ var cur = k } } if (dir == 'next'){ next = this.curindex + 1 > this.len - 1 ? 0 : this.curindex + 1; this.curindex = this.curindex >= this.len - 1 ? 0 : this.curindex + 1; this.accountpageindex(this.curindex); if (this.movemode != 'fade'){ pg.css(this.sbox, this.otarget._mode, this.otarget._size * 2); pg.css(this.sarr[next], this.otarget._scroll, this.otarget._size); pg.css(this.sarr[next], 'display', 'block'); pg.startmove(this.sbox, this.otarget._mnext, pg.move_type.buffer, function(){ thistemp.resetclass(next); pg.css(thistemp.sbox, thistemp.otarget._scroll, 0); pg.css(thistemp.sbox, thistemp.otarget._mode, thistemp.otarget._size); if (thistemp.callback) thistemp.callback(); thistemp.state = 'ready' }); } else { pg.css(this.sarr[next], 'display', 'block'); pg.startmove(this.sarr[next], {opacity : 100}, pg.move_type.buffer); pg.startmove(this.sarr[cur], {opacity : 0}, pg.move_type.buffer, function(){ thistemp.resetclass(next); if (thistemp.callback) thistemp.callback(); thistemp.state = 'ready' }); } } else { prev = this.curindex - 1 < 0 ? this.len - 1 : this.curindex - 1; this.curindex = this.curindex <= 0 ? this.len - 1 : this.curindex - 1; this.accountpageindex(this.curindex); if (this.movemode != 'fade'){ pg.css(this.sbox, this.otarget._mode, this.otarget._size * 2); pg.css(this.sbox, this.otarget._scroll, - this.otarget._size); pg.css(this.sarr[prev], this.otarget._scroll, 0); pg.css(this.sarr[prev], 'display', 'block'); pg.css(this.sarr[cur], this.otarget._scroll, this.otarget._size); pg.startmove(this.sbox, this.otarget._mprev, pg.move_type.buffer, function(){ thistemp.resetclass(prev); pg.css(thistemp.sbox, thistemp.otarget._mode, thistemp.otarget._size); if (thistemp.callback) thistemp.callback(); thistemp.state = 'ready' }); } else { pg.css(this.sarr[prev], 'display', 'block'); pg.startmove(this.sarr[prev], {opacity : 100}, pg.move_type.buffer); pg.startmove(this.sarr[cur], {opacity : 0}, pg.move_type.buffer, function(){ thistemp.resetclass(prev); if (thistemp.callback) thistemp.callback(); thistemp.state = 'ready' }); } } }, accountpageindex: function(index){ if (!this.ctrlsid) return; for (var i = 0;i < this.len;i++) { pg.removeclass(this.obtnarr[i], 'current'); } pg.addclass(this.obtnarr[index], 'current'); }, resetclass: function(index){ for (var i = 0;i < this.len;i++){ pg.removeclass(this.sarr[i], 'current'); pg.attr(this.sarr[i], 'style', ''); } pg.addclass(this.sarr[index], 'current'); }, fncut: function(index){ if (this.state != 'ready') return; for (var i = 0;i < this.obtnarr.length;i++){ if (pg.hasclass(this.obtnarr[i], 'current')){ var curindex = i } } if (index > curindex){ this.curindex = index - 1; this.exec('next') } else { this.curindex = index + 1; this.exec('prev') } }, play: function(){ var thistemp = this; if (!this.autoplay) return; this._timer = setinterval(function(){ thistemp.exec('next'); }, this.autoplaytime * 1000) }, stop: function(){ if (!this.autoplay) return; clearinterval(this._timer); }, setstyle: function(obj, json) { var attr=""; for(attr in json) { obj.style[attr]=json[attr] } } }