Tab选项卡点击 滑动效果js实现
html部分代码:
[html]
css部分代码:
*{ margin: 0; padding:0; list-style: none; font-size: 12px; } .notice{ width: 298px; height: 98px; margin: 10px; border: 1px solid #eee; overflow: hidden; } .notice-tit{ height: 27px; position: relative; background-color: #f7f7f7; } .notice-tit ul{ position: absolute; width: 301px; left: -1px; } .notice-tit ul li{ float: left; width: 58px; height: 26px; text-align: center; line-height: 26px; overflow: hidden; padding: 0 1px; background: #f7f7f7; border-bottom: 1px solid #eee; } .notice-tit ul li.select{ background: #fff; border-bottom-color: #fff; border-left: 1px solid #eee; border-right: 1px solid #eee; padding: 0; font-weight: bolder; } .notice li a:link,.notice-tit li a:visited{ text-decoration: none; color: #000; } .notice li a:hover{ color: #f90; } .notice-con .mod{ margin: 10px 10px 10px 19px; } .notice-con .mod ul li{ float: left; width: 134px; height: 25; overflow: hidden; }
js部分代码:
function $(id){ return typeof id==='string'?document.getElementById(id):id; } window.onload = function() { //获取鼠标滑过或点击的标签和要切换内容的元素 var titles = $('notice-tit').getElementsByTagName('li'); var divs =$('notice-con').getElementsByTagName('div'); // alert(divs.length); if(titles.length!=divs.length) return; //遍历titles下的所有li for(var i=0;i<titles.length;i++) { titles[i].id = i; titles[i].onmouseover = function(){ for(var j=0;j<titles.length;j++) { //清除所有li上的class titles[j].className=''; divs[j].style.display = 'none'; } //设置当前为高亮显示 this.className = 'select'; divs[this.id].style.display = 'block'; } } }
效果演示:
越努力越幸运