2012-11-28 302 views
2

我正在使用一些我在下拉菜单中找到的代码。它适用于Firefox和Chrome,但不适用于IE。在IE中,子菜单项被移到右侧,而不是直接出现在主菜单项下面。 (以我的代码为例,“Level 2.1”,“Level 2.2”等我悬停时并不直接在“Two Levels”之下,它们右移了。天,我对编码不太了解,尽管我已经为解决方案做了大量的研究工作,但我不明白,也不太清楚。我希望有人能帮助我,否则我只是。要放弃CSS下拉菜单将子菜单项移到右侧,但只在ie

我想离开DOCTYPE和meta标签作为是

我的文档类型是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

我有meta标签:

<meta content='IE=EmulateIE7' http-equiv='X-UA-Compatible'/> 

这里是CSS:

#nav { 
float: left; 
font: bold 12px Arial, Helvetica, Sans-serif; 
border: 1px solid #121314; 
border-top: 1px solid #2b2e30; 
overflow: hidden; 
width: 100%; 
background: #3C4042; 
background: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(59,63,65)), color-stop(0.55, rgb(72,76,77)), color-stop(0.78, rgb(75,77,77))); 
background: -moz-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
background: -o-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1) inset, 0 0 5px rgba(0, 0, 0, 0.1) inset; 
} 

#nav ul { 
margin: 0; 
padding: 0; 
list-style: none; 
} 

#nav ul li { 
float: left; 
} 

#nav ul li a { 
float: left; 
color: #d4d4d4; 
padding: 10px 20px; 
text-decoration: none; 
background: #3C4042; 
background: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(59,63,65)), color-stop(0.55, rgb(72,76,77)), color-stop(0.78, rgb(75,77,77))); 
background: -moz-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
background: -o-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1) inset, 0 0 5px rgba(0, 0, 0, 0.1) inset; 
border-left: 1px solid rgba(255, 255, 255, 0.05); 
border-right: 1px solid rgba(0,0,0,0.2); 
text-shadow: 0 -1px 1px rgba(0, 0, 0, 0.6); 
} 
/* Drop Down Menu By helperblogger.com */ 
#nav ul li a:hover, 
#nav ul li:hover > a { 
color: #252525; 
background: #3C4042; 
background: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(77,79,79)), color-stop(0.55, rgb(67,70,71)), color-stop(0.78, rgb(69,70,71))); 
background: -moz-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
background: -o-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.2), 0 -1px #000; 
} 

#nav li ul a:hover, 
#nav ul li li:hover > a { 
color: #2c2c2c; 
background: #5C9ACD; 
background: -webkit-gradient(linear, left bottom, left top, color-stop(0.17, rgb(61,111,177)), color-stop(0.51, rgb(80,136,199)), color-stop(1, rgb(92,154,205))); 
background: -moz-linear-gradient(center bottom, rgb(61,111,177) 17%, rgb(80,136,199) 51%, rgb(92,154,205) 100%); 
background: -o-linear-gradient(center bottom, rgb(61,111,177) 17%, rgb(80,136,199) 51%, rgb(92,154,205) 100%); 
border-bottom: 1px solid rgba(0,0,0,0.6); 
border-top: 1px solid #7BAED9; 
text-shadow: 0 1px rgba(255, 255, 255, 0.3); 
} 
/* Drop Down Menu By helperblogger.com */ 
#nav li ul { 
background: #3C4042; 
background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(77,79,79)), color-stop(0.55, rgb(67,70,71)), color-stop(0.78, rgb(69,70,71))); 
background-image: -moz-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
background-image: -o-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
left: -999em; 
margin: 35px 0 0; 
position: absolute; 
width: 160px; 
z-index: 9999; 
box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
-moz-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
-webkit-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
border: 1px solid rgba(0, 0, 0, 0.5); 
} 

#nav li:hover ul { 
left: auto; 
} 

#nav li ul a { 
background: none; 
border: 0 none; 
margin-right: 0; 
width: 120px; 
box-shadow: none; 
-moz-box-shadow: none; 
-webkit-box-shadow: none; 
border-bottom: 1px solid transparent; 
border-top: 1px solid transparent; 
} 

.nav ul li ul { 
    position: absolute; 
    left: 0; 
    display: none; 
    visibility: hidden; 
} 

.nav ul li ul li { 
    display: list-item; 
    float: none; 
} 

.nav ul li ul li ul { 
    top: 0; 
} 

