<img src="http://files.jb51.net/file_images/article/201305/201352161501921.gif?201342161524" alt="jQuery弹性滑动导航菜单实现思路及代码_jquery" ><br><div class="codetitle"> <span><a style="CURSOR: pointer" data="44625" class="copybut" id="copybut44625" onclick="doCopy('code44625')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code44625"> <br><!-- 本文参照了该作者的思路:http://runjs.cn/code/ouvvjqka,jquery代码系另外编写 --> <br><!-- 本文出自:http://blog.csdn.net/liuyanzhi08 --> <br><!-- 实现思路: <BR>CSS:用一个绝对定位的div作为动画滑块,改变它的left属性来呈现动画效果 <BR>javascript:hover()触发移动鼠标到导航nav的动作,和移出导航nav的动作(即返回到current导航) <BR>--> <br> <br> <br> <br><meta charset="UTF-8"> <br><meta name="author" content="Nancle"> <br><title>jQuery弹性滑动导航菜单</title> <br><style type="text/css"> <BR>body{ font-family:"Microsoft YaHei";} <BR>#nav_wrapper{ position:relative; width:835px; margin:80px auto; border-bottom:2px #ddd solid;} <BR>#nav_wrapper #nav_menu{ height:50px;} <BR>#nav_wrapper #nav_menu a{ display:block; float:left; height:50px; padding:0 40px; line-height:50px; color:#666; font-size:16px; text-decoration:none;} <BR>#nav_wrapper #nav_animate_block{ position:absolute; bottom:-2px; height:2px; overflow:hidden; background:#80b600;} <BR></style> <br><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <br><script type="text/javascript"> <BR>$(function(){ <BR>var $current_nav = $("#current_nav"); //current元素(当前所在导航项) <BR>var current_nav_width = $current_nav.innerWidth(); <BR>var current_nav_left = $current_nav.position().left; <BR>var $nav_animate_block = $("#nav_animate_block"); //动画滑块 <BR>$nav_animate_block.css({ width:current_nav_width, left:current_nav_left }); //初始状态下,动画滑块位置在current元素 <BR>// 鼠标进入a元素时触发动画事件 <BR>$("#nav_menu a").hover(function(){ <BR>var index = $(this).index(); <BR>var $a_cur = $("#nav_menu").find("a").eq(index);//鼠标移动到的a元素 <BR>//利用触发的a元素索引获取其left位置和它的宽度 <BR>var width = $a_cur.innerWidth(); <BR>var left = $a_cur.position().left; <BR>//设置动画滑块位置 <BR>$nav_animate_block.stop().animate({ <BR>width: width, <BR>left: left <BR>}, 300) <BR>}, function(){ <BR>//鼠标离开a元素时,动画滑块返回current元素位置 <BR>$nav_animate_block.stop().animate({ <BR>width: current_nav_width, <BR>left: current_nav_left <BR>}) <BR>}) <BR>}); <BR></script> <br> <br> <br><div id="nav_wrapper"> <br><div id="nav_menu"> <br><a href="#" id="current_nav">首页</a> <br><a href="#">产品列表</a> <br><a href="#">客户案例</a> <br><a href="#">服务平台</a> <br><a href="#">交流论坛</a> <br><a href="#">关于我们</a> <br> </div> <br><div id="nav_animate_block"></div> <br> </div> <br> <br> <br> </div>