var getid = function (id) { return "string" == typeof id ? document.getelementbyid(id) : id; }; 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 = getid(container), oslider = getid(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: 7,//滑动变化率 time: 35,//滑动延时 auto: true,//是否自动转换 pause: 5000,//停顿时间(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"; } }; window.onload=function(){ function each(list, fun){ for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); } }; ////////////////////////test2 var objs2 = getid("idnum12").getelementsbytagname("li"); var tv2 = new transformview("idtransformview2", "idslider2", 1000, banner_num, { onstart: function(){ each(objs2, function(o, i){ o.classname = tv2.index == i ? "on" : ""; }) },//按钮样式 up: false }); tv2.start(); each(objs2, function(o, i){ o.onmouseover = function(){ o.classname = "on"; tv2.auto = false; tv2.index = i; tv2.start(); } o.onmouseout = function(){ o.classname = ""; tv2.auto = true; tv2.start(); } }) //getid("idstop1").onclick = function(){ tv2.auto = false; tv2.stop(); } //getid("idstart1").onclick = function(){ tv2.auto = true; tv2.start(); } //getid("idnext1").onclick = function(){ tv2.index++; tv2.start(); } //getid("idpre1").onclick = function(){ tv2.index--;tv2.start(); } //getid("idfast1").onclick = function(){ if(--tv2.step <= 0){tv2.step = 1;} } // getid("idslow1").onclick = function(){ if(++tv2.step >= 10){tv2.step = 10;} } // getid("idreduce1").onclick = function(){ tv2.pause-=1000; if(tv2.pause <= 0){tv2.pause = 0;} } // getid("idadd1").onclick = function(){ tv2.pause+=1000; if(tv2.pause >= 5000){tv2.pause = 5000;} } /* getid("idreset1").onclick = function(){ tv2.step = math.abs(tv2.options.step); tv2.time = math.abs(tv2.options.time); tv2.auto = !!tv2.options.auto; tv2.pause = math.abs(tv2.options.pause); } */ }