1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>百度导航栏</title> <style type="text/css"> *{ margin: 0; padding: 0; } .root{ width: 100%; height: 40px; background-color: darkblue; margin-top: 50px; } .root ul{ list-style: none; width: 900px; margin: 0 auto; height: 40px; line-height: 40px; color: white; font-weight: bold; position: relative; } .root ul li{ float: left; text-align: center; width: 65px; position: relative; z-index: 1; } .root ul li:first-of-type{ background-color: darkred; } .slide{ width: 65px; height: 40px; background-color: darkred; position: absolute; left: 0px; top: 0; } </style> </head> <body> <div class="root"> <ul> <li>首页</li> <li>国内</li> <li>国际</li> <li>军事</li> <li>财经</li> <li>娱乐</li> <li>体育</li> <li>互联网</li> <li>科技</li> <li>游戏</li> <li>女人</li> <li>汽车</li> <li>房产</li> <div class="slide"></div> </ul> </div> </body> </html> <script src="js/jquery-3.5.1.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var index = 0; $(".root ul li").click(function(){ $(this).css("background-color","darkred").siblings("li").css("background-color","transparent"); index = $(this).index(); }) $(".root ul li").mouseenter(function(){ $(".slide").stop().animate({ left: $(this).index() * $(this).innerWidth(), },300,"linear") }) $(".root ul").mouseleave(function(){ $(".slide").stop().animate({ left:index * $("li").innerWidth(), }) }) </script>
|