Home > Back-end >  Skip to next div slideshow
Skip to next div slideshow

Time:04-17

I have a slideshow with some images. I change between divs that contain images with next and prev keys (every div contain an image). I want to be able to hide one of these picture. Not just hide it, but skipping to next div. The example is from w3school I want to be able to skip the images that have a specific class, not just hide them. Because when I tried to make display to none, this hided the image but the div is still showing.

let slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex  = n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let 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";
}
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  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);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* 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 {
  animation-name: fade;
  animation-duration: 1.5s;
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}
<div >

<div >
  <div >1 / 5</div>
  <img src="https://picsum.photos/220" style="width:100%">
  <div >Caption Text</div>
</div>

 
<div  style="display:none">
<div >
  <div >2 / 5</div>
  <img src="https://picsum.photos/212" style="width:100%">
  <div >Caption Two</div>
</div>
</div>

<div  style="display:none">
<div >
  <div >3 / 5</div>
  <img src="https://picsum.photos/241" style="width:100%">
  <div >Caption Two</div>
</div>
</div>

<div >
  <div >4 / 5</div>
  <img src="https://picsum.photos/244" style="width:100%">
  <div >Caption Three</div>
</div>

<div >
  <div >5 / 5</div>
  <img src="https://picsum.photos/201" style="width:100%">
  <div >Caption Three</div>
</div>

<a  onclick="plusSlides(-1)">❮</a>
<a  onclick="plusSlides(1)">❯</a>

</div>
<br>

<div style="text-align:center">
  <span  onclick="currentSlide(1)"></span> 
  <span  onclick="currentSlide(2)"></span> 
  <span  onclick="currentSlide(3)"></span> 
  <span  onclick="currentSlide(4)"></span> 
  <span  onclick="currentSlide(5)"></span> 
</div>

CodePudding user response:

it better to write

<div >

instead of

<div  style="display:none">
<div >

then to skip element that has class dontview do

if (slides[slideIndex - 1].classList.contains("dontview")) {
    return showSlides(slideIndex  = 1)
}

demo

let slides = document.querySelectorAll(".mySlides"),
  dots = document.querySelectorAll(".dot");
  
let slideIndex = 1;

showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex  = n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  let i;

  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  
  // check it here
  if (slides[slideIndex - 1].classList.contains("dontview")) {
    return showSlides(slideIndex  = 1)
  }
  
  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";

}
*{box-sizing:border-box}
body{font-family:Verdana,sans-serif;margin:0}
.mySlides{display:none}
img{vertical-align:middle}
.slideshow-container{max-width:200px;position:relative;margin:auto}
.prev,.next{cursor:pointer;position:absolute;top:50%;width:auto;padding:16px;margin-top:-22px;color:#fff;font-weight:700;font-size:18px;transition:.6s ease;border-radius:0 3px 3px 0;user-select:none}
.next{right:0;border-radius:3px 0 0 3px}
.prev:hover,.next:hover{background-color:rgba(0,0,0,0.8)}
.text{color:#f2f2f2;font-size:15px;padding:8px 12px;position:absolute;bottom:8px;width:100%;text-align:center}
.numbertext{color:#f2f2f2;font-size:12px;padding:8px 12px;position:absolute;top:0}
.dot{cursor:pointer;height:15px;width:15px;margin:0 2px;background-color:#bbb;border-radius:50%;display:inline-block;transition:background-color .6s ease}
.active,.dot:hover{background-color:#717171}
.fade{animation-name:fade;animation-duration:1.5s}
@keyframes fade {from{opacity:.4}to{opacity:1}}
@media only screen and (max-width: 300px) {.prev,.next,.text{font-size:11px}}
<div >

  <div >
    <div >1 / 5</div>
    <img src="https://via.placeholder.com/200/0000FF/FFFFFF/?text=1" style="width:100%">
    <div >Caption one</div>
  </div>

  <div >
    <div >2 / 5</div>
    <img src="https://via.placeholder.com/200/0000FF/FFFFFF/?text=2" style="width:100%">
    <div >Caption Two</div>
  </div>

  <div >
    <div >3 / 5</div>
    <img src="https://via.placeholder.com/200/0000FF/FFFFFF/?text=3" style="width:100%">
    <div >Caption three</div>
  </div>

  <div >
    <div >4 / 5</div>
    <img src="https://via.placeholder.com/200/FF0000/FFFFFF/?text=4" style="width:100%">
    <div >Caption four</div>
  </div>

  <div >
    <div >5 / 5</div>
    <img src="https://via.placeholder.com/200/0000FF/FFFFFF/?text=5" style="width:100%">
    <div >Caption five</div>
  </div>

  <a  onclick="plusSlides(-1)">❮</a>
  <a  onclick="plusSlides(1)">❯</a>

</div>
<br>

<div style="text-align:center">
  <span  onclick="currentSlide(1)"></span>
  <span  onclick="currentSlide(2)"></span>
  <span  onclick="currentSlide(3)"></span>
  <span  onclick="currentSlide(4)"></span>
  <span  onclick="currentSlide(5)"></span>
</div>

  • Related