作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个问题:
setTimeout(showSlides, 2000);//每 2 秒更换一次图像
但所做的只是将其设置为从一张幻灯片移动到另一张幻灯片的某种延迟。在我想要的位置,它停留在 1 号幻灯片上,他们等待大约 10 秒左右,然后移到 2 号幻灯片上。
如上所述,IO 更改了以下行:
setTimeout(showSlides, 2000);//每 2 秒更换一次图像
并更改边距,但什么也没有。
以下代码适用于 Q1 和 2。
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
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";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
* {
box-sizing: border-box;
}
body {
font-family: Verdana, sans-serif;
}
.mySlides {
display: none;
}
img {
vertical-align: middle;
}
/* Slideshow container */
.slideshow-container {
max-width: 100%;
position: relative;
margin: auto;
padding-left: 25px;
padding-right: 25px;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: laft;
background-color: #000;
width: 150px;
margin-left: 15px;
}
/* The dots/bullets/indicators */
.dot {
height: 15px;
width: 15px;
margin: 0px 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
float: left;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {
font-size: 11px
}
}
My HTML Is:
<div class="slideshow-container">
<div class="mySlides fade">
<img src="img/1.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<img src="img/1.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<img src="img/1.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
</div>
<br>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
这是幻灯片的屏幕截图,其中标明了我希望圆点所在的位置。
感谢任何帮助并提前感谢您
最佳答案
问题 1:
在触发下一张图片之前将下面的更改为“等待”
setTimeout(showSlides, 5000); // Change image every 5 seconds
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
如果您希望图像之间的过渡有一些延迟,请更改淡入淡出动画级别
-webkit-animation-duration: 2s;
和
animation-duration: 2s;
问题 2:
只需将您的点 div 移动到容器 div 之前
var slideIndex = 0;
showSlides();
function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1
}
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";
setTimeout(showSlides, 5000); // Change image every 2 seconds
}
* {
box-sizing: border-box;
}
body {
font-family: Verdana, sans-serif;
}
.mySlides {
display: none;
}
img {
vertical-align: middle;
}
/* Slideshow container */
.slideshow-container {
max-width: 100%;
position: relative;
margin: auto;
padding-left: 25px;
padding-right: 25px;
}
/* Caption text */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: laft;
background-color: #000;
width: 150px;
margin-left: 15px;
}
/* The dots/bullets/indicators */
.dot {
height: 15px;
width: 15px;
margin: 0px 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
float: left;
}
.active {
background-color: #717171;
}
/* Fading animation */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
@-webkit-keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
@keyframes fade {
from {
opacity: .4
}
to {
opacity: 1
}
}
/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
.text {
font-size: 11px
}
}
My HTML Is:
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
<div class="slideshow-container">
<div class="mySlides fade">
<img src="https://www.mediawiki.org/static/images/project-logos/mediawikiwiki.png" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<img src="https://www.mediawiki.org/static/images/project-logos/mediawikiwiki.png" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<img src="https://www.mediawiki.org/static/images/project-logos/mediawikiwiki.png" style="width:100%">
<div class="text">Caption Three</div>
</div>
</div>
关于javascript - 如何让幻灯片在移动到下一张幻灯片之前持续很长时间,以及如何将圆点向上移动到幻灯片的左上角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423319/
我是一名优秀的程序员,十分优秀!