javascript - 带手动幻灯片的弹出框画廊

标签 javascript html css popup gallery

你能帮我看看这段代码吗?当有人点击图像时,此代码会打开一个图库弹出窗口。如果有人点击不同的图片,它应该会打开不同的画廊,但它不会……你能更正我的代码,让它适用于不同的画廊吗?谢谢!

// Get the image and insert it inside the modal
function imgg(id){
    var modal = document.getElementById(id);
		var modalImg = document.getElementById('mySlides');
    modal.style.display = "block";
    modalImg.src = this.src;
}

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

// Sliseshow
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = x.length} ;
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  x[slideIndex-1].style.display = "block";  
}
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* 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.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.mySlides {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
box-shadow: 0px 0px 50px -6px #000;
}

@-webkit-keyframes zoom {
    from {-webkit-transform:scale(0)} 
    to {-webkit-transform:scale(1)}
}

@keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

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

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .mySlides {
        width: 100%;
    }
}

.w3-btn-floating {
  
}
<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="test.css" rel="stylesheet" type="text/css">

<link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css">


</head>
<body>

<img id="myImg" onClick="imgg('myModal')" src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="" width="300" height="200">

<img id="myImg" onClick="imgg('myModal1')"src="http://therivardreport.com/wp-content/uploads/2013/05/Daniel-Chaffin.jpg" alt="" width="300" height="200">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span onclick="close()" class="close">×</span>
	<img class="mySlides" id="img_modal" src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" >
	<img class="mySlides" id="img_modal" src="http://interrete.org/wp-content/uploads/2014/04/Miniature-World-of-Insects6.png" >
	<img class="mySlides" id="img_modal" src="http://www.isharearena.com/wp-content/uploads/2012/12/wallpaper-281049.jpg?d54e04" >
	<img class="mySlides" id="img_modal" src="http://cdn.theatlantic.com/assets/media/img/photo/2015/11/images-from-the-2016-sony-world-pho/s01_130921474920553591/main_900.jpg?1448476701" >

	<a class="w3-btn-floating" style="position:absolute;top:45%;left:280px;" onclick="plusDivs(-1)">&#10094;</a>
	<a class="w3-btn-floating" style="position:absolute;top:45%;right:280px;" onclick="plusDivs(1)">&#10095;</a>
</div>

<div id="myModal1" class="modal">
  <span onclick="close()" class="close">×</span>
	<img class="mySlides" id="img_modal" src="http://therivardreport.com/wp-content/uploads/2013/05/Daniel-Chaffin.jpg" >
	<img class="mySlides" id="img_modal" src="http://www.catnipcamera.com/wp-content/uploads/2012/03/DSCN98911.jpg" >
	<img class="mySlides" id="img_modal" src="http://www.desibucket.com/db2/01/26021/26021.jpg" >

	<a class="w3-btn-floating" style="position:absolute;top:45%;left:280px;" onclick="plusDivs(-1)">&#10094;</a>
	<a class="w3-btn-floating" style="position:absolute;top:45%;right:280px;" onclick="plusDivs(1)">&#10095;</a>
</div>
</body>
</html>

最佳答案

关闭按钮不起作用,因为变量“modal”已声明到函数“imgg”中,因此函数“close”无法使用它。

关于javascript - 带手动幻灯片的弹出框画廊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37442361/

相关文章:

javascript - Bluebird 的 Promise 解析返回未定义

javascript - 更改选定复选框上的标签类别

css - 在 Full Calendar 2.3.2 中更改标题标题颜色

javascript - 基于 jquery 的图像拼接墙布局

javascript - 单击按钮时无法更改输入框的值

javascript - LitElement 不会为原生 Web 组件设置 textContent

html - Doctype over Meta X-UA-Compatibility

html - 将图像 div 对齐到外部 div 的底部

javascript - 查找并替换可能包含子元素的 html 元素的文本部分

c# - 带有 Bootstrap Glyphicon 的 ASP.NET 按钮