纯CSS实现下拉菜单与JS交互示例
作者:页面旅途时间:2026-10-24 03:57:11浏览:561
本文介绍了纯CSS实现下拉菜单的方法,并提供了包含JavaScript交互功能的完整代码示例。文中特别提示,复制代码时建议使用标准文本编辑器以避免因格式问题导致的运行错误,同时强调了引入外部JS后需刷新页面以执行脚本的注意事项。
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>css菜单演示</title> <style type="text/css"> <!-- *{margin:0;padding:0;border:0;} body { font-family: arial, 宋体, serif; font-size:12px; } #nav { line-height: 24px; list-style-type: none; background:#666; } #nav a { display: block; width: 80px; text-align:center; } #nav a:link { color:#666; text-decoration:none; } #nav a:visited { color:#666;text-decoration:none; } #nav a:hover { color:#FFF;text-decoration:none;font-weight:bold; } #nav li { float: left; width: 80px; background:#CCC; } #nav li a:hover{ background:#999; } #nav li ul { line-height: 27px; list-style-type: none;text-align:left; left: -999em; width: 180px; position: absolute; } #nav li ul li{ float: left; width: 180px; background: #F6F6F6; } #nav li ul a{ display: block; width: 156px;text-align:left;padding-left:24px; } #nav li ul a:link { color:#666; text-decoration:none; } #nav li ul a:visited { color:#666;text-decoration:none; } #nav li ul a:hover { color:#F3F3F3;text-decoration:none;font-weight:normal; background:#C00; } #nav li:hover ul { left: auto; } #nav li.sfhover ul { left: auto; } #content { clear: left; } --> </style> </head> <body> <ul id="nav"> <li>产品介绍 <ul> <li>产品一</li> <li>产品一</li> <li>产品一</li> <li>产品一</li> <li>产品一</li> <li>产品一</li> </ul> </li> <li>服务介绍 <ul> <li>服务二</li> <li>服务二</li> <li>服务二</li> <li>服务二服务二</li> <li>服务二服务二服务二</li> <li>服务二</li> </ul> </li> <li>成功案例 <ul> <li>案例三</li> <li>案例</li> <li>案例三案例三</li> <li>案例三案例三案例三</li> </ul> </li> <li>关于我们 <ul> <li>我们四</li> <li>我们四</li> <li>我们四</li> <li>我们四111</li> </ul> </li> <li>在线演示 <ul> <li>演示</li> <li>演示</li> <li>演示</li> <li>演示演示演示</li> <li>演示演示演示</li> <li>演示演示</li> <li>演示演示演示</li> <li>演示演示演示演示演示</li> </ul> </li> <li>联系我们 <ul> <li>联系联系联系联系联系</li> <li>联系联系联系</li> <li>联系</li> <li>联系联系</li> <li>联系联系</li> <li>联系联系联系</li> <li>联系联系联系</li> </ul> </li> </ul> </body> </html>纯CSS下拉菜单与JS交互示例
注意:若需引入外部JavaScript文件,代码执行前必须刷新页面。
纯CSS下拉菜单方案
<!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>老Y天下-网页特效-导航菜单-漂亮的二级下拉菜单</title> <meta http-equiv="content-Type" content="text/html;charset=gb2312"> <!--把下面代码加到<head>与</head>之间--> <style type="text/css"> /* common styling */ /* set up the overall width of the menu div, the font and the margins */ .menu { font-family: arial, sans-serif; width:750px; margin:0; margin:50px 0; } /* remove the bullets and set the margin and padding to zero for the unordered list */ .menu ul { padding:0; margin:0; list-style-type: none; } /* float the list so that the items are in a line and their position relative so that the drop down list will appear in the right place underneath each list item */ .menu ul li { float:left; position:relative; } /* style the links to be 104px wide by 30px high with a top and right border 1px solid white. Set the background color and the font size. */ .menu ul li a, .menu ul li a:visited { display:block; text-align:center; text-decoration:none; width:104px; height:30px; color:#000; border:1px solid #fff; border-width:1px 1px 0 0; background:#c9c9a7; line-height:30px; font-size:11px; } /* make the dropdown ul invisible */ .menu ul li ul { display: none; } /* specific to non IE browsers */ /* set the background and foreground color of the main menu li on hover */ .menu ul li:hover a { color:#fff; background:#b3ab79; } /* make the sub menu ul visible and position it beneath the main menu list item */ .menu ul li:hover ul { display:block; position:absolute; top:31px; left:0; width:105px; } /* style the background and foreground color of the submenu links */ .menu ul li:hover ul li a { display:block; background:#faeec7; color:#000; } /* style the background and forground colors of the links on hover */ .menu ul li:hover ul li a:hover { background:#dfc184; color:#000; } </style> <!--[if lte IE 6]> <style type="text/css"> /* styling specific to Internet Explorer IE5.5 and IE6. Yet to see if IE7 handles li:hover */ /* Get rid of any default table style */ table { border-collapse:collapse; margin:0; padding:0; } /* ignore the link used by 'other browsers' */ .menu ul li a.hide, .menu ul li a:visited.hide { display:none; } /* set the background and foreground color of the main menu link on hover */ .menu ul li a:hover { color:#fff; background:#b3ab79; } /* make the sub menu ul visible and position it beneath the main menu list item */ .menu ul li a:hover ul { display:block; position:absolute; top:32px; left:0; width:105px; } /* style the background and foreground color of the submenu links */ .menu ul li a:hover ul li a { background:#faeec7; color:#000; } /* style the background and forground colors of the links on hover */ .menu ul li a:hover ul li a:hover { background:#dfc184; color:#000; } </style> <![endif]--> </head> <body> <!--把下面代码加到<body>与</body>之间--> <div class="menu"> <ul> <li>DEMOS <!--[if lte IE 6]> DEMOS <table><tr><td> <![endif]--> <ul> <li>zero dollars</li> <li>wrapping text</li> <li>styled form</li> <li>active focus</li> <li>shadow boxing</li> <li>image map</li> <li>fun backgrounds</li> <li>fade scrolling</li> <li>em sized images</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>MENUS <!--[if lte IE 6]> MENUS <table><tr><td> <![endif]--> <ul> <li>spies menu</li> <li>vertical menu</li> <li>enlarging list</li> <li>link images</li> <li>non-rectangular</li> <li>jigsaw links</li> <li>circular links</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>LAYOUTS <!--[if lte IE 6]> LAYOUTS <table><tr><td> <![endif]--> <ul> <li>Fixed 1</li> <li>Fixed 2</li> <li>Fixed 3</li> <li>Fixed 4</li> <li>minimum width</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>BOXES <!--[if lte IE 6]> BOXES <table><tr><td> <![endif]--> <ul> <li>spies menu</li> <li>vertical menu</li> <li>enlarging list</li> <li>link images</li> <li>non-rectangular</li> <li>jigsaw links</li> <li>circular links</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>MOZILLA <!--[if lte IE 6]> MOZILLA <table><tr><td> <![endif]--> <ul> <li>drop down menu</li> <li>cascading menu</li> <li>content:</li> <li>mozzie box</li> <li>rainbow box</li> <li>snooker cue</li> <li>target practise</li> <li>two tone headings</li> <li>shadow text</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>EXPLORER <!--[if lte IE 6]> EXPLORER <table><tr><td> <![endif]--> <ul> <li>example one</li> <li>weft fonts</li> <li>vertical align</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> <li>OPACITY <!--[if lte IE 6]> OPACITY <table><tr><td> <![endif]--> <ul> <li>opaque colours</li> <li>opaque menu</li> <li>partial opacity</li> <li>partial opacity II</li> </ul> <!--[if lte IE 6]> </td></tr></table> <![endif]--> </li> </ul> <!-- clear the floats if required --> <div class="clear"> </div> </div> </body> </html> 注意:若需引入外部JavaScript文件,代码执行前必须刷新页面。
一般情况复制的东西,尽量不要用editplus复制,容易运行错误。
作者最新文章
JS监听滚动事件:回到顶部按钮的显示与隐藏逻辑
2026-11-05 16:19:37
JavaScript怎么读写LocalStorage与SessionStorage
2026-11-05 15:40:11
anime.js 与 GSAP 对比:轻量级项目该怎么选择动画库
2026-11-05 11:24:55
flv.js 与 hls.js 在直播场景下的选型对比
2026-11-05 08:18:52
JavaScript能做什么?前端开发中的作用与应用场景解析
2026-11-04 23:17:54
