模式关闭图标停​​止工作

问题描述:

模式左上角的关闭按钮不起作用 这里是我的代码。我一直在试图找到问题,但我只是找不到问题 我试着从它运行刮但没有帮助过,模式关闭图标停​​止工作

的模态的左上角的关闭按钮并不在这里工作 是我code.I一直在试图寻找这个问题,但我只是不能找到问题 我试图运行它从零开始,但也没有帮助,

<style> 
 
/* The Modal (background) */ 
 
.modal { 
 
    display: none; /* Hidden by default */ 
 
    position: fixed; /* Stay in place */ 
 
    z-index: 1; /* Sit on top */ 
 
    padding-top: 100px; /* Location of the box */ 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; /* Full width */ 
 
    height: 100%; /* 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 { 
 
    position: relative; 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 0; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 
 
    -webkit-animation-name: animatetop; 
 
    -webkit-animation-duration: 0.4s; 
 
    animation-name: animatetop; 
 
    animation-duration: 0.4s 
 
} 
 

 
/* Add Animation */ 
 
@-webkit-keyframes animatetop { 
 
    from {top:-300px; opacity:0} 
 
    to {top:0; opacity:1} 
 
} 
 

 
@keyframes animatetop { 
 
    from {top:-300px; opacity:0} 
 
    to {top:0; opacity:1} 
 
} 
 

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

 
.close:hover, 
 
.close:focus { 
 
    color: #000; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 

 
.modal-header { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 

 
.modal-body {padding: 2px 16px;} 
 

 
.modal-footer { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 
.vege { 
 
float: left; 
 
width: 50%; 
 
} 
 
</style> 
 

 

 

 
<h2>Animated Modal with Header and Footer</h2> 
 

 
<!-- Trigger/Open The Modal --> 
 
<div class="vege" id="myBtn"><img src="https://cdn.shopify.com/s/files/1/1726/2025/files/final-1.jpg?12075096900285349117" <div=""> 
 

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

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
    <div class="modal-header"> 
 
     <span class="close">×</span> 
 
     <h2>Modal Header</h2> 
 
    </div> 
 
    <div class="modal-body"> 
 
     <p>Some text in the Modal Body</p> 
 
     <p>Some other text...</p> 
 
    </div> 
 
    <div class="modal-footer"> 
 
     <h3>Modal Footer</h3> 
 
    </div> 
 
    </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 = "block"; 
 
} 
 

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

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
     modal.style.display = "none"; 
 
    } 
 
} 
 
</script> 
 

 

 

 
</div>

+0

显示你的HTML代码和JavaScript代码,如果它有什么。 –

+0

@HimanshuUpadhyay你滚动了片段吗? –

+1

@SagarV,对不起,我的坏。收回我的评论。 –

您已将eventListener添加到关闭按钮,另一个添加到窗口。

当您单击跨度时,将执行两个事件回调。在span的click事件处理程序的回调中使用e.stopPropagation()来防止它。

// 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 = "block"; 
 
} 
 

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

 
// When the user clicks anywhere outside of the modal, close it 
 
