这篇文章主要介绍js怎么实现带缓动动画的导航栏效果,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
实例代码:
<!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>
以上是“js怎么实现带缓动动画的导航栏效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。