我正在使用自定义的灯箱在网页上显示图片。
当我在同一页面上两次使用同一灯箱时。发生某种错误。
我将每个灯箱划分为两个部分,分别将内容放在两个单独的div中,并添加了两次引用每个灯箱的脚本,从而将它们分开。
重新加载页面时,我遇到许多不同的错误。
看看。
<!--Lightbox script-->
<script>
function openModal() {
document.getElementById('myModal1').style.display = "block";
}
function closeModal() {
document.getElementById('myModal1').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;
}
</script>
<!--Lightbox script-->
<script>
function openModal() {
document.getElementById('myModal2').style.display = "block";
}
function closeModal() {
document.getElementById('myModal2').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;
}
</script>
.containercontent1
{
position:relative;
width:100%;
height:750px;
}
.containercontent2
{
position:relative;
width:100%;
height:750px;
}
/* Lightbox css */
.column1 {
box-sizing:border-box;
padding:5px;
float:left;
width:30%; }
.column1:hover {
cursor:pointer; }
.modal1 {
display: none;
position: fixed;
z-index: 12;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal2 {
display: none;
position: fixed;
z-index: 12;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
position: absolute;
z-index:12;
background-color: #fefefe;
top:50px;
left:200px;
padding: 0;
width: 90%;
max-width:900px;
}
/* lightbox buttons*/
.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
}
.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;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 4);
}
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/*lightbox img*/
img{
margin-bottom: -4px;}
<div class="containercontent1">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" class="column1" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" class="column1" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div id="myModal1" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 2</div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 2</div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
<div class="containercontent2">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" class="column1" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" class="column1" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" class="column1" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div id="myModal2" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 3</div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 3</div>
<img src="https://cdn.sstatic.net/Sites/stackoverflow/img/error-lolcat-problemz.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 3</div>
<img src="" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
答案 0 :(得分:0)
问题是您两次定义了相同名称的函数。 这两个-tag在范围上没有分隔,因此,如果您使用相同的名称定义一个函数,则最新定义的一个将始终获胜。
因此,请为您的函数选择其他名称,或编写通用函数以检查单击了哪个触发器。