我希望幻灯片放映按钮如何运行的屏幕截图:
我正在尝试为我的幻灯片创建前进和后退按钮,这些按钮是不可见的并且跨越图像到页面边缘。但是我不知道该怎么做。
我尝试使用 flexbox,父元素是图像容器,子元素是按钮。然而,每次我插入这些属性时,按钮都消失了,我再也无法点击或找到按钮了。
这是我的 html:
<!-- Slideshow container -->
<div class="slideshow-container">
<!-- Full-width images with number and caption text -->
<div class="mySlides">
<div class="numbertext">1 / 3</div>
<img src="img/3.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides">
<div class="numbertext">2 / 3</div>
<img src="img/30.JPG" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides">
<div class="numbertext">3 / 3</div>
<img src="img/39.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<!-- Next and previous buttons -->
<div class="prev" onclick="plusSlides(-1)"></div>
<div class="next" onclick="plusSlides(1)"></div>
</div>
这是我的 CSS:
* {box-sizing:border-box}
/* Slideshow container */
.slideshow-container {
max-width: 30%;
max-height: 30%;
position: relative;
margin: auto;
margin-top: 20vh;
margin-bottom: 20vh;
justify-content: space-between;
display: flex;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next & previous buttons */
.prev, .next {
cursor: pointer;
flex: 1;
user-select: none;
}
这是我的 JS:
let 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) {
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";
}
尝试使用
position: absolute;
作为按钮,我做了一个例子如下:
let 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) {
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}
/* Slideshow container */
.slideshow-container {
max-width: 30%;
max-height: 30%;
position: relative;
margin: auto;
margin-top: 20vh;
margin-bottom: 20vh;
justify-content: space-between;
display: flex;
}
/* Hide the images by default */
.mySlides {
display: none;
}
/* Next and previous buttons */
.prev, .next {
position: absolute;
top: 0;
bottom: 0;
z-index: 1;
cursor: pointer;
width: 50%;
height: 100%;
background-color: transparent;
}
.prev {
left: 0;
}
.next {
right: 0;
}
/* Hide button elements by default */
.prev, .next {
opacity: 0;
transition: opacity 0.3s ease;
}
/* Show button elements on hover */
.slideshow-container:hover .prev, .slideshow-container:hover .next {
opacity: 1;
}
<!-- Slideshow container -->
<div class="slideshow-container">
<!-- Full-width images with number and caption text -->
<div class="mySlides">
<div class="numbertext">1 / 3</div>
<img src="img/3.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides">
<div class="numbertext">2 / 3</div>
<img src="img/30.JPG" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides">
<div class="numbertext">3 / 3</div>
<img src="img/39.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<!-- Next and previous buttons -->
<div class="prev" onclick="plusSlides(-1)"></div>
<div class="next" onclick="plusSlides(1)"></div>
</div>
我想这就是你想要的,但提醒你,你的JS代码中的点似乎是不必要的,它会导致错误。我建议你删除这些代码。
你只需要在 JS 点变量中使用
mySlides
类而不是 dot
。
并且在 css 中有一些更正,如下所述:
请参考此链接进行现场演示以了解详细信息。 https://jsfiddle.net/76L4phfc/
.prev,
.next {
background: red;
opacity: 0.2;
z-index: 55555;
width: 48%;
height: 100%;
display: block;
position: absolute;
}
.next { right: 0;}
.prev { left: 0;}