Reputation: 852
I have a slide Down toggle div which contains a title, content and a toggle open button. I've set up a fiddle but the div currently closes up completely preventing you from opening again. The height i'd like the div to close up to is the height of the title. How do I fox this?
$(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" );
}
});
});
Upvotes: 1
Views: 474
Reputation: 15725
take the button outside of the box see this 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>
Upvotes: 2