<script type="text/javascript">
<!--
function Tab(callback, active, interval, direction, auto){
this.active = active||null;
this.list = [];
this.timer = 0;
this.interval = interval || 2000;
this.direction = direction || "right";
this.auto = auto || false;
this.callback = callback;
}
function addEvent(o, type, fn)
{
var func = function(o){
return function()
{
return fn.apply(o, arguments);
}
}(o);
if(o.attachEvent)
{
o.attachEvent("on" + type, func);
}
else if(o.addEventListener)
{
o.addEventListener(type, func, false);
}else{
o["on" + type] = func;
}
return func;
}
Tab.prototype.push = function(elem, type){
type = type || "click";
this.list.push(elem);
var _this = this;
addEvent(elem, type, this.attachEvent.call(this));
}
Tab.prototype.attachEvent = function(){
var _this = this;
return function(){
_this.stop();
if(_this.active == this) return false;
for(var i = 0, l = _this.list.length; i < l; i++){
if(this == _this.list[i]){
_this.execute(this);
break;
}
}
return false;
}
}
Tab.prototype.execute = function(elem){
this.callback(elem, this.active);
this.active = elem;
}
Tab.prototype.autorun = function(){
if(this.timer) clearInterval(this.timer);
var _this = this;
this.active = this.active || this.list[0];
this.timer = setInterval(function(){
for(var i = 0, l = _this.list.length; i < l; i++){
if(_this.active == _this.list[i]){
if(_this.direction == "right"){
if(i == l-1)
_this.execute(_this.list[0], _this.callback);
else
_this.execute(_this.list[i+1], _this.callback);
break;
}else{
if(i == 0)
_this.execute(_this.list[l], _this.callback);
else
_this.execute(_this.list[i-1], _this.callback);
break;
}
}
}
}, this.interval);
}
Tab.prototype.stop = function(){
if(this.timer) clearInterval(this.timer);
}
var oLis = document.getElementsByTagName("a");
var o = new Tab(callback, oLis[0],500);
function callback (elem, active){
elem.className = "active";
if(active) active.className = "";
}
for(var i = 0, l = oLis.length; i < l; i++){
o.push(oLis[i]);
}
o.autorun(500);
//-->
</script>
posted on 2009-02-27 11:07
汪杰 阅读(202)
评论(0) 编辑 收藏 引用 所属分类:
javascript