【发布时间】:2015-07-17 03:23:26
【问题描述】:
我正在尝试使用 animate() 为 div 设置动画,使其向上滑动。这很容易,关键是我希望它隐藏在其上方的切换按钮下方(具有更高的 z-index)并消失。换句话说,我不想看到 div 出现在按钮上方。如果它上面的按钮(或其他东西)延伸到屏幕边缘,这会很容易,但事实并非如此。
我尝试使用高度,但这会从下往上缩短 div,而不是相反。我的代码在 Code Pen 上:http://codepen.io/solona/pen/NqPmpp
<div class='column'>
<button class='toggle'>Hide Text</button>
<div class='drawer'>
<p>Content</p>
</div>
</div>
.column {
width: 50%;
margin: 0 auto;
background: lightgrey;
padding: 1em;
}
.toggle {
width: 100%;
background: black;
color: white;
position: relative;
z-index: 500;
}
.drawer {
padding: 1em;
background: pink;
position: relative;
}
$(".toggle").click(function() {
if ($(".drawer").hasClass('open')) {
$(".drawer").animate({
'bottom': $(".drawer").height()
}, 400);
$(this).html("Show Text");
$(".drawer").removeClass('open');
} else if (!$(".drawer").hasClass('open')) {
$(".drawer").animate({
'bottom': '0px'
}, 400);
$(this).html("Hide Text");
$(".drawer").addClass('open');
}
});
【问题讨论】:
标签: jquery jquery-animate slide