关于选项卡大家一定不会陌生,应用非常的频繁,通常选项卡都是需要点击或者划过才能够实现切换。
代码实例如下:
<html> <head> <meta charset=" utf-8"> <title>tab切换</title> <style type="text/css"> body,h2,p{ margin:0px; padding:0px; }ul,li{ margin:0px; padding:0px; float:left; list-style-type:none; } body{font-size:12px;} .box{ width:722px; height:99px; margin:10px auto; border:1px solid #dedede; } .list{ width:711px; height:22px; float:left; padding:4px 0 0 9px; border-top:1px solid #fff; border-left:1px solid #fff; border-right:1px solid #fff; } .list li{ width:74px; height:22px; float:left; cursor:pointer; color:#656565; line-height:22px; text-align:center; } .list li.hove{ width:72px; height:20px; color:#fc6703; line-height:20px; border-top:1px solid #dedede; border-left:1px solid #dedede; border-right:1px solid #dedede; border-bottom:1px solid #fff; background:#fff; } .content{ width:722px; height:72px; float:left; display:none; } </style> <script> function $(id){ return typeof id === "string" ? document.getElementById(id) : id; } function $$(oParent, elem){ return (oParent || document).getElementsByTagName(elem); } function $$$(oParent, sClass){ var aElem = $$(oParent, '*'); var aClass = []; var index = 0; for(index=0;index<aElem.length;index++){ if(aElem[index].className == sClass){ aClass.push(aElem[index]); } } return aClass; } function addEvent(oElm, strEvent, fuc) { addEventListener?oElm.addEventListener(strEvent,fuc,false):oElm.attachEvent('on'+strEvent,fuc); }; function Tab(){ this.initialize.apply(this, arguments); } Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; }; Tab.prototype = { initialize : function(obj, dis, content, onEnd, eq){ this.obj = $(obj); this.oList = $$$(this.obj, 'list')[0]; this.aCont = $$$(this.obj, content); this.oUl = $$(this.oList, 'ul')[0]; this.aLi = this.oUl.children; this.timer = null; eq ? (this.aLi.length < eq ? eq = 0 : eq) : eq = 0; this.oEve(onEnd); this.onEnd.method == 'mouseover' ? this.method = "mouseover" : this.method = "click"; this.onEnd.autoplay == 'stop' ? this.autoplay = "stop" : this.autoplay = "play"; this.aCont[eq].style.display = 'block'; this.aLi[eq].className = 'hove'; this.display(dis); this.autoPlayTab(eq, dis); }, oEve: function(onEnd){ this.onEnd = { method: 'mouseover', autoplay: 'stop', }; Object.extend(this.onEnd, onEnd || {}); }, display : function(dis){ var _this = this; var index = iNow = 0; for(index=0;index<_this.aLi.length;index++){ (function(){ var j = index; addEvent(_this.aLi[j], _this.method, function() { _this.fnClick(j,dis); _this.autoPlayTab(j, dis); }) })() } }, autoPlayTab : function(iNow, dis){ if(this.autoplay == 'play'){ var _this = this; this.iNow = iNow; this.obj.onmouseover = function(){ clearInterval(_this.timer); }; this.obj.onmouseout = function(){ _this.timer = setInterval(playTab,5000); }; clearInterval(_this.timer); _this.timer = setInterval(playTab,5000); function playTab(){ if(_this.iNow == _this.aLi.length)_this.iNow = 0; _this.fnClick(_this.iNow, dis) _this.iNow++ } } }, fnClick : function(oBtn, dis){ var index = 0; for(index=0;index<this.aLi.length;index++){ this.aLi[index].className = ''; this.aCont[index].style.display = 'none'; } this.aLi[oBtn].className = dis; this.aCont[oBtn].style.display = 'block'; } }; window.onload = function(){ new Tab("box", 'hove', 'content', { method : 'mouseover', autoplay : 'play' },0); }; </script> </head> <body> <div> <div> <ul> <li>div教程</li> <li>jquery教程</li> <li>css教程</li> </ul> </div> <div>蚂蚁部落欢迎您</div> <div>本站url地址是softwhy.com</div> <div>只有努力才会有美好的未来</div> </div> </body> </html>
上面的代码实现了我们的要求,下面介绍一下它的实现过程。
(1)模拟实现jQuery中的id选择器,参数是元素的id属性值
function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}
(2).function $$(oParent, elem){
return (oParent || document).getElementsByTagName(elem);
},此函数实现了后去指定元素下所有特定元素的对象集合。
(3).此函数的功能是将oParent元素下所有class属性值为sClass的元素存入数组
function $$$(oParent, sClass){ var aElem = $$(oParent, '*'); var aClass = []; var index = 0; for(index=0;index<aElem.length;index++){ if(aElem[index].className == sClass){ aClass.push(aElem[index]); } } return aClass; }
(4)事件处理函数的绑定封装,实现了浏览器兼容功能。
.function addEvent(oElm, strEvent, fuc) { addEventListener?oElm.addEventListener(strEvent,fuc,false) : oElm.attachEvent('on'+strEvent,fuc); }
(5).此方法实现了基本的初始化操作
function Tab(){ this.initialize.apply(this, arguments); }
(6).实现了合并对象的功能,比如可以将对象a中的属性合并到对象b中
Object.extend = function(destination, source) { for (var property in source) { destination[property] = source[property]; } return destination; }