【问题标题】:Stick window pop-up to it's position将弹出窗口粘贴到其位置
【发布时间】:2022-10-23 16:21:09
【问题描述】:

因此,我创建了一个在单击按钮时出现的窗口弹出窗口。如果重要,内容窗口会显示视频。现在,问题是当窗口显示/加载时我仍然可以滚动。现在,我正在考虑保持这种方式,因为代码是这样编写的,因此您可以单击网站上的其他任何位置,然后窗口弹出窗口消失。但是,问题是当我滚动时弹出窗口随之而来。如何使弹出窗口粘在它的位置?

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
<style>


  /* The Modal (background) */
.modal {
  display: none; 
  position: fixed; 
  z-index: 1; 
  left: 0;
  top: 0;
  width: 80%; 
  height: 60%; 
  overflow: hidden;
}

/* Modal Content/Box */
.modal-content {
   width: 70%;
        height: 80%;
        position: fixed; /*Can also be `fixed`*/
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        /*Solves a problem in which the content is being cut when the div is smaller than its' wrapper:*/
        max-width: 100%;
        max-height: 100%;
  background-color: #0E0E0E;
  
  
  border: 2px solid #FFFFFF;
 
  
}

/* The Close Button */
.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  position: relative;
  left: -10px;
  top: -1px;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
@media only screen and (min-width: 600px) {
  .col-s-1 {width: 8.33%;}
  .col-s-2 {width: 16.66%;}
  .col-s-3 {width: 25%;}
  .col-s-4 {width: 33.33%;}
  .col-s-5 {width: 41.66%;}
  .col-s-6 {width: 50%;}
  .col-s-7 {width: 58.33%;}
  .col-s-8 {width: 66.66%;}
  .col-s-9 {width: 75%;}
  .col-s-10 {width: 83.33%;}
  .col-s-11 {width: 91.66%;}
  .col-s-12 {width: 100%;}
}

@media only screen and (min-width: 768px) {
  .col-1 {width: 8.33%;}
  .col-2 {width: 16.66%;}
  .col-3 {width: 25%;}
  .col-4 {width: 33.33%;}
  .col-5 {width: 41.66%;}
  .col-6 {width: 50%;}
  .col-7 {width: 58.33%;}
  .col-8 {width: 66.66%;}
  .col-9 {width: 75%;}
  .col-10 {width: 83.33%;}
  .col-11 {width: 91.66%;}
  .col-12 {width: 100%;}
}
.video{
           width: 65%;
        height: 65%;
        position: absolute; /*Can also be `fixed`*/
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        /*Solves a problem in which the content is being cut when the div is smaller than its' wrapper:*/
        max-width: 100%;
        max-height: 100%;
 
}
.myBtn{
  margin-top: 20%;

}
</style>
</head>
        <body>   
<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <center>
    <div class="video">
        
    <video src="https://cdn.shopify.com/videos/c/o/v/1ef399dd4bb345e485f6e4784a9d0031.mp4" type="video/mp4" autoplay width="100%" height="100%"></video>
</center>
    </div>
    
  </div>
  <button class="myBtn" id="myBtn">Open Modal</button>
   
 

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

var btn = document.getElementById("myBtn");

var span = document.getElementsByClassName("close")[0];

btn.onclick = function() {
  modal.style.display = "block";
}

span.onclick = function() {
  modal.style.display = "none";
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
            </script>


  </html>

【问题讨论】:

    标签: javascript html css web-site-project


    【解决方案1】:

    fixed 定位意味着它固定在视口上,无论您滚动它停留在哪里,都可以使用absolute 定位,它相对于文档正文“修复”它(除非另有说明)。您可以阅读有关定位here 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多