The example in this article describes the stretch and shrink menu code implemented in JavaScript. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:
When the mouse is hovering, the menu items move upward into white text on a blue background. After clicking, a blue bar will appear at the bottom to indicate the currently selected item.
Page code, each item of the menu is a div, including a ul to place the display text, etc., and the other div is the blue bar at the bottom. Different classes need to be set for the first and last items. , the style needs to use:
<div id="nav"> <div class="navItem indexNavItem"> <ul class="navUl"> <li>首页</li> <li class="hoverLi">首页</li> </ul> <div class="highlighter selectedNav"></div> </div> <div class="navItem"> <ul class="navUl"> <li>A</li> <li class="hoverLi">A</li> </ul> <div class="highlighter"></div> </div> <div class="navItem lastNavItem"> <ul class="navUl"> <li>A</li> <li class="hoverLi">A</li> </ul> <div class="highlighter"></div> </div> <div id="logoutNavItem" class="navItem logoutNavItem lastNavItem"> <ul class="navUl"> <li>退出</li> <li class="hoverLi">退出</li> </ul> <div class="highlighter"></div> </div> </div>
Style, mainly the setting of the left and right borders of each menu item and the position setting of ul and li:
* { padding: 0; margin: 0; } body { background-color: #fffff3; font: 12px/1.6em Helvetica, Arial, sans-serif; } ul,li{ list-style: none; } #nav { text-align: center; height: 50px; font-size: 10px; line-height: 30px; background-color: #F0E6DB; margin-bottom: 10px; } .navItem { cursor: pointer; position: relative; float: left; width: 100px; height: 50px; font-size: 15px; border-right: 2px solid rgb(255,255,255); border-left: 2px solid rgb(255,255,255); overflow: hidden; font-weight:bold; } .indexNavItem { border-left: 4px solid rgb(255,255,255); margin-left: 10px; } .lastNavItem { border-right: 4px solid rgb(255,255,255); } .logoutNavItem { float: right; width: 120px; margin-right: 10px; border-left: 4px solid rgb(255,255,255); } .navUl { position: relative; height: 100px; width: 100%; border-bottom: 5px solid rgb(2,159,212); } .navUl li { height: 50px; line-height: 50px; } .highlighter { position: absolute; bottom: 0; height: 5px; width: 100%; } .selectedNav { background-color: #029FD4; } .hoverLi { background-color: #029FD4; color: #ffffff; }
The next step is to write the js code for the hover and click events for the menu. When hovering, move the ul up to the height of the li, and then restore it after the mouse is moved away. After clicking, just add styles to the blue bar div:
$(function() { $(".navItem").hover(function() { $(this).children("ul").animate({ top: "-50px" }, 100); }, function() { $(this).children("ul").animate({ top: "0px" }, 100); }); $(".navItem").click(function(event) { $(this).siblings().children('.highlighter').removeClass('selectedNav'); $(this).children('.highlighter').addClass('selectedNav'); }); })
The above is the key code for jQuery to achieve a simple navigation menu effect. I hope it will be helpful to everyone's learning.