尝试在 1 个单击事件中滑动切换两个单独的元素。它有点工作,但是一个元素没有动画,我在使动画流畅时遇到问题
CSS
.wrapper {
width: 300px; margin: 10px auto; height: 300px;
background-color: #e5e5e5;
border: 1px solid #d8d8d8;
position: relative;
}
.menu {
list-style: none; margin: 0; padding: 0; width: 62%; background-color: blue;
position: absolute; right: 0; top: 0; height: 100%; display: none; z-index: 300;
}
a {
display: block; width: 50px; height: 60px; background-color: orange;
position: absolute; right: 5%; z-index: 500;
}
.moveButton { right: auto; left: 38%; }
HTML
<div class="wrapper">
<a href="#">Menu</a>
<ul class="menu">
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
</div>
jquery
$('a').click(function() { var effect = 'slide'; var options = { direction: 'right' } var duration = 200;
$('.menu').stop().toggle(effect, options, duration); $(this).toggleClass('moveButton'); });
fiddle http://jsfiddle.net/NZ2DX/
注释 我尝试过对toggleClass进行动画处理,但它从左侧而不是右侧进入
菜单按钮应位于展开时的菜单内
最佳答案
我猜你想要this .
$('a').click(function () {
var effect = 'slide';
var options = {
direction: 'right'
}
var duration = 200;
if ($('.menu').is(":hidden")) {
$(this).animate({
right: "62%"
}, duration);
} else {
$(this).animate({
right: "5%"
}, duration);
}
$('.menu').stop().toggle(effect, options, duration);
});
如果您确实希望该按钮位于菜单顶部,您可以这样做 this .
$('a').click(function () {
var effect = 'slide';
var options = {
direction: 'right'
}
var duration = 200;
if ($('.menu').is(":hidden")) {
var w = $(this).parent().width()*0.62 - $(this).width();
$(this).animate({
right: w
}, duration);
} else {
$(this).animate({
right: "5%"
}, duration);
}
$('.menu').stop().toggle(effect, options, duration);
});
关于javascript - jquery SlideToggle 使用 1 个单击事件来切换两个单独的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617101/