javascript - 为什么移动设备上的幻灯片之间的图像闪烁?
问题描述
首先,我显示一个错误,内容如下:
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)">❮</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 -->
解决方案
我认为您尚未定义的“类名”在dots[i].className = dots[i].className.replace("active", "");
推荐阅读
- google-bigquery - 将旧版 SQL 转换为标准 SQL - 增强型电子商务
- python-3.x - 如何使用帧单元制作 TIME 字幕(.srt、.smi 等)
- c# - 无法从另一个脚本实例化预制件
- facebook-graph-api - 获取我自己的 Instagram 帖子所需的 Graph API 权限
- python - 扩展“特定”的 defaultdict(str,...)?
- reactjs - 确保将文件包含在 Jest 的 transformIgnorePatterns 中
- python - 如何使用二维数组的索引通过 matplotlib/seaborn 生成热图?
- regex - 如何使用正则表达式选择引号内的液体标签,但不一定要跟随/跟随引号
- python - 如何在 ubuntu 18.04 仿生上安装密码学?
- php - Select2 在 bootstrap 4 和 laravel 中不起作用