前提:一直想本身写一个以下图的效果,虽然网上也有,可是考虑到不受未来设计图的多样性,本身写一个拓展性和维护性更佳。javascript
html以下:css
<div class="menu"> <ul> <li>tab01</li> <li>tab02</li> <li>tab03</li> <li>tab04</li> <li>tab05</li> </ul> <div class="hover-line"></div> </div>
css以下:(为每个li定义了位移值。)html
/*清全局*/ body{ padding: 200px; background: #ECECEC; font-size: 12px;} *{ margin: 0; padding: 0;} ul,li{ list-style: none;} /*菜单*/ .menu{ position: relative; width: 600px; height:36px;} .menu ul li{ transition: all 0.5s; float:left; width: 100px; height: 36px; line-height: 36px; text-align: center; background: #002A80; color: #fff;} .menu ul li:hover{ background: #008080;} /*线条*/ .hover-line{ transition: all 0.5s; opacity: 0; position: absolute; left: -100px; bottom: 0px; width: 100px; height: 3px; background: greenyellow;} .hover-line-1{ transform: translateX(100px); opacity: 1;} .hover-line-2{ transform: translateX(200px); opacity: 1;} .hover-line-3{ transform: translateX(300px); opacity: 1;} .hover-line-4{ transform: translateX(400px); opacity: 1;} .hover-line-5{ transform: translateX(500px); opacity: 1;}
js以下:(用到了jquery的hover事件。hover就追加位移的样式,out就移除;)java
$(function(){ $(".menu ul li").eq(0).hover( function(){ $(".hover-line").addClass("hover-line-1") }, function(){ $(".hover-line").removeClass("hover-line-1") } ) $(".menu ul li").eq(1).hover( function(){ $(".hover-line").addClass("hover-line-2") } , function(){ $(".hover-line").removeClass("hover-line-2") } ) $(".menu ul li").eq(2).hover( function(){ $(".hover-line").addClass("hover-line-3") }, function(){ $(".hover-line").removeClass("hover-line-3") } ) $(".menu ul li").eq(3).hover( function(){ $(".hover-line").addClass("hover-line-4") }, function(){ $(".hover-line").removeClass("hover-line-4") } ) $(".menu ul li").eq(4).hover( function(){ $(".hover-line").addClass("hover-line-5") }, function(){ $(".hover-line").removeClass("hover-line-5") } ) })
缺点:所有写死,代码过多。jquery
css以下(这一步将每次位移值都去掉了。放到了下面js中。)函数
/*清全局*/ body{ padding: 200px; background: #ECECEC; font-size: 12px;} *{ margin: 0; padding: 0;} ul,li{ list-style: none;} /*菜单*/ .menu{ position: relative; width: 600px; height:36px;} .menu ul li{ transition: all 0.5s; float:left; width: 100px; height: 36px; line-height: 36px; text-align: center; background: #002A80; color: #fff;} .menu ul li:hover{ background: #008080;} /*线条*/ .hover-line{ transition: all 0.5s; opacity: 0; position: absolute; left: -100px; bottom: 0px; width: 100px; height: 3px; background: greenyellow;}
js以下:(这一步的js作了 公共部分的提取,①:hover上的时候,将$(".hover-line").css("opacity",1))优化
这个提出来;②:out时,调用头部的回退函数,这个是每次鼠标一移开,跟随短线会回到起始地方。做为公共也会提取出来了,代码以下;
function go_start(){
$(".hover-line").css({transform:"translateX(-100px)",opacity:0})
}this
以上代码能够必定程度上的减小代码量。同时也是一个对思路的整理过程。spa
$(function(){ //回退函数 function go_start(){ $(".hover-line").css({transform:"translateX(-100px)",opacity:0}) } //事件响应 $(".menu ul li").hover( function(){ var this_index = $(this).index(); //获得索引号 $(".hover-line").css("opacity",1) /*hover后,透明度能够做为公共样式抽到顶部来先执行掉。*/ if(this_index==0) { $(".hover-line").css("transform","translateX(100px)") } if(this_index==1) { $(".hover-line").css("transform","translateX(200px)") } if(this_index==2) { $(".hover-line").css("transform","translateX(300px)") } if(this_index==3) { $(".hover-line").css("transform","translateX(400px)") } if(this_index==4) { $(".hover-line").css("transform","translateX(500px)") } }, function(){ return go_start(); /*回到起点能够做为公共的函数来调用*/ } ) })
$(function(){ //回退函数 function go_start(){ $(".hover-line").css({transform:"translateX(-100px)",opacity:0}) } //事件响应 $(".menu ul li").hover( function(){ var this_index = $(this).index(); //获得索引号 $(".hover-line").css("opacity",1)/*hover后,透明度能够做为公共样式抽到顶部来先执行掉。*/ var this_left_px = (this_index+1)*100; //取到偏移值 if(this_left_px!=="") { $(".hover-line").css("transform","translateX("+this_left_px+"px)") //带进来 } }, function(){ return go_start(); /*回到起点能够做为公共的函数来调用*/ } ) })