我正在尝试使用CC模板设计网站,并尝试按照以下方式实施Lightbox:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_lightbox
灯箱显示效果很好,但我遇到的问题是,如果我在灯箱外面点击,没有任何反应 - 也没有关闭按钮。
我对JavaScript和Web开发非常基础,所以请保持温和。
这是从w3schools拉出的灯箱代码,我将其修改为使用我自己的图像并添加colwrap div,以便我可以并排放置图像:
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="images/team/test2.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="images/team/test3.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="images/team/test4.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="images/team/test5.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div id="colwrap">
<img class="demo cursor" src="images/team/test2.jpg" style="width:20%" onclick="currentSlide(1)" alt="Jacob Wilson - Vocals">
<img class="demo cursor" src="images/team/test3.jpg" style="width:20%" onclick="currentSlide(2)" alt="Kevin Lee - Lead Guitar">
<img class="demo cursor" src="images/team/test4.jpg" style="width:22%; height:5%;" onclick="currentSlide(3)" alt="Jeff Bridges - Drums">
<img class="demo cursor" src="images/team/test5.jpg" style="width:20%" onclick="currentSlide(4)" alt="Shikamaru Nara - Bass">
</div>
这是关闭模式的JavaScript:
function closeModal() {
document.getElementById('myModal').style.display = "none";
}
所以目前没有关闭图标,外面点击也没有做任何事情。但是,如果我改变这一行:
<span class="close cursor" onclick="closeModal()">×</span>
跨越任何不会关闭光标的班级&#34;模态左上角会有一个小X,可以用来关闭灯箱。我不能真正理解那条线正在做什么,因为我可以把它设置为一个不存在的类,它仍然会给那个小X.但是如果它关闭了光标我不会#39;得到那个X.
对于格式不好的帖子感到抱歉,我不确定要包含哪些代码,因为它非常冗长。任何想法都将不胜感激。
答案 0 :(得分:0)
您可以尝试从事件对象调用名为stopPropagation()
的函数:
我从这个演示改变了2个功能:
function plusSlides(event, n) {
event.stopPropagation();
showSlides(slideIndex += n);
}
function currentSlide(event, n) {
event.stopPropagation();
showSlides(slideIndex = n);
}
和html at:
<a class="prev" onclick="plusSlides(event, -1)">❮</a>
<a class="next" onclick="plusSlides(event, 1)">❯</a>
<div class="column">
<img class="demo cursor" src="img_nature_wide.jpg" style="width:100%" onclick="currentSlide(event, 1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="img_fjords_wide.jpg" style="width:100%" onclick="currentSlide(event, 2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(event, 3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="img_lights_wide.jpg" style="width:100%" onclick="currentSlide(event, 4)" alt="Northern Lights">
</div>
</div>
</div>
接下来,我将closeModal
移至父div
<div id="myModal" class="modal" onclick="closeModal()">
<span class="close cursor">×</span>
工作正常!
在here为您链接演示!希望这可以提供帮助!
答案 1 :(得分:0)
检查代码段是否有帮助。
function openModal() {
document.getElementById('myModal').style.display = "block";
}
function closeModal() {
document.getElementById('myModal').style.display = "none";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
$(document).mouseup(function(e){
if($(e.target).parents('.modal-content').length==0 && !$(e.target).is('.modal-content')){
$('.modal').css('display', 'none');
}
});
&#13;
body {
font-family: Verdana, sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
}
/* The Close Button */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
.mySlides {
display: none;
}
.cursor {
cursor: pointer
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
img {
margin-bottom: -4px;
}
.caption-container {
text-align: center;
background-color: black;
padding: 2px 16px;
color: white;
}
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s
}
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
&#13;
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="http://placehold.it/200x200" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://placehold.it/200x200" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://placehold.it/200x200" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="http://placehold.it/200x200" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="http://placehold.it/500x500" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="http://placehold.it/500x500" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="http://placehold.it/500x500" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="http://placehold.it/500x500" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="demo cursor" src="http://placehold.it/100x100" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="http://placehold.it/100x100" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="http://placehold.it/100x100" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="http://placehold.it/100x100" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
&#13;
点击modal
或lightbox
内容view this link
其他文件添加Google ajax
。
modal
之外点击时,用于关闭lightbox
或modal-content
的jQuery脚本。
$(document).mouseup(function(e){
if($(e.target).parents('.modal-content').length==0 && !$(e.target).is('.modal-content')){
$('.modal').css('display', 'none');
}
});
答案 2 :(得分:0)
当有人点击外部并关闭模式时,您需要捕获事件。
试试这个。
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Verdana, sans-serif;
margin: 0;
}
* {
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: black;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
width: 90%;
max-width: 1200px;
}
/* The Close Button */
.close {
color: white;
position: absolute;
top: 10px;
right: 25px;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #999;
text-decoration: none;
cursor: pointer;
}
.mySlides {
display: none;
}
.cursor {
cursor: pointer
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
}
/* Position the "next button" to the right */
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
/* Number text (1/3 etc) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
img {
margin-bottom: -4px;
}
.caption-container {
text-align: center;
background-color: black;
padding: 2px 16px;
color: white;
}
.demo {
opacity: 0.6;
}
.active,
.demo:hover {
opacity: 1;
}
img.hover-shadow {
transition: 0.3s
}
.hover-shadow:hover {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
</style>
<body>
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="1.png" style="width:100%" onclick="openModal(event);currentSlide(event,1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="2.jpeg" style="width:100%" onclick="openModal(event);currentSlide(event,2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="3.jpg" style="width:100%" onclick="openModal(event);currentSlide(event,3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="4.jpg" style="width:100%" onclick="openModal(event);currentSlide(event,4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal(event)">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="1.png" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="2.jpeg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="3.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="4.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(event, -1)">❮</a>
<a class="next" onclick="plusSlides(event, 1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="demo cursor" src="1.png" style="width:100%" onclick="currentSlide(event,1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="2.jpeg" style="width:100%" onclick="currentSlide(event,2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="3.jpg" style="width:100%" onclick="currentSlide(event, 3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="4.jpg" style="width:100%" onclick="currentSlide(event,4)" alt="Northern Lights">
</div>
</div>
</div>
<script>
window.addEventListener('click', clickedOutside, false);
function clickedOutside(event){
event.stopPropagation();
closeModal() ;
}
function openModal(event) {
event.stopPropagation();
document.getElementById('myModal').style.display = "block";
}
function closeModal(event) {
document.getElementById('myModal').style.display = "none";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(event,n) {
event.stopPropagation();
showSlides(slideIndex += n);
}
function currentSlide(event,n) {
event.stopPropagation();
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
</script>
</body>
</html>