有一个位置切换菜单,除了 IE 8/9(不针对任何旧版本),它在任何地方都可以正常工作。我基本上想要一个选择样式的对象,但不想经历自定义样式选择元素的所有麻烦。我正在使用 prepend() 将单击的 li 元素移动到无序列表的顶部。问题是在 IE 8 和 9 中,被移动的 li 似乎仍然认为它被悬停,因此当您将鼠标移离列表时,:hover 样式不会清除。在这些浏览器中,您必须将鼠标移回顶部的 li 元素以清除其 :hover CSS。(maxWidth 部分与动态内容调整大小有关,似乎不是问题。)
HTML:
<div id="locationselector">
<ul>
<li class="current">Home</li>
<li>Banf</li>
<li>Pago Pago</li>
</ul>
</div>
jQuery:
<script type="text/javascript">
$(document).ready(function() {
var maxWidth = Math.max.apply( null, $( '#locationselector li' ).map( function () {
return $( this ).outerWidth( true );
}).get() );
$("#locationselector").width(maxWidth + 37);
$('#locationselector li').click(function(e) {
e.preventDefault();
$('#locationselector li.current').removeClass('current');
$(this).parent().prepend(this);
$(this).addClass('current');
});
});
</script>
CSS:
<style type="text/css">
#locationselector {
background: url(http://zgraphicsdev.com/stackoverflow/location-switch-bg.png) no-repeat;
height: 30px;
display: inline-block;
margin: -10px 5px;
position: relative;
border-radius: 6px;
-webkit-border-radius: 6px;
-webkit-box-shadow: 0px 0px 2px 2px rgba(0, 0, 0, .3);
box-shadow: 0px 0px 2px 2px rgba(0, 0, 0, .3);
}
#locationselector ul {
height: 30px;
margin: 3px 0 0 27px;
padding: 0;
list-style-type: none;
position: absolute;
overflow: hidden;
}
#locationselector ul:hover {
background-color: #d9d9d9;
overflow: visible;
height: auto;
-webkit-box-shadow: 5px 5px 5px 0px rgba(0, 0, 0, .3);
box-shadow: 5px 5px 5px 0px rgba(0, 0, 0, .3);
}
#locationselector ul li {
background-color: transparent;
color: #494949;
font-family: "museo-sans", "Museo Sans 500", sans-serif;
font-weight: 500;
font-size: 13px;
line-height: 14px;
padding: 6px 3px;
margin: 0;
white-space: nowrap;
text-align: left;
cursor: pointer;
}
#locationselector ul li:hover {
background-color: #FFFFFF;
}
#locationselector ul li.current {
color: #FFFFFF;
}
#locationselector ul li.current:hover {
color: #494949;
}
#locationselector ul:hover li.current {
color: #494949;
}
</style>
谢谢你的时间!