2017-07-27 14 views
1

私は画面の右上隅にあるこの浮動モーダルポップアップを作成しようとしています。私はバックグラウンドをアクティブにしたい(ポップアップを気にせずにウェブページ上で正常に行うことができるように)コンテンツへのポップアップのサイズ変更 - CSS

内部のコンテンツにモーダルのサイズを変更したいときに問題が発生します。私はそのポップアップの中に段落を書くことになっていましたが、それは自動的にそのテキストを含むようにモーダルのサイズを変更することになりました。はるか:

// Get the modal 
 
    var modal = document.getElementById('myModal'); 
 

 
    // Get the button that opens the modal 
 
    var btn = document.getElementById("myBtn"); 
 

 
    // Get the <span> element that closes the modal 
 
    var span = document.getElementsByClassName("close")[0]; 
 

 
    // When the user clicks the button, open the modal 
 
    btn.onclick = function() { 
 
     modal.style.display = "inline-block"; 
 
    } 
 

 
    // When the user clicks on <span> (x), close the modal 
 
    span.onclick = function() { 
 
     modal.style.display = "none"; 
 
    }
  /* The Modal (background) */ 
 
      .modal { 
 
       display: none; /* Hidden by default */ 
 
       position: fixed; /* Stay in place */ 
 
       z-index: 1; /* Sit on top */ 
 
       right: 0; 
 
       top: 0; 
 
       width: 50%; /* Full width */ 
 
       height: 50px; /* Full height */ 
 
       overflow: auto; /* Enable scroll if needed */ 
 
       background-color: rgb(0,0,0); /* Fallback color */ 
 
       background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
 
      } 
 

 
      /* Modal Content */ 
 
      .modal-content { 
 
       background-color: orange; 
 
       position: absolute; 
 
       right: 0px; 
 
       border: 1px solid #888; 
 
       width: 100%; 
 
       height: 100%; 
 
       margin: auto; 
 
      } 
 

 
      /* The Close Button */ 
 
      .close { 
 
       color: #aaaaaa; 
 
       float: right; 
 
       font-size: 28px; 
 
       font-weight: bold; 
 
      } 
 

 
       .close:hover, 
 
       .close:focus { 
 
        color: #000; 
 
        text-decoration: none; 
 
        cursor: pointer; 
 
       }
<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 

 
    </head> 
 
    <body> 
 

 
     <h2>Modal Example</h2> 
 

 
     <!-- Trigger/Open The Modal --> 
 
     <button id="myBtn">Open Modal</button> 
 

 
     <!-- The Modal --> 
 
     <div id="myModal" class="modal"> 
 

 
      <!-- Modal content --> 
 
      <div class="modal-content"> 
 
       <span class="close">&times;</span> 
 
       <p>Some text in the Modal..</p> 
 
      </div> 
 

 
     </div> 
 
    </body> 
 
    </html>

したがって、「モーダルコンテンツ」divがテキストのためにサイズが大きくなったときは、親div(myModal)のサイズをそれに応じて調整します。私はこれを行う方法を理解することはできません。何か案は?

ありがとうございます!

答えて

1

を追加私は長いテキストがありますときにスクロールバーが表示されませんoverflow:autoを削除しました。モーダルは自動的に展開され、最小高さは100ピクセルです。

var modal = document.getElementById('myModal'); 
 

 
// Get the button that opens the modal 
 
var btn = document.getElementById("myBtn"); 
 

 
// Get the <span> element that closes the modal 
 
var span = document.getElementsByClassName("close")[0]; 
 

 
// When the user clicks the button, open the modal 
 
btn.onclick = function() { 
 
    modal.style.display = "inline-block"; 
 
} 
 
