上一个按钮导致幻灯片消失
我已经采取了一个简单的w3学校幻灯片,编辑here由用户Sinisake和编辑样式删除幻灯片计数器和标题,并编辑prev和下一个按钮,占用每一半幻灯片的一侧,光标改变以指示点击。上一个按钮导致幻灯片消失
唯一的问题是,当您单击第一张幻灯片上的“上一张”时,previousElementSibling会调用不存在的元素,整个幻灯片将消失。我知道这是因为我改变了前一个和下一个位置,但我不知道为什么。
此外,我隐藏点:无,但如果我从HTML和JavaScript中删除它们,幻灯片放映。
我知道一些关于CSS和html的知识,但在JavaScript中很新,所以我很感谢大家的耐心和帮助。
我的代码的下方,并且页面也在这里举办:http://dominicfortunato.com/Template%20Studio%20Site/index-updated-slideshow-2.html
(function() { init(); //on page load - show first slide, hidethe rest
function init() {
parents = document.getElementsByClassName('slideshow-container');
for (j = 0; j < parents.length; j++) {
var slides = parents[j].getElementsByClassName("mySlides");
var dots = parents[j].getElementsByClassName("dot");
slides[0].classList.add('active-slide');
dots[0].classList.add('active');
}
}
dots = document.getElementsByClassName('dot'); //dots functionality
for (i = 0; i < dots.length; i++) {
dots[i].onclick = function() {
slides = this.parentNode.parentNode.getElementsByClassName("mySlides");
for (j = 0; j < this.parentNode.children.length; j++) {
this.parentNode.children[j].classList.remove('active');
slides[j].classList.remove('active-slide');
if (this.parentNode.children[j] == this) {
index = j;
}
}
this.classList.add('active');
slides[index].classList.add('active-slide');
}
}
//prev/next functionality
links = document.querySelectorAll('.slideshow-container a');
for (i = 0; i < links.length; i++) {
links[i].onclick = function() {
current = this.parentNode;
var slides = current.getElementsByClassName("mySlides");
var dots = current.getElementsByClassName("dot");
curr_slide = current.getElementsByClassName('active-slide')[0];
curr_dot = current.getElementsByClassName('active')[0];
curr_slide.classList.remove('active-slide');
curr_dot.classList.remove('active');
if (this.className == 'next') {
if (curr_slide.nextElementSibling.classList.contains('mySlides')) {
curr_slide.nextElementSibling.classList.add('active-slide');
curr_dot.nextElementSibling.classList.add('active');
} else {
slides[0].classList.add('active-slide');
dots[0].classList.add('active');
}
}
if (this.className == 'prev') {
\t \t if (curr_slide.previousElementSibling.classList.contains('mySlides')) {
curr_slide.previousElementSibling.classList.add('active-slide');
curr_dot.previousElementSibling.classList.add('active');
} else {
slides[slides.length - 1].classList.add('active-slide');
dots[slides.length - 1].classList.add('active');
}
}
}
}
})();
/* Slideshow container */ .slideshow-container {
max-width: 80%;
position: relative;
padding-top: 3%;
padding-bottom: 2%;
margin: auto;
}
/* Next & previous buttons */
.prev, .next {
cursor: url('Images/arrow.png'), w-resize;
float: left;
position: absolute;
width: 50%;
height: 90%;
margin-right: auto;
margin-left: auto;
transition: 0.3s ease;
border-radius: 0 0px 0px 0;
}
/* Position the "next button" to the right */
.next {
cursor: url('Images/arrow.png'), e-resize;
right: 0;
border-radius: 0px 0 0 0px;
}
/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
background-color: rgba(0,0,0,0);
}
.mySlides {
display:none;
}
.active-slide {
display:block;
}
.active-slide-1 {
display:block;
}
.dot {
cursor:pointer;
height: 13px;
width: 13px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: none;
transition: background-color 0.6s ease;
}
.active, .dot:hover {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 0.2s;
animation-name: fade;
animation-duration: 0.2s;
}
@-webkit-keyframes fade {
from {opacity: .1}
to {opacity: 1}
}
@keyframes fade {
from {opacity: .4}
to {opacity: 1}
}
.text-container {
\t z-index: 300;
\t max-width: 80%;
\t float: center;
\t text-align: left;
\t position: relative;
\t margin-right: auto;
\t margin-left: auto;
\t padding-bottom: 2%;
\t font-family:"GT Walsheim", Calibri, sans-serif;
}
.project-description {
\t width: 100%;
\t font-size: 36px;
\t margin-left: 20px;
\t font-family:"GT Walsheim", Calibri, sans-serif;
\t display: inline;
}
.project-title {
\t width: 100%;
\t display: inline;
\t margin-left: 150px;
\t text-decoration: underline;
\t font-size: 36px;
}
<div class="slideshow-container"> <a class="prev" ></a>
<a class="next"></a>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/185154040.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/186154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/187154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="http://placehold.it/1000x350" style="width:100%">
</div>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="text-container">
\t <div class="project-title">Rust Belt Riders</div>
\t <div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
</div>
<br>
<div class="slideshow-container">
<a class="prev" ></a>
<a class="next"></a>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/185154040.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/186154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/187154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="http://placehold.it/1000x350" style="width:100%">
</div>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="text-container">
\t <div class="project-title">Rust Belt Riders</div>
\t <div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
</div>
<br>
回答:
这个工作对我来说:(你可能已经忘记了在第二滑点)。看看这个小提琴:https://jsfiddle.net/prtuxoxx/
<div class="slideshow-container"> <a class="prev" ></a>
<a class="next"></a>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/185154040.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/186154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/187154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="http://placehold.it/1000x350" style="width:100%">
</div>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="text-container">
<div class="project-title">Rust Belt Riders</div>
<div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
</div>
<br>
<div class="slideshow-container">
<a class="prev" ></a>
<a class="next"></a>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/185154040.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/186154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="/wp-content/uploads/new2022/20220327voicc/187154041.jpg" style="width:100%">
</div>
<div class="mySlides fade">
<img src="http://placehold.it/1000x350" style="width:100%">
</div>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<div class="text-container">
<div class="project-title">Rust Belt Riders</div>
<div class="project-description">Identity for Rust Belt Riders, a waste management and composting company from Cleveland. </div>
</div>
和JS:
(function() { init(); //on page load - show first slide, hidethe rest
function init() {
parents = document.getElementsByClassName('slideshow-container');
for (j = 0; j < parents.length; j++) {
var slides = parents[j].getElementsByClassName("mySlides");
var dots = parents[j].getElementsByClassName("dot");
slides[0].classList.add('active-slide');
dots[0].classList.add('active');
}
}
dots = document.getElementsByClassName('dot'); //dots functionality
for (i = 0; i < dots.length; i++) {
dots[i].onclick = function() {
slides = this.parentNode.parentNode.getElementsByClassName("mySlides");
for (j = 0; j < this.parentNode.children.length; j++) {
this.parentNode.children[j].classList.remove('active');
slides[j].classList.remove('active-slide');
if (this.parentNode.children[j] == this) {
index = j;
}
}
this.classList.add('active');
slides[index].classList.add('active-slide');
}
}
//prev/next functionality
links = document.querySelectorAll('.slideshow-container a');
for (i = 0; i < links.length; i++) {
links[i].onclick = function() {
current = this.parentNode;
var slides = current.getElementsByClassName("mySlides");
var dots = current.getElementsByClassName("dot");
curr_slide = current.getElementsByClassName('active-slide')[0];
curr_dot = current.getElementsByClassName('active')[0];
curr_slide.classList.remove('active-slide');
curr_dot.classList.remove('active');
if (this.className == 'next') {
console.log('next ', curr_slide);
if (curr_slide.nextElementSibling.classList.contains('mySlides')) {
curr_slide.nextElementSibling.classList.add('active-slide');
curr_dot.nextElementSibling.classList.add('active');
} else {
slides[0].classList.add('active-slide');
dots[0].classList.add('active');
}
}
if (this.className == 'prev') {
console.log('previous ', curr_slide);
if (curr_slide.previousElementSibling.classList.contains('mySlides')) {
curr_slide.previousElementSibling.classList.add('active-slide');
curr_dot.previousElementSibling.classList.add('active');
} else {
slides[slides.length - 1].classList.add('active-slide');
dots[slides.length - 1].classList.add('active');
}
}
}
}
})();
以上是 上一个按钮导致幻灯片消失 的全部内容, 来源链接: utcz.com/qa/257965.html