문제

I created a jquery code which lets you click on a button to show or hide a div. However, when I toggle the div the container shifts a bit while it opens. I googled, and everywhere it says the expanding div is losing it's height. How do I fix it? I can't set a height to that div because inside the div I have more expanding content, like little paragraphs where you click "read more" and then more text appears, so the height of the expanding div is always changing.

This is my code:

<script type="text/javascript">
    $(window).load(
      function ()
      {
        $(".expandbutton").click(
          function ()
          {
            $("#mainexpand").toggle("fast");
          }
        );
      }
    );
</script> 

So how do I prevent the content from moving around when you click the .expandbutton div to toggle the #mainexpand div?

도움이 되었습니까?

해결책

$(document).ready(function(){
  $("#mainexpand").hide();

 $( ".expandbutton" ).click(function() {

  if($("#mainexpand").is(":visible")){
    $("#mainexpand").hide();
  } 
   else {
   $("#mainexpand").show();
  }

 });
 });

다른 팁

You need to set visibility instead of displaying or hinding it with display:

$(window).load(function(){
 $( ".expandbutton" ).click(function() {
  if($("#mainexpand").css("visibility") == "visible"){
    $("#mainexpand").css("visibility","hidden");
  } else {
   $("#mainexpand").css("visibility","visible");
  }
 });
 });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top