该程序分为三部分:css部分,html部分,java script部分
第一部分:css
* { padding: 0; margin: 0; border: 0; outline: none; list-style-type: none; } em, i { font-style: normal; } a { color: #666; text-decoration: none; } a:hover { color: greenyellow; } body { color: #666; font: 12px/22px "Simsun", "宋体"; overflow-x: hidden; } /*结束 */ .wrapper { width: 1920px; overflow: hidden; position: absolute; left: 50%; top: 0; margin: 0 0 0 -960px; height:600px; } /*第二部分*/ .topNav { width: 100%; height: 47px; background: url(src/images/navbg.png); z-index: 1; } .topNavList { width: 1000px; height: 47px; margin: 0 auto; } .topNavSub { position: relative; } .topNav a { display: block; float: left; height: 47px; line-height: 47px; color:indianred; font: 16px/47px "Microsoft YaHei", "Simsun", "宋体"; margin: 0 53px 0 13px; padding: 0 7px; } .activeTopNav { background: url(src/images/navItemBg_13.png) 0 -1px; } .activeLeft { width: 8px; height: 51px; position: absolute; top: -1px; background: url(src/images/navItemBg_12.png); display: none; } .activeRight { width: 10px; height: 51px; position: absolute; top: -1px; background: url(src/images/navItemBg_14.png); display: none; } /*第三部分*/ .subNavWrapper { width: 1000px; height: 300px; position: absolute; left: 50%; top: 44px; margin: 0 0 0 -495px; z-index: 1; display: none; } .subNavSet { width: 1000px; height: 300px; margin: 0 auto; position: relative; } .subNav { position: relative; display: none; } .subNav ul { width: 132px; height: 234px; position: absolute; left: 23px; top: 16px; color: #333; font: 14px/30px "Simsun", "宋体"; } .subNav ul a { width: 130px; display: block; padding: 0 0 0 16px; float: left; color: #333; font: 14px/34px "Microsoft YaHei", "Simsun", "宋体"; background: url(src/images/arrowSub.png) left center no-repeat; } .subNav ul a:hover { color: #066; text-decoration: underline; } .sub1 { left: 144px; } .sub2 { left: 168px; } .sub3 { left: 262px; } .sub4 { left: 157px; } .sub5 { left: 263px; } .sub0 .subBgTopLeft, .sub1 .subBgTopLeft { width: 706px; height: 7px; display: block; background: url(src/images/subTop0.png); } .sub3 .subBgTopLeft { width: 706px; height: 7px; display: block; background: url(src/images/subTop3.png); } .sub5 .subBgTopLeft { width: 706px; height: 7px; display: block; background: url(src/images/subTop5.png); } .sub0 .subBgBotLeft, .sub1 .subBgBotLeft, .sub3 .subBgBotLeft, .sub5 .subBgBotLeft { width: 706px; height: 206px; display: block; background: url(src/images/subBot0.png); } .sub2 .subBgTopLeft { width: 832px; height: 7px; display: block; background: url(src/images/subTop2.png); } .sub4 .subBgTopLeft { width: 832px; height: 7px; display: block; background: url(src/images/subTop4.png); } .sub2 .subBgBotLeft, .sub4 .subBgBotLeft { width: 832px; height: 206px; display: block; background: url(src/images/subBot2.png); } .features { position: absolute; left: 195px; top: 19px; } .features i { width: 174px; display: block; float: left; margin: 0 40px; } .features a:hover img { border: 1px solid #448c8c; } .features i img { display: block; float: left; border: 1px solid #d7d8d8; margin-bottom: 12px; } .features i em { width: 174px; display: block; float: left; line-height: 20px; }
第二部分:html
<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div class="wrapper"> <div class="head"> <div class="topNav"> <div class="topNavList"> <a href="#" class="">第一栏目</a> <a href="#" class="">第二栏目</a> <a href="#" class="">第三栏目</a> <a href="#" class="">第四栏目</a> <a href="#" class="">第五栏目</a> <a href="#" class="">第六栏目</a> <div class="topNavSub"> <div class="activeLeft" style="display: none; left: 725px;"></div> <div class="activeRight" style="display: none; left: 811px;"></div> </div> </div> </div> <div class="subNavWrapper" style="display: none;"> <div class="subNavSet"> <div class="subNav sub0" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 132px;"> <li> <a href="#">第一栏目1</a> <a href="#">第一栏目2</a> <a href="#">第一栏目3</a> </li> </ul> <span class="features" style="left: 195px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>净世界,美生活。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>诚信 专业 激情。</strong></em></a></i> </span> </div> <div class="subNav sub1" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 132px;"> <li> <a href="#">第二栏目1</a> <a href="#">第二栏目2</a> <a href="#">第二栏目3</a> </li> </ul> <span class="features" style="left: 195px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>诚信 专业 激情。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>行业的领跑者。</strong></em></a></i> </span> </div> <div class="subNav sub2" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 292px;"> <li> <a href="#">第三栏目1</a> <a href="#">第三栏目2</a> <a href="#">第三栏目3</a> <a href="#">第三栏目4</a> <a href="#">第三栏目5</a> <a href="#">第三栏目6</a> <a href="#">第三栏目7</a> </li> </ul> <span class="features" style="left: 315px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>精雕细刻 巧夺天工。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>精益求精 无微不至。</strong></em></a></i> </span> </div> <div class="subNav sub3" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 132px;"> <li> <a href="#">第四栏目1</a> <a href="#">第四栏目2</a> <a href="#">第四栏目3</a> </li> </ul> <span class="features" style="left: 195px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>高新技术产品的研发和生产。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index2.jpg"><em><strong>提供优质的品牌产品和服务。</strong></em></a></i> </span> </div> <div class="subNav sub4" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 292px;"> <li> <a href="#">第五栏目1</a> <a href="#">第五栏目2</a> <a href="#">第五栏目3</a> <a href="#">第五栏目4</a> <a href="#">第五栏目5</a> <a href="#">第五栏目6</a> <a href="#">第五栏目7</a> </li> </ul> <span class="features" style="left: 315px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>沟通创造价值。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>稳定和可持续发展。</strong></em></a></i> </span> </div> <div class="subNav sub5" style="display: none;"> <span><i class="subBgTopLeft"></i><i class="subBgBotLeft"></i></span> <ul style="width: 132px;"> <li> <a href="#">第六栏目1</a> <a href="#">第六栏目2</a> </li> </ul> <span class="features" style="left: 195px;"> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>xxxxxxxxxxxxx。</strong></em></a></i> <i><a href="#"><img width="177" height="100" src="images/index1.jpg"><em><strong>xxxxxxxxxxx。</strong></em></a></i> </span> </div> </div> </div> </div> </div> <script type="text/javascript" src="src/bootstrap-3.3.7-dist/js/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="src/bootstrap-3.3.7-dist/js/script.js"></script> </body> </html>
第三部分:java script (本人在连接java script时连接如两个javascript文件:1、jquery-1.9.1.min.js不用写直接下载 2、script.js自己写的下面给出代码)
$(document).ready(function() { nav(); }) var idx; var arr_offsetX = [0, 144, 233, 345, 400, 450]; function nav() { $(".topNavList a").hover(function() { idx = $(".topNavList a").index(this); $(".subNavWrapper").show(); $(".activeLeft").show(); $(".activeRight").show(); var offsetXArrow = $(".topNavList a").eq(idx).position().left - 455; var w = $(".topNavList a").eq(idx).width() + 22; $(".topNavList a").eq(idx).addClass("activeTopNav"); $(".activeLeft").css("left", offsetXArrow+'px'); $(".activeRight").css("left", offsetXArrow+w+'px'); $(".subNavSet div").eq(idx).show(); if (idx == 0 || idx == 1 || idx == 3 || idx == 5) { $(".subNavSet .subNav").eq(idx).find("ul").css("width", "132px"); $(".subNavSet .features").eq(idx).css("left", "195px"); } else { $(".subNavSet .subNav").eq(idx).find("ul").css("width", "292px"); $(".subNavSet .features").eq(idx).css("left", "315px"); }; }, function() { $(".subNavWrapper").hide(); $(".activeLeft").hide(); $(".activeRight").hide(); $(".topNavList a").eq(idx).removeClass("activeTopNav"); $(".subNavSet div").eq(idx).hide(); }) $(".subNavSet div").hover(function() { $(".subNavWrapper").show(); $(".topNavList a").eq(idx).addClass("activeTopNav"); $(".activeLeft").show(); $(".activeRight").show(); $(this).show(); }, function() { $(".subNavWrapper").hide(); $(".activeLeft").hide(); $(".topNavList a").eq(idx).removeClass("activeTopNav"); $(".activeRight").hide(); $(this).hide(); }) }
本文有借鉴网上给的代码模板。代码中有些使用到图片我有在CSDN下载上传,
链接地址http://download.csdn.net/detail/qq_35164169/9903538。
初学者记得看自己代码中用到的图片路径,路径不一样会报错。
运行结果图:
学习让我快乐!