我正在尝试创建一个图像库,如下所示,其中每个“宝丽来”div 可以有多个图像可以滚动。我遇到的问题是,每次用户单击左/右箭头时,它不仅会滚动一个 polaroid div,还会继续滚动到其他 div。我想要尝试实现的是对于每个单独的宝丽来 div,我希望用户滚动浏览该特定 div 的图像。我想知道我可以在现有代码中做/更改什么来实现这一目标?
var slideIndex = 1;
showSlides(slideIndex);
// Next/previous controls
function plusSlides(n) {
showSlides(slideIndex += n);
}
// Thumbnail image controls
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
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";
}
.gallery {
display: flex;
flex-wrap: wrap;
}
div.polaroid {
width: 30%;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
/* margin-bottom: 25px;*/
margin-left: 35px;
margin-top: 50px;
}
div.container{
text-align: center;
padding: 10px 20px;
}
#polaroid a:hover{
color: #3a92c8;
}
/* Slideshow container */
.slideshow-container {
max-width: 1000px;
position: relative;
margin: auto;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
position: absolute;
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;
user-select: none;
}
/* 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);
}
/* The dots/bullets/indicators */
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
<div class = "gallery">
<div class = "polaroid">
<div class = "slideshow-container">
<div class="mySlides fade">
<img src="https://scottberkun.com/wp-content/uploads/2016/09/fun-1012680_960_720.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<div class="mySlides fade">
<img src="https://www.todaysparent.com/wp-content/uploads/2020/03/family-activities-battle-coronavirus-cabin-fever-1280x720-810x454.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div class ="container">
<p> A website created using Wix for a life coach, podcast services startup. </p>
</div>
</div>
<div class = "polaroid">
<div class = "slideshow-container">
<div class="mySlides fade">
<img src="https://scottberkun.com/wp-content/uploads/2016/09/fun-1012680_960_720.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<div class="mySlides fade">
<img src="https://www.todaysparent.com/wp-content/uploads/2020/03/family-activities-battle-coronavirus-cabin-fever-1280x720-810x454.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div class ="container">
<p> A website created using sqaurespace for an accounting services business.</p>
</div>
</div>
<div class = "polaroid">
<div class = "slideshow-container">
<div class="mySlides fade">
<img src="https://scottberkun.com/wp-content/uploads/2016/09/fun-1012680_960_720.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<div class="mySlides fade">
<img src="https://www.todaysparent.com/wp-content/uploads/2020/03/family-activities-battle-coronavirus-cabin-fever-1280x720-810x454.jpg" alt="website layout" style="width:100%" class="visible">
</div>
<!-- Next and previous buttons -->
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div class ="container">
<p> A website created using squarespace for a photography and beauty services startup.</p>
</div>
</div>
</div> <!---- THIS DIV IS FOR THE div = gallery layout to make elements on one row-->
答案 0 :(得分:0)
您没有正确计算这些函数中的数字
// Next/previous controls
function plusSlides(n) {
slideIndex += n
showSlides(slideIndex );
}
// Thumbnail image controls
function currentSlide(n) {
slideIndex = n
showSlides(slideIndex);
}
答案 1 :(得分:0)
我认为你可以做的是:
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("polaroid"); //this has changed
var dots = document.getElementsByClassName("dot");
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";
}
我希望我的回答对您有所帮助且清楚,请随时提出问题!
祝你有个美好的一天