首页 > 解决方案 > 为什么移动设备上的幻灯片之间的图像闪烁?

问题描述

首先,我显示一个错误,内容如下:

Uncaught TypeError: Cannot read property 'className' of undefined

其次,当我在移动设备上点击幻灯片时,图像之间出现了奇怪的闪光。我试图理解错误消息的含义,但我不明白。这是否意味着我需要在某个地方摆脱课程?

未定义的 className 究竟是什么?

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";
}
* {box-sizing:border-box}

body {
  margin: 0;
  box-sizing: border-box;
  padding: 60px;
  overflow-x: hidden;
  -webkit-transform: translateZ(0);
  background: white;
  
}

.title {
  line-height: 150%;
  font-size: 17px;
  font-family: 'helvetica', sans-serif;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 22vw;
  display: none;
}

/* Slideshow container */
.slideshow-container {
  max-height: calc(100vh - 120px);
  height: calc(100vh - 120px);
  display: flex;
  justify-content: flex-end;
  margin: 0px;
  line-height: 0%;
}

.slideshow-container img {
  object-fit: cover;
}
/* Hide the images by default */
.mySlides {
    display: none;
  max-height: calc(100vh - 120px);
  height: calc(100vh - 120px);
  line-height: 0%;
  -webkit-backface-visibility: hidden;
}

section img {
  width: calc(100vw - 120px);
  height: calc(100vh - 120px);
}

.contain img {
  object-fit: contain;
  object-position: 100% 50%;
  line-height: 0%;
  
}

img {
  height: 100%;
  max-width: 100%;
  
}

/* Next & previous buttons */
.prev {
  cursor: pointer;
  display: none;
  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;
}

/* Position the "next button" to the right */
.next {
  border-radius: 3px 0 0 3px;
  top: 60px;
  position: absolute;
  width: calc(100vw - 120px);
  height: calc(100vh - 120px);
  -webkit-backface-visibility: hidden;
  
}

.next:active {
   -webkit-backface-visibility: hidden;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: transparent;
  cursor: pointer;
}

/* 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: ;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: transparent;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1s;
  animation-name: fade;
  animation-duration: 1s;
}

@-webkit-keyframes fade {
  0% {opacity: 1} 
  100% {opacity: 1}
}

@keyframes fade {
  0% {opacity: 1} 
  100% {opacity: 1}
}







@media only screen and (max-width: 720px) {
  
  * {box-sizing: border-box}

body {
      padding: 0px;
}
  
  .mySlides {
    display: flex;
  max-height: calc(100vh - 20px);
  height: calc(100vh - 90px);
  line-height: 0%;
    overflow-x: hidden;
}

section img {
  width: calc(100vw - 20px);
  height: calc(100vh - 90px);
margin: 10px;
  
  
  }
  
  .next {
  border-radius: 0;
  top: 0px;
  position: absolute;
  width: 100vw;
  background: transparent;
  height: calc(100vh - 90px);
    -webkit-transform-style: preserve-3d;
    -webkit-backface-visibility: hidden;
  
}
  
   .next:hover {
    background: rgba(0,0,0,0);
  }
  
  .next:active {
    background: rgba(0,0,0,0);
    -webkit-backface-visibility: hidden;
  }
  
  .contain img {
  object-fit: contain;
    background: rgba(0,0,0,0);
    
}
  
  
}


@media only screen and (max-height: 520px) {
  body {

  }
  
  
  .slideshow-container {
  max-height: calc(100vh - 0px);
  height: calc(100vh - 120px);
  margin: 0px;
  line-height: 0%;
}
  
  
    .mySlides {

  max-height: calc(100vh - 0px);
  height: calc(100vh - 0px);
  line-height: 0%;
    overflow-x: hidden;

}

section img {
  width: calc(100vw - 20px);
  height: calc(100vh - 20px);
margin: 10px;

  
  }
  
  .next {
  border-radius: 0;
  top: 0px;
  position: absolute;
  width: 100vw;
  background: rgba(0,0,0,0);
  height: calc(100vh - 0px);
  
}
  
     .next:hover {
    background: rgba(0,0,0,0);
  }
  
  .next:active {

  }
  
  
}
<!-- Slideshow container -->
<div class="slideshow-container">


  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.00.17+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  
    <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.09.46+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
  <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.14.05+AM.png" alt>
</section>
    <div class="text"></div>
  </div>

  
  
  
  
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.15.57+AM.png">
    </section>
    <div class="text"></div>
  </div>

  
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.11.45+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.12.32+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.14.05+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.13.40+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.45.03+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.12.54+AM.png" >
    </section>
    <div class="text"></div>
  </div>
  
  <div class="mySlides fade">
    <div class="numbertext"></div>
    <section class="contain">
    <img src="https://assets.codepen.io/226140/Screen+Shot+2020-08-09+at+2.42.28+AM.png" >
    </section>
    <div class="text"></div>
  </div>


  <!-- Next and previous buttons -->
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)"></a>
</div>
<br>

<!--
<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>
The dots/circles -->

标签: javascriptslideshow

解决方案


我认为您尚未定义的“类名”在dots[i].className = dots[i].className.replace("active", "");


推荐阅读