window.onclick = function(event) { 
 
    if (event.target == modal) { 
 
    modal.style.display = "none"; 
 
    } 
 
}
/* The Modal (background) */ 
 

 
.modal { 
 
    display: none; 
 
    /* Hidden by default */ 
 
    position: fixed; 
 
    /* Stay in place */ 
 
    z-index: 1; 
 
    /* Sit on top */ 
 
    padding-top: 100px; 
 
    /* Location of the box */ 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; 
 
    /* Full width */ 
 
    height: 100%; 
 
    /* 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 { 
 
    position: relative; 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 0; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); 
 
    -webkit-animation-name: animatetop; 
 
    -webkit-animation-duration: 0.4s; 
 
    animation-name: animatetop; 
 
    animation-duration: 0.4s 
 
} 
 

 

 
/* Add Animation */ 
 

 
@-webkit-keyframes animatetop { 
 
    from { 
 
    top: -300px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    top: 0; 
 
    opacity: 1 
 
    } 
 
} 
 

 
@keyframes animatetop { 
 
    from { 
 
    top: -300px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    top: 0; 
 
    opacity: 1 
 
    } 
 
} 
 

 

 
/* The Close Button */ 
 

 
.close { 
 
    color: white; 
 
    float: right; 
 
    font-size: 28px; 
 
    font-weight: bold; 
 
} 
 

 
.close:hover, 
 
.close:focus { 
 
    color: #000; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 

 
.modal-header { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 

 
.modal-body { 
 
    padding: 2px 16px; 
 
} 
 

 
.modal-footer { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 

 
.vege { 
 
    float: left; 
 
    width: 50%; 
 
}
<h2>Animated Modal with Header and Footer</h2> 
 

 
<!-- Trigger/Open The Modal --> 
 
<div class="vege" id="myBtn"><img src="https://cdn.shopify.com/s/files/1/1726/2025/files/final-1.jpg?12075096900285349117" <div=""> 
 

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

 
    <!-- Modal content --> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <span class="close">×</span> 
 
     <h2>Modal Header</h2> 
 
     </div> 
 
     <div class="modal-body"> 
 
     <p>Some text in the Modal Body</p> 
 
     <p>Some other text...</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <h3>Modal Footer</h3> 
 
     </div> 
 
    </div> 
 

 
    </div> 
 

 

 

 

 
</div>

+0

我需要你的帮助http://liveweave.com/#&togetherjs=B2TcxhtHrl第二个模型不工作的关闭跨度 – devdreamer

+0

嗨@devdreamer你有一个问题'var gpan = document.getElementsByClassName(“close2”)'你缺少该指数。它应该是'var gpan = document.getElementsByClassName(“close2”)[0]'。我尝试和完美的作品 –

试试这个: -

我已经改变了它点点。 更改: -

1)。给定Id到跨度标签

2)。在JS中关于量程变更的代码

3)。正确关闭Div和Img标签。 4)。添加的类(.modal-背景)在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.getElementById("CloseModal"); 
 

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

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

 
    // When the user clicks anywhere outside of the modal, close it 
 
    window.onclick = function (event) { 
 
     if (event.target == modal) { 
 
      modal.style.display = "none"; 
 
     } 
 
    }
/* The Modal (background) */ 
 
.modal { 
 
    display: none; /* Hidden by default */ 
 
    position: fixed; /* Stay in place */ 
 
    z-index: 1; /* Sit on top */ 
 
    padding-top: 100px; /* Location of the box */ 
 
    left: 0; 
 
    top: 0; 
 
    width: 100%; /* Full width */ 
 
    height: 100%; /* 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 { 
 
    position: relative; 
 
    background-color: #fefefe; 
 
    margin: auto; 
 
    padding: 0; 
 
    border: 1px solid #888; 
 
    width: 80%; 
 
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 
 
    -webkit-animation-name: animatetop; 
 
    -webkit-animation-duration: 0.4s; 
 
    animation-name: animatetop; 
 
    animation-duration: 0.4s 
 
} 
 

 
/* Add Animation */ 
 
@-webkit-keyframes animatetop { 
 
    from {top:-300px; opacity:0} 
 
    to {top:0; opacity:1} 
 
} 
 

 
@keyframes animatetop { 
 
    from {top:-300px; opacity:0} 
 
    to {top:0; opacity:1} 
 
} 
 

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

 
.close:hover, 
 
.close:focus { 
 
    color: #000; 
 
    text-decoration: none; 
 
    cursor: pointer; 
 
} 
 

 
.modal-header { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 

 
.modal-body {padding: 2px 16px;} 
 

 
.modal-footer { 
 
    padding: 2px 16px; 
 
    background-color: #5cb85c; 
 
    color: white; 
 
} 
 
.vege { 
 
float: left; 
 
width: 50%; 
 
} 
 

 
.modal-backdrop { 
 
    position: fixed; 
 
    top: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    z-index: 1030; 
 
}
<h2>Animated Modal with Header and Footer</h2> 
 

 
    <!-- Trigger/Open The Modal --> 
 
    <div class="vege" id="myBtn" data-toggle="modal" data-target="#myModal"> 
 
     <img src="https://cdn.shopify.com/s/files/1/1726/2025/files/final-1.jpg?12075096900285349117" /> 
 
    </div> 
 

 
    <!-- The Modal --> 
 
    <div id="myModal" class="modal"> 
 
     <!-- Modal content --> 
 
     <div class="modal-content"> 
 
      <div class="modal-header"> 
 
       <span id="CloseModal" class="close">×</span> 
 
       <h2>Modal Header</h2> 
 
      </div> 
 
      <div class="modal-body"> 
 
       <p>Some text in the Modal Body</p> 
 
       <p>Some other text...</p> 
 
      </div> 
 
      <div class="modal-footer"> 
 
       <h3>Modal Footer</h3> 
 
      </div> 
 
     </div> 
 

 
    </div>

感谢。

+0

没有工作Archana,如果你可以使其工作任何其他方式将不胜感激 – devdreamer

+0

嘿@devdreamer,我已经更新了我的答案。请尝试。 –