var class = { create: function() { return function() { this.initialize.apply(this, arguments); } } } object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; } var transformview = class.create(); transformview.prototype = { //容器对象,滑动对象,切换参数,切换数量 initialize: function(container, slider, parameter, count, options) { if(parameter <= 0 || count <= 0) return; var ocontainer = $2(container), oslider = $2(slider), othis = this; this.index = 0;//当前索引 this._timer = null;//定时器 this._slider = oslider;//滑动对象 this._parameter = parameter;//切换参数 this._count = count || 0;//切换数量 this._target = 0;//目标参数 this.setoptions(options); this.up = !!this.options.up; this.step = math.abs(this.options.step); this.time = math.abs(this.options.time); this.auto = !!this.options.auto; this.pause = math.abs(this.options.pause); this.onstart = this.options.onstart; this.onfinish = this.options.onfinish; ocontainer.style.overflow = "hidden"; ocontainer.style.position = "relative"; oslider.style.position = "absolute"; oslider.style.top = oslider.style.left = 0; }, //设置默认属性 setoptions: function(options) { this.options = {//默认值 up: true,//是否向上(否则向左) step: 5,//滑动变化率 time: 10,//滑动延时 auto: true,//是否自动转换 pause: 3000,//停顿时间(auto为true时有效) onstart: function(){},//开始转换时执行 onfinish: function(){}//完成转换时执行 }; object.extend(this.options, options || {}); }, //开始切换设置 start: function() { if(this.index < 0){ this.index = this._count - 1; } else if (this.index >= this._count){ this.index = 0; } this._target = -1 * this._parameter * this.index; this.onstart(); this.move(); }, //移动 move: function() { cleartimeout(this._timer); var othis = this, style = this.up ? "top" : "left", inow = parseint(this._slider.style[style]) || 0, istep = this.getstep(this._target, inow); if (istep != 0) { this._slider.style[style] = (inow + istep) + "px"; this._timer = settimeout(function(){ othis.move(); }, this.time); } else { this._slider.style[style] = this._target + "px"; this.onfinish(); if (this.auto) { this._timer = settimeout(function(){ othis.index++; othis.start(); }, this.pause); } } }, //获取步长 getstep: function(itarget, inow) { var istep = (itarget - inow) / this.step; if (istep == 0) return 0; if (math.abs(istep) < 1) return (istep > 0 ? 1 : -1); return istep; }, //停止 stop: function(itarget, inow) { cleartimeout(this._timer); this._slider.style[this.up ? "top" : "left"] = this._target + "px"; } };