我想在模态窗口中创建一个滑块。我制作了代码的第一部分:点击每张图片后打开模态窗口。现在我在模态窗口中前后两个按钮,点击后我想转到下一个图像。我无法理解如何为上一个和下一个按钮编写js函数。我的代码如下:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="uff-8">
<style>
.myImg{
cursor:pointer;
transition:0.3s;
}
.myImg:hover{
opacity:0.7;
}
.modal{
display:none;
position:fixed;
z-index:1;
padding-top: 100px;
left:0;
top:0;
width:100%;
height: 100%;
overflow:auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.9);
}
.modal-content{
margin: auto;
display: block;
width:80%;
max-width: 700px;
}
.modal-content{
-webkit-animation-name:zoom;
-webkit-animation-duration: 0.6s;
animation-name:zoom;
animation-duraion:0.6s;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
top: 0;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* 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);
}
@-webkit-keyframes zoom{
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}
.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;
}
@media only screen and (max-width: 700px){
.modal-content{
width: 100%;
}
}
</style>
</head>
<body>
<img class="myImg" src="/img1" width="300" height="150">
<img class="myImg" src="/img2" width="300" height="150">
<img class="myImg" src="/img3" width="300" height="150">
<div id="myModal" class="modal">
<span class="close">X</span>
<img class="modal-content" id="img01">
<a class="prev">❮</a>
<a class="next">❯</a>
</div>
<script>
var modal = document.getElementById("myModal");
var img = document.getElementsByTagName('img');
var modalImg = document.getElementById('img01');
var prev = document.getElementsByClassName('prev');
var next = document.getElementsByClassName('next');
var i;
var currentImage;
for(i = 0; i<img.length; i++){
img[i].onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
currentImage = this.img[i];
}
}
prev.onclick = function(){
modal.style.display = "block";
modalImg.src = img.src;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
</script>
</body>
</html>
答案 0 :(得分:0)
基本上,我在幻灯片图片中添加了data
属性,其中包含索引,每当点击下一个或上一个时,我会加载下一个或上一个索引图像。
查看以下代码:
var modal = document.getElementById("myModal");
var img = document.getElementsByTagName('img');
var modalImg = document.getElementById('img01');
var currentImage;
for (var i = 0; i < img.length - 1; i++) {
img[i].setAttribute('data', i);
img[i].onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
currentImage = this;
}
}
document.getElementById('prev').onclick = function() {
var count = parseInt(currentImage.getAttribute("data"));
if (count > 0) currentImage = img[count - 1];
modalImg.src = currentImage.src;
}
document.getElementById('next').onclick = function() {
var count = parseInt(currentImage.getAttribute("data"));
if (count < img.length - 2) currentImage = img[count + 1];
modalImg.src = currentImage.src;
}
document.getElementsByClassName("close")[0].onclick = function() {
modal.style.display = "none";
}
.myImg {
cursor: pointer;
transition: 0.3s;
}
.myImg:hover {
opacity: 0.7;
}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.9);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
.modal-content {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duraion: 0.6s;
}
/* Next & previous buttons */
.prev,
.next {
cursor: pointer;
position: absolute;
top: 0;
top: 50%;
width: auto;
margin-top: -22px;
padding: 16px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
}
/* 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);
}
@-webkit-keyframes zoom {
from {
-webkit-transform: scale(0)
}
to {
-webkit-transform: scale(1)
}
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
.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;
}
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
<img class="myImg" src="https://pixabay.com/static/uploads/photo/2012/02/19/17/36/duck-15026_960_720.jpg" width="300" height="150">
<img class="myImg" src="https://pixabay.com/static/uploads/photo/2013/09/22/16/56/duck-185014_960_720.jpg" width="300" height="150">
<img class="myImg" src="https://pixabay.com/static/uploads/photo/2015/01/07/10/32/mandarin-591333_960_720.jpg" width="300" height="150">
<div id="myModal" class="modal">
<span class="close">X</span>
<img class="modal-content" id="img01">
<a id="prev" class="prev">❮</a>
<a id="next" class="next">❯</a>
</div>