It is not necessary to determine the state of the block by checking the variable (in your case one). It is enough to check for the presence / absence of a class, for example, .opened , and add / remove it through toggleClass() .
All animation is much easier to describe in CSS, then it will be much easier to change (if necessary), use media queries.
It is not necessary to set the height of the drop-down block, the functions slideDown() / slideUp() / slideToggle() will determine it itself.
As for me, opening / closing a block is best done with a click not on the entire block, but on a specific trigger (in your case the .content-before header), but this is my subjective opinion, of course, you decide.
Slightly universalized your example in case there are several such drop-down blocks.
$(document).ready(function() { $('.content-before').click(function() { var block = $(this).closest('.content-box'); if (!block.hasClass('opened')) { block.find(".content-after").slideDown(1000); } else { block.find(".content-after").slideUp(500); } block.toggleClass('opened'); }); });
html, body, ul, li { margin: 5px; padding: 5px; } * { box-sizing: border-box; } .content-box { width: 240px; background: dodgerblue; border-radius: 8px; transition: width 0.5s; padding: 15px; } .content-before { font-size: 24px; text-align: center; } .content-after { display: none; font-size: 18px; opacity: 0; color: #ffffff; transition: opacity 1s; width: 470px; } .content-box.opened { width: 500px; transition: width 1s; } .content-box.opened .content-after { opacity: 1; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content-box"> <p class="content-before">Click me to show more !</p> <p class="content-after"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. A adipisci assumenda debitis error maxime molestias quae quasi reiciendis vero voluptatem? Amet aperiam commodi, cum debitis dolor dolorem doloremque dolores dolorum ducimus esse excepturi facere in iure maiores necessitatibus nesciunt nihil, nobis pariatur perspiciatis porro quia quisquam quo quod quos ratione! </p> </div>
If the speed of opening / opening is the same, then the code can be reduced to this option:
$(document).ready(function() { $('.content-before').click(function() { var block = $(this).closest('.content-box'); block.toggleClass('opened'); block.find('.content-after').slideToggle(1000); }); });
html, body, ul, li { margin: 5px; padding: 5px; } * { box-sizing: border-box; } .content-box { width: 240px; background: dodgerblue; border-radius: 8px; transition: width 1s; padding: 15px; } .content-before { font-size: 24px; text-align: center; } .content-after { display: none; font-size: 18px; opacity: 0; color: #ffffff; transition: opacity 1s; width: 470px; } .content-box.opened { width: 500px; } .content-box.opened .content-after { opacity: 1; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content-box"> <p class="content-before">Click me to show more !</p> <p class="content-after"> Lorem ipsum dolor sit amet, consectetur adipisicing elit. A adipisci assumenda debitis error maxime molestias quae quasi reiciendis vero voluptatem? Amet aperiam commodi, cum debitis dolor dolorem doloremque dolores dolorum ducimus esse excepturi facere in iure maiores necessitatibus nesciunt nihil, nobis pariatur perspiciatis porro quia quisquam quo quod quos ratione! </p> </div>