.nav ul li ul li a { 
    font: normal 13px Verdana; 
    width: 160px; 
    padding: 5px; 
    margin: 0; 
    border-top-width: 0; 
    border-bottom: 1px solid gray; 
} 

#nav li li ul { 
margin: -1px 0 0 160px; 
visibility: hidden; 
} 

#nav li li:hover ul { 
visibility: visible; 
} 

这里是HTML:

<div id="nav"> 
<ul> 
    <li><a href="#">Home</a></li> 
    <li><a href="#">About Us</a></li> 
    <li><a href="#">Our Portfolio</a></li> 
    <li><a href="#">One Dropdown</a></li> 
    <li><a href="#">Two Levels</a> 
    <ul> 
    <li><a href="#">Level 2.1</a></li> 
    <li><a href="#">Level 2.2</a></li> 
    <li><a href="#">Level 2.3</a></li> 
    <li><a href="#">Level 2.4</a></li> 
    <li><a href="#">Level 2.5</a></li> 
    </ul> 
    </li> 
    <li><a href="#">Services</a></li> 
    <li><a href="#">Contact Us</a></li> 
</ul> 
</div> 

任何帮助我非常感谢。谢谢。在你的CSS

+0

下次在jsfiddle上发布你的代码请:http://jsfiddle.net/faywk/ – xiaoyi

+0

对不起,下次我会试着去做。 –

回答

0

好,花直上这5天,没有得到它的工作后,和几个小时的研究,我终于找到了一个很好的教程,解释了如何创建一个CSS下拉菜单,它适用于所有浏览器,包括IE 7,8和9。使用该代码并进行修改以适合我的偏好。

谢谢@Prasad和@Xiaoyi的帮助。

如果这可能对别人有帮助(可能只有像我这样的初学者),这里是链接。有一个小修正加入了z-index,所以如果你的菜单下拉菜单被其他元素所覆盖,请确保你阅读了所有的评论。我永远不希望任何人通过我所经历的事情。 http://designmodo.com/css3-dropdown-menu/

0

补充以下变化

#nav { 
float: left; 
font: bold 12px Arial, Helvetica, Sans-serif; 
border: 1px solid #121314; 
border-top: 1px solid #2b2e30; 
overflow: hidden; 
width: 100%; 
background: #3C4042; 
background: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(59,63,65)), color-stop(0.55, rgb(72,76,77)), color-stop(0.78, rgb(75,77,77))); 
background: -moz-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
background: -o-linear-gradient(center bottom, rgb(59,63,65) 9%, rgb(72,76,77) 55%, rgb(75,77,77) 78%); 
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1) inset, 0 0 5px rgba(0, 0, 0, 0.1) inset; 
*height:34px; //IE7 Hack 
} 

#nav li ul { 
background: #3C4042; 
background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0.09, rgb(77,79,79)), color-stop(0.55, rgb(67,70,71)), color-stop(0.78, rgb(69,70,71))); 
background-image: -moz-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
background-image: -o-linear-gradient(center bottom, rgb(77,79,79) 9%, rgb(67,70,71) 55%, rgb(69,70,71) 78%); 
left: -999em; 
margin: 35px 0 0; 
position: absolute; 
*position:relative;//DD menu will start after </a> IE7 
width: 160px; 
z-index: 9999; 
box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
-moz-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
-webkit-box-shadow: 0 0 15px rgba(0, 0, 0, 0.4) inset; 
border: 1px solid rgba(0, 0, 0, 0.5); 
} 
+0

谢谢Prasad!但是,这并没有奏效。菜单遍布各处。不知道发生了什么事。我的目标是让浏览器的菜单保持一致。 –

0

简单的解决方法:

#nav ul li { 
    position: relative; 
    float: left; 
} 
#nav li:hover ul { 
    left: 0; 
} 

演示:http://jsfiddle.net/faywk/2/

+0

谢谢xiayii的帮助!然而,这也没有工作。几乎但不完全。在Firefox和Chrome中,主菜单是可以的,但是子菜单项在页面的左侧完成。在IE中,主菜单不正确。它在一个对角线上,子菜单项目在页面的左侧完成。我看到你的代码在jsfiddle中工作,为什么它不能在浏览器中工作? –

+0

如果它工作在isfiddle这是正确的代码。必须有一些你自己的代码搞砸了。 – xiaoyi

+0

xiaoyi,我打开了另一个问题,包括我在head标签中的所有代码。你可以看看,看看你是否看到问题在哪里? http://stackoverflow.com/questions/13634465/blogger-css-drop-down-menu-shifts-sub-menu-items-to-the-right-only-in-ie谢谢! –