span.onclick = function() { 
 
    modal.style.display = "none"; 
 
}
/* The Modal (background) */ 
 

 
.modal { 
 
    display: none; 
 
    /* Hidden by default */ 
 
    position: fixed; 
 
    /* Stay in place */ 
 
    z-index: 1; 
 
    /* Sit on top */ 
 
    right: 0; 
 
    top: 0; 
 
    width: 30%; 
 
    /* Full width */ 
 
    height: auto; 
 
    min-height: 100px; 
 
    /* Full height */ 
 
    background-color: orange; 
 
    border: 1px solid #888; 
 
} 
 

 
.modal-content { 
 
} 
 

 

 
/* The Close Button */ 
 

 
.close { 
 
    color: #aaaaaa; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
    position: absolute; 
 
    top: 0; 
 
    right: 0; 
 
} 
 

 
.close:hover, 
 
.close:focus { 
 
    color: #000; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
}
<h2>Modal Example</h2> 
 

 
<!-- Trigger/Open The Modal --> 
 
<button id="myBtn">Open Modal</button> 
 

 
<!-- The Modal --> 
 
<div id="myModal" class="modal"> 
 

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <span class="close">&times;</span> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam purus nunc, rhoncus in pellentesque vitae, bibendum vitae nunc. Morbi a arcu blandit, maximus tortor rhoncus, commodo libero. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus egestas odio mauris, eu imperdiet ligula lobortis malesuada. Cras sit amet laoreet odio. Vivamus feugiat tortor a ullamcorper pulvinar. Mauris vestibulum semper ex nec pretium. Curabitur in elementum leo, et mollis dui. Integer convallis scelerisque metus, vel pulvinar metus sollicitudin eu. Nunc eu tristique odio.</p> 
 
    </div> 
 

 
</div>

0

モーダルコンテンツからの絶対位置を削除して、いくつかのパディングに

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title> Modal Content </title> 
 
    <style> 
 
     /* The Modal (background) */ 
 
     .modal { 
 
      display: none; /* Hidden by default */ 
 
      position: fixed; /* Stay in place */ 
 
      z-index: 1; /* Sit on top */ 
 
      right: 0; 
 
      top: 0; 
 
      width: 50%; /* Full width */ 
 
      height: auto; /* Full height */ 
 
      overflow: auto; /* Enable scroll if needed */ 
 
      background-color: rgb(0,0,0); /* Fallback color */ 
 
      background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
 

 
     } 
 

 
     /* Modal Content */ 
 
     .modal-content { 
 
      background-color: orange; 
 
      border: 1px solid #888; 
 
      margin: auto; 
 
      padding:20px; 
 
     } 
 

 
     /* The Close Button */ 
 
     .close { 
 
      color: #aaaaaa; 
 
      float: right; 
 
      font-size: 28px; 
 
      font-weight: bold; 
 
     } 
 

 
      .close:hover, 
 
      .close:focus { 
 
       color: #000; 
 
       text-decoration: none; 
 
       cursor: pointer; 
 
      } 
 
    </style> 
 
</head> 
 
<body> 
 

 
    <h2>Modal Example</h2> 
 

 
    <!-- Trigger/Open The Modal --> 
 
    <button id="myBtn">Open Modal</button> 
 

 
    <!-- The Modal --> 
 
    <div id="myModal" class="modal"> 
 

 
     <!-- Modal content --> 
 
     <div class="modal-content"> 
 
      <span class="close">&times;</span> 
 
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p> 
 
     </div> 
 

 
    </div> 
 

 

 

 
    <script> 
 
// Get the modal 
 
var modal = document.getElementById('myModal'); 
 

 
// Get the button that opens the modal 
 
var btn = document.getElementById("myBtn"); 
 

 
// Get the <span> element that closes the modal 
 
var span = document.getElementsByClassName("close")[0]; 
 

 
// When the user clicks the button, open the modal 
 
btn.onclick = function() { 
 
    modal.style.display = "inline-block"; 
 
} 
 

 
// When the user clicks on <span> (x), close the modal 
 
span.onclick = function() { 
 
    modal.style.display = "none"; 
 
} 
 
    </script> 
 
</body> 
 
</html>

関連する問題