1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| ;function getByClass(obj, sclass) { var allEle = obj.getElementsByTagName('*'); var aResult = []; var re = new RegExp('\\b' + sclass + '\\b'); for (var i= 0; i < allEle.length; i++) { if (re.test(allEle[i].className)) { aResult.push(allEle[i]); } } return aResult; } function extend(obj1, obj2) { for(var attr in obj2) { obj1[attr] = obj2[attr]; } } function addEvent(obj, events, fn) { obj.listeners = obj.listeners || {}; obj.listeners[events] = obj.listeners[events] || []; obj.listeners[events].push(fn); if (obj.addEventListener) { obj.addEventListener(events,fn,false); } else { obj.attachEvent('on' + events,fn); } }
function fireEvent(obj, events) { for(var i = 0; i < obj.listeners[events].length; i++) { obj.listeners[events][i](); } } function TabList(id) { this.tab = document.getElementById(id); this.tabTb = getByClass(this.tab, 'tab-tb')[0]; this.tabTbLi = this.tabTb.getElementsByTagName('li'); this.tabTd = getByClass(this.tab, 'tab-td')[0]; this.tabTdLi = this.tabTd.getElementsByTagName('li'); this.iNow = 0; this.settings = { event: 'mouseover', delay: 0, autoplay: false, autotime: 5000, nowSel: 1 } } TabList.prototype.init = function(opt) { var _this = this; extend(this.settings, opt); if (this.settings.autoplay) { this.autoPlay(this.settings.autotime); } for (var i=0; i<this.tabTbLi.length; i++) { this.tabTbLiA = this.tabTbLi[i].getElementsByTagName('a')[0]; this.tabTbLi[i].index = i; this.tabTbLiA.index = i; addEvent(this.tabTbLiA, this.settings.event, function() { var This = this; if (_this.settings.event === 'mouseover' && _this.settings.delay) { _this.delayTimer = setTimeout(function() { _this.tabShow(This); _this.iNow = This.parentNode.index; },_this.settings.delay); } else { _this.tabShow(This); _this.iNow = This.parentNode.index; } }); this.tabTbLiA.onmouseout = function() { clearTimeout(_this.delayTimer); } } this.tabActive(this.tabTbLi[this.settings.nowSel-1].getElementsByTagName('a')[0]); } TabList.prototype.tabShow = function(obj) { for(var i = 0; i<this.tabTbLi.length; i++) { this.tabTbLi[i].className = ''; this.tabTdLi[i].style.display = 'none'; } obj.index = obj.parentNode.index; this.tabActive(obj); } TabList.prototype.tabActive = function(obj) { obj.parentNode.className = 'active'; this.tabTdLi[obj.index].style.display = 'block'; } TabList.prototype.autoPlay = function(time) { var _this = this; this.autofn(time); for (var i = 0; i < this.tabTbLi.length; i++) { this.tabTbLiA = this.tabTbLi[i].getElementsByTagName('a')[0]; this.tabTbLiA.onmouseover = function() { this.iNow = this.parentNode.index; clearInterval(_this.autoTimer); } } this.tabTd.onmouseover = function() { clearInterval(_this.autoTimer); } this.tabTbLiA.onmouseout = this.tabTd.onmouseout = function() { _this.autofn(_this.settings.autotime); } return this; } TabList.prototype.autofn = function(time) { var _this = this; _this.autoTimer = setInterval(function(){ if( _this.iNow === _this.tabTbLi.length-1) { _this.iNow = 0; } else { _this.iNow++; } for (var i = 0; i < _this.tabTbLi.length; i++) { _this.tabTbLi[i].className = ''; _this.tabTdLi[i].style.display = 'none'; } _this.tabTbLi[_this.iNow].className = 'active'; _this.tabTdLi[_this.iNow].style.display = 'block'; }, time); }
|