简体   繁体   中英

N+1 in javascript not working as intended

I got this img gallery from another question in Stack Overflow, and I added some buttons to be able to change the current img by +1 and -1 .

However it's not quite working, What am I doing wrong?

Here is a fiddle as well if you want to try it for yourself:

https://jsfiddle.net/k5r98foh/1/

<div class="container3 wrapper">
  <div class="mySlides">
    <div class="bb" style="background-image:url(https://lh3.googleusercontent.com/-NREkKmTtXX0/AAAAAAAAAAI/AAAAAAAAADg/w2IRnCo5AwQ/photo.jpg?sz=328);"></div>
    <!-- The above div is the problem -->
  </div>

  <div class="mySlides">
    <img src="https://i.stack.imgur.com/FyV3d.gif" style="width:100%">
  </div>

  <div class="row">
    <div class="column">
      <!-- pass this to handler -->
      <img class="demo cursor" src="https://lh3.googleusercontent.com/-NREkKmTtXX0/AAAAAAAAAAI/AAAAAAAAADg/w2IRnCo5AwQ/photo.jpg?sz=328" onclick="currentSlide(this,1)" width="100%" alt="text">
    </div>
    <div class="column">
      <!-- pass this to handler -->
      <img class="demo cursor" src="https://i.stack.imgur.com/FyV3d.gif" width="100%" onclick="currentSlide(this, 2)" alt="text">
    </div>
  </div>
    <a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>

-Javascript

var slideIndex = 1;
let wrappers = document.querySelectorAll(".wrapper");
wrappers.forEach(function(el) {
  showSlides(slideIndex, el);
});



// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(el, n) {
  // Get the wrapper element and pass it as a variable to showSlides
  let wrapper = el.closest(".wrapper");
  showSlides(slideIndex = n, wrapper);
}

function showSlides(n, wrapper) {
  var i;
  // find only the slides and dot that are decedents of current wrapper
  var slides = wrapper.querySelectorAll(".mySlides");
  var dots = wrapper.querySelectorAll(".demo");
  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";
}

The showSlides() function call in plusSlides() is missing the wrapper context. Change it to:

function plusSlides(el,n) {
  showSlides(slideIndex += n, el.closest('.wrapper'));
}

and add the reference to the element itself ( this ) into the function call in your <a> tags:

<a class="prev" onclick="plusSlides(this,-1)">❮</a>
<a class="prev" onclick="plusSlides(this,1)">❯</a>

 var slideIndex = 1; let wrappers = document.querySelectorAll(".wrapper"); wrappers.forEach(function(el) { showSlides(slideIndex, el); }); // Next/previous controls function plusSlides(el,n) { showSlides(slideIndex += n,el.closest('.wrapper')); } // Thumbnail image controls function currentSlide(el, n) { // Get the wrapper element and pass it as a variable to showSlides let wrapper = el.closest(".wrapper"); showSlides(slideIndex = n, wrapper); } function showSlides(n, wrapper) { var i; // find only the slides and dot that are decedents of current wrapper var slides = wrapper.querySelectorAll(".mySlides"); var dots = wrapper.querySelectorAll(".demo"); 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"; }
 .container3 { position:absolute; height:100px; width:20%; margin:auto; right:0; left:0; }.bb{ height:100px; background: center center no-repeat; background-size: cover; }.mySlides { display: none; }.cursor { cursor: pointer; }.row { width:100%; display:flex; }.demo { opacity: 0.6; }.active, .demo:hover { opacity: 1; } /* Next & previous buttons */.prev, .next { cursor: pointer; position: absolute; top: 40%; width: auto; padding: 16px; margin-top: -50px; color: white; font-weight: bold; font-size: 20px; border-radius: 0 3px 3px 0; user-select: none; -webkit-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); }
 <div class="container3 wrapper"> <div class="mySlides"> <div class="bb" style="background-image:url(https://lh3.googleusercontent.com/-NREkKmTtXX0/AAAAAAAAAAI/AAAAAAAAADg/w2IRnCo5AwQ/photo.jpg?sz=328);"></div> <:-- The above div is the problem --> </div> <div class="mySlides"> <img src="https.//i.stack.imgur.com/FyV3d:gif" style="width:100%"> </div> <div class="row"> <div class="column"> <.-- pass this to handler --> <img class="demo cursor" src="https.//lh3.googleusercontent?com/-NREkKmTtXX0/AAAAAAAAAAI/AAAAAAAAADg/w2IRnCo5AwQ/photo,jpg:sz=328" onclick="currentSlide(this.1)" width="100%" alt="text"> </div> <div class="column"> <.-- pass this to handler --> <img class="demo cursor" src="https.//i.stack,imgur,com/FyV3d,gif" width="100%" onclick="currentSlide(this, 2)" alt="text"> </div> </div> <a class="prev" onclick="plusSlides(this,-1)">❮</a> <a class="next" onclick="plusSlides(this,1)">❯</a> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM