gpt4 book ai didi

javascript - 将每个特定图像与模式中的位置链接起来

转载 作者:行者123 更新时间:2023-11-28 15:19:55 24 4
gpt4 key购买 nike

正如我所说...我想将每个特定图像链接到模态轮播中的位置。
现在我只有第一个是事件图像,每次有任何图像时它都会打开被点击。

抱歉,我一点也不擅长 JavaScript。
如果你能帮助我,我将非常高兴。

谢谢。


脚本:

<script>
var modal = document.getElementById('myModal');
var images = document.getElementsByTagName('img');
var captionText = document.getElementById("caption");
var i;
for (i = 0; i < images.length; i++) {
images[i].onclick = function () {
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
};
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function () {
modal.style.display = "none";
};
function myFunction() {
document.getElementById("panel").style.display = "block";};
$('.modal').on('show.bs.modal', centerModal);$(window).on("resize", function () {$('.modal:visible').each(centerModal);});

模态轮播代码:

<div class="container">
<div class="row content">
<div class="gallery text-center">
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/1.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/2.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/3.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/1.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/2.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/3.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/1.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/2.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/3.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/1.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/2.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/3.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/1.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/2.jpg" width="256" height="171" alt="" ></div>
<div class="col-md-4 col-xs-6 img-responsive item"><img src="images/3.jpg" width="256" height="171" alt="" ></div>
</div>

<div class="modal" id="myModal" tabindex="-1" role="dialog">
<span class="close">&times;</span>

<div class="modal-content">

<div id="carousel-modal-demo" class="carousel slide" data-ride="carousel" data-interval="9999999999999">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-modal-demo" data-slide-to="0" class="active"></li>
<li data-target="#carousel-modal-demo" data-slide-to="1"></li>
<li data-target="#carousel-modal-demo" data-slide-to="2"></li>
<li data-target="#carousel-modal-demo" data-slide-to="3"></li>
<li data-target="#carousel-modal-demo" data-slide-to="4"></li>
<li data-target="#carousel-modal-demo" data-slide-to="5"></li>
<li data-target="#carousel-modal-demo" data-slide-to="6"></li>
<li data-target="#carousel-modal-demo" data-slide-to="7"></li>
<li data-target="#carousel-modal-demo" data-slide-to="8"></li>
<li data-target="#carousel-modal-demo" data-slide-to="9"></li>
<li data-target="#carousel-modal-demo" data-slide-to="10"></li>
<li data-target="#carousel-modal-demo" data-slide-to="11"></li>
<li data-target="#carousel-modal-demo" data-slide-to="12"></li>
<li data-target="#carousel-modal-demo" data-slide-to="13"></li>
<li data-target="#carousel-modal-demo" data-slide-to="14"></li>
</ol>



<div class="carousel-inner">
<div class="item active">
<img src="images/1.jpg" alt=""/>
</div>
<div class="item">
<img src="images/2.jpg" alt=""/>
</div>
<div class="item">
<img src="images/3.jpg" alt=""/>
</div>
<div class="item">
<img src="images/1.jpg" alt=""/>
</div>
<div class="item">
<img src="images/2.jpg" alt=""/>
</div>
<main class="item">
<img src="images/3.jpg" alt=""/>

</main>
<div class="item">
<img src="images/1.jpg" alt=""/>
</div>
<main class="item">
<img src="images/2.jpg" alt=""/>
</main>
<main class="item">
<img src="images/3.jpg" alt=""/>
</main>
<div class="item">
<img src="images/1.jpg" alt=""/>
</div>
<div class="item">
<img src="images/2.jpg" alt=""/>
</div>
<div class="item">
<img src="images/3.jpg" alt=""/>
</div>
<div class="item">
<img src="images/1.jpg" alt=""/>
</div>
<p class="item">
<img src="images/2.jpg" alt=""/>
</p>
<p class="item">
<img src="images/3.jpg" alt=""/>
</p>



<a class="left carousel-control" href="#carousel-modal-demo" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-modal-demo" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>

</div>
</div>
</div>
</div>
</div>

最佳答案

您需要获取被点击图像的索引并将其传递给 carousel()。

用这个改变你的for循环

for (i = 0; i < images.length; i++) {
images[i].onclick = function () {
modal.style.display = "block";
var index= $(this).closest("div").index();
$('#carousel-modal-demo').carousel(index);
};
}

关于javascript - 将每个特定图像与模式中的位置链接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46389842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com