var isie = (document.all) ? true : false; var $ = function (id) { return "string" == typeof id ? document.getelementbyid(id) : id; }; var class = { create: function() { return function() { this.initialize.apply(this, arguments); } } } var extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } } var bind = function(object, fun) { return function() { return fun.apply(object, arguments); } } var each = function(list, fun){ for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); } }; //ie only var revealtrans = class.create(); revealtrans.prototype = { initialize: function(container, options) { this._img = document.createelement("img"); this._a = document.createelement("a"); this._timer = null;//计时器 this.index = 0;//显示索引 this._onindex = -1;//当前索引 this.setoptions(options); this.auto = !!this.options.auto; this.pause = math.abs(this.options.pause); this.duration = math.abs(this.options.duration); this.transition = parseint(this.options.transition); this.list = this.options.list; this.onshow = this.options.onshow; //初始化显示区域 this._img.style.width = this._img.style.height = "100%"; this._img.style.border = 0; this._img.onmouseover = bind(this, this.stop); this._img.onmouseout = bind(this, this.start); isie && (this._img.style.filter = "revealtrans()"); this._a.target = "_blank"; $(container).appendchild(this._a).appendchild(this._img); }, //设置默认属性 setoptions: function(options) { this.options = {//默认值 auto: true,//是否自动切换 pause: 1000,//停顿时间(微妙) duration: 1,//变换持续时间(秒) transition: 23,//变换效果(23为随机) list: [],//数据集合,如果这里不设置可以用add方法添加 onshow: function(){}//变换时执行 }; extend(this.options, options || {}); }, start: function() { cleartimeout(this._timer); //如果没有数据就返回 if(!this.list.length) return; //修正index if(this.index < 0 || this.index >= this.list.length){ this.index = 0; } //如果当前索引不是显示索引就设置显示 if(this._onindex != this.index){ this._onindex = this.index; this.show(this.list[this.index]); } //如果要自动切换 if(this.auto){ this._timer = settimeout(bind(this, function(){ this.index++; this.start(); }), this.duration * 1000 + this.pause); } }, //显示 show: function(list) { if(isie){ //设置变换参数 with(this._img.filters.revealtrans){ transition = this.transition; duration = this.duration; apply(); play(); } } //设置图片属性 this._img.src = list.img; this._img.alt = list.text; //设置链接 !!list["url"] ? (this._a.href = list["url"]) : this._a.removeattribute("href"); //附加函数 this.onshow(); }, //添加变换对象 add: function(siimg, stext, surl) { this.list.push({ img: siimg, text: stext, url: surl }); }, //停止 stop: function() { cleartimeout(this._timer); } };