JavaScript 实现带滑动下划线的动画导航菜单

作者:交互手记时间:2026-10-29 14:46:23浏览:115

本文提供一段基于 JavaScript 实现的简单动画导航菜单代码。该效果在菜单项下方添加了一条横线,当鼠标悬停时,横线会自动滑动至当前菜单位置以指示状态。代码包含完整的 HTML、CSS 样式及 JS 交互逻辑,适合需要简洁动态导航效果的前端项目参考。

JavaScript 动画导航菜单实现

本文分享一段使用 JavaScript 编写的动画导航菜单示例代码,供开发者参考借鉴。

这是一个基础的动画导航菜单实现。其核心功能是在每个菜单项下方设置一条横线,当鼠标移入不同菜单项时,该横线会自动滑动到对应位置,从而直观地指示当前选中的菜单项。

具体运行效果如下图所示:

JavaScript 动画导航菜单实现

如需查看在线演示,可访问以下地址:

http://demo.jb51.net/js/2015/js-animate-nav-menu-style-codes/


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>动画导航菜单</title>
<style type="text/css">
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote{margin:0; padding:0;}
body { font:12px "\5B8B\4F53",san-serif; color:#808080; padding-top:6px; }
a { text-decoration:none; color:#808080 }
a:hover { text-decoration:underline; color:#ba2636 }
ul, li { list-style:none; }
#nav{ padding:41px 0 0 0; height:27px;width:542px; overflow:hidden; float:right; margin-right:17px;position:relative ;}
#nav ul{ height:27px; width:100%; background: url(i/naVbotBdr.gif) repeat-x 0 100%; }
#nav ul li{ width:67px; height:24px; float:left; padding-right:28px; display:inline; cursor:pointer; overflow:hidden}
#nav a{background:#999;width:100%; height:24px; display:block; float:left; outline:none; color:#fff; line-height:24px; text-align:center; font-size:16px; font-weight:bold; overflow:hidden}
#navCur{ position:absolute; left:0; bottom:0; height:3px; color:#F00; width:67px; margin:0; padding:0; display: block; float:none; background:#FF0000; cursor:pointer; overflow:hidden}
</style>
</head>
<body>
  <div id="nav">
  <ul class="cf">
  <li><a href="" hidefocus="true">1</a></li>
  <li><a href="1" hidefocus="true">2</a></li>
  <li class="cur"><a href="2" hidefocus="true">3</a></li>
  <li><a href="3" hidefocus="true">4</a></li>
  <li><a href="4" hidefocus="true">5</a></li>
  <li><a href="5" hidefocus="true">6</a></li>
 </ul>
  <span id="navCur"></span>
 </div>
<script type="text/javascript">
(function(){
 var curPostion,thisLeft, curPostion_1;
 var obj = getId('nav').getElementsByTagName('ul')[0], timer = null ,navCur = getId('navCur'),x,liArr = getId('nav').getElementsByTagName('li'),liLength = liArr.length-1;
 for (var x=0;x<liArr.length; x++ ){
  liArr[liLength].style.paddingRight = "0";
  var li = liArr[x],curPostion = navCur.offsetLeft;
  if(li.className == "cur"){ 
   navCur.style.left = li.offsetLeft + "px";
  }
  li.onmouseover=function(){
   clearTimeout(timer);
   thisLeft = this.offsetLeft;
   if (thisLeft > navCur.offsetLeft)
   {
    hover();
   }
   else
   {
    curPostion_1 = this.offsetLeft;
    out();
   }
  };
  li.onmouseout=function(){
   clearTimeout(timer);
   if (curPostion < navCur.offsetLeft)
   {
    curPostion_1 = curPostion;
    out();
   }
   else
   {
    thisLeft = curPostion;
    hover();
   }
  };
 }
 function hover(){
  if (navCur.offsetLeft <= thisLeft)
  {
   var a = Math.max(parseInt((thisLeft - navCur.offsetLeft) / 15), 3);
   navCur.style.left = navCur.offsetLeft + a + "px";
   timer = setTimeout(arguments.callee, 5);
  }
  else
  {
   navCur.style.left = thisLeft + "px";
   clearTimeout(timer);
  }
 }
 function out(){
  if (navCur.offsetLeft >= curPostion_1)
  {
   var a = Math.max(parseInt((navCur.offsetLeft - curPostion_1) / 15), 3);
   navCur.style.left = navCur.offsetLeft - a + "px";
   timer = setTimeout(arguments.callee, 5);
  }
  else
  {
   navCur.style.left = curPostion_1 + "px";
   clearTimeout(timer);
  }
 }
 function getId(id){
  return document.getElementById(id)
 }
})();
</script>
</body>
</html>

希望这段代码能为您的 JavaScript 程序设计提供有价值的参考。

作者最新文章