我有一个向下滑动切换div,其中包含标题,内容和切换打开按钮。我设置了一个小提琴,但div目前关闭完全阻止你再次打开。我喜欢关闭的div的高度是标题的高度。我怎么这个狐狸?
$(function(){
var open = 1;
$(".toggle").click(function () {
if (open == 1) {
$(".box").animate({"height":"toggle"}, 1000);
open = 0;
$( ".toggle" ).addClass( "open" );
}
else if (open == 0) {
$(".box").animate({"height":"toggle"}, 1000);
open = 1;
$( ".toggle" ).removeClass( "open" );
}
});
});
答案 0 :(得分:2)
点击框外的按钮,看看fiddle
$(function() {
var open = 1;
$(".toggle").click(function() {
if (open == 1) {
$(".box").animate({
"height": "toggle"
}, 1000);
open = 0;
$(".toggle").addClass("open");
} else if (open == 0) {
$(".box").animate({
"height": "toggle"
}, 1000);
open = 1;
$(".toggle").removeClass("open");
}
});
});

.box {
position: fixed;
z-index: 3000;
left: 0;
text-align: left;
background: #fff;
max-width: 250px;
width: 100%;
top: 15%;
}
.box h2 {
/* fallback */
/*background-color: #e9e9e9;
background: url(images/linear_bg_2.png);
background-repeat: repeat-x;*/
/* Safari 4-5, Chrome 1-9 */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#e9e9e9), to(#f6f6f6));
/* Safari 5.1, Chrome 10+ */
background: -webkit-linear-gradient(top, #e9e9e9, #f6f6f6);
/* Firefox 3.6+ */
background: -moz-linear-gradient(top, #e9e9e9, #f6f6f6);
/* IE 10 */
background: -ms-linear-gradient(top, #e9e9e9, #f6f6f6);
/* Opera 11.10+ */
background: -o-linear-gradient(top, #e9e9e9, #f6f6f6);
line-height: 3rem;
padding: 2rem;
margin: 0;
font-size: 1.6em;
}
.box {
position: relative;
z-index: 3000;
left: 0;
text-align: left;
background: #fff;
max-width: 100%;
width: 100%;
top: 0;
}
.toggle {
width: 40px;
height: 26px;
outline: none;
border: none!important;
box-shadow: none;
border-radius: 3px;
}
.toggle.open {}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
<h2>title here</h2>
<div>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas
sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora
incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate
velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
</div>
</div>
<button class="toggle">open</button>
&#13;