欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现带缓动动画的导航栏效果

程序员文章站 2022-06-29 20:06:44
 话不多说,请看实例代码: ...

 话不多说,请看实例代码:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <style type="text/css">
  ul,li { padding: 0; margin: 0; }
  li { list-style: none; }
   #box{
   height: 48px;
   width: 900px;
   background-color: #459df5;
   margin: 50px auto;
   position: relative;
   }
   #caution {
   width: 150px;
   height: 48px;
   background-color: #4b92d8;
   position: absolute;
   top: 0;
   left: 0;
   }
 #list {
 position: absolute;
 }
   #list li {
   width: 150px;
   height: 48px;
   float: left;
   color: #fff;
   font: 500 20px/48px "微软雅黑";
   text-align: center;
   cursor: pointer;
   }
  </style>
  <script type="text/javascript">
   window.onload = function(){
   //获取ul id
   var oul = document.getelementbyid("list");
   // 获取span id
   var caution = document.getelementbyid("caution");
   //获取ul 所有孩子
   var ali = oul.children;
   //定时器管理
   var timer = null;
   //缓动
   var leader = 0;
   var target = 0;
    //停留
    var current = 0;
   for(var i = 0;i<ali.length;i++){
     //鼠标移动
   ali[i].onmouseover = function(){
      //关闭定时器
   clearinterval(timer);
      //获取当前的left值
   target = this.offsetleft;
      //开启定时器
   timer = setinterval(autocaution,20);   
   }
     //鼠标点击
     ali[i].onmousedown = function(){
      //清楚所有高亮的文字
      for(var j = 0;j<ali.length;j++){
       ali[j].style.color = "#fff"; 
      }
      //当前点击栏变为红色
      this.style.color = "#ccc";
      //提示框
      current = this.offsetleft;
      caution.style.left = current+"px";      
     }
   }
    //鼠标离开
   oul.onmouseout = function(){
     //关闭定时器
     clearinterval(timer);
     target = current;
     timer = setinterval(autocaution,20);
   }
   //缓动动画
   function autocaution(){   
   leader = leader +(target - leader) /10;
   caution.style.left = leader+"px";
   }
   }
  </script>
 </head>
 <body>
  <div id="box">
  <span id="caution"></span>
  <ul id="list">
  <li>首页</li>
  <li>公司简介</li>
  <li>企业文化</li>
  <li>招贤纳士</li>
  <li>企业论坛</li>
  </ul>
  </div>
 </body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!