試試這個fiddle
HTML
<div class="menu">
<ul class="upper">
<li><a href="" title="">Dynamic item 1</a>
<ul class="inner">
<li> <a href="">Subitem 1 of 1</a>
</li>
<li> <a href="">Subitem 2 of 1</a>
</li>
</ul>
</li>
<li> <a href="">Static item</a>
</li>
<li><a href="">Dynamic item 2</a>
<ul class="inner">
<li><a href="">Subitem 1 of 2</a>
</li>
</ul>
</li>
</ul>
</div>
CSS
.menu {
width: 150px;
float: right;
position: relative;
}
.menu ul li {
margin-bottom: 1px;
}
.menu ul.upper li a {
width: 150px;
background-color: #000;
color: #FFF;
text-decoration: none;
display: block;
padding: 7px 10px;
text-align: left;
font-weight: bold;
}
.menu ul.inner {
position: absolute;
right: 160px;
z-index: 1000;
display: none;
width: 150px;
right: 170px;
}
.menu ul.inner li a {
width: 150px;
background-color: #3D6AA2;
color: #FFF;
text-decoration: none;
display: block;
padding: 7px 10px;
text-align: left;
font-weight: bold;
}
JS
$('.upper li').each(function() {
$(this).find('ul.inner').css({top: $(this).offset().top})
});
$('.upper li').hover(function() {
$(this).find('ul.inner').stop(true, true).animate({
width: "toggle"
}, {
queue: false,
duration: 250
});
});
小提琴請..... –
如果您不希望它只工作一次,您必須在開始動畫之前每次都將寬度設置爲0。 – slash197