正如我所说......我希望将每个特定图像链接到模态轮播中的位置。
现在我只有第一个是活动图像,每当任何图像时它都会打开点击。
抱歉,我根本不擅长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">×</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>
答案 0 :(得分:1)
您需要获取所点击图像的索引并将其传递给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);
};
}