如何使幻灯片放映持续很长时间,然后再移至下一张幻灯片,以及如何将点移至幻灯片的左上方

时间:2019-03-29 18:10:13

标签: javascript html css

我有两个问题:

  1. 在继续进行下一个幻灯片放映之前,如何使幻灯片放映持续更长的时间,因为目前幻灯片放映仅持续约1秒钟。我搞砸了以下行

setTimeout(showSlides,2000); //每2秒更改图片

但是,所有操作都将其设置为从幻灯片到幻灯片的某种延迟。在我想要的位置上,它停留在1号幻灯片上,他们等了约10秒钟,然后移到2号幻灯片上。

  1. 当我将边距更改为负数时,如何将指示点移动到幻灯片的左上方,但这完全把点弄乱了。

如上所述,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>

这是幻灯片的屏幕截图,其中指示了我希望点所在的位置。

https://imgur.com/EiC4vSu

任何帮助表示感谢,并预先感谢您

2 个答案:

答案 0 :(得分:0)

将超时时间更改为10似乎可以在一张幻灯片上停留10秒钟。 setTimeout(showSlides,10000);

我知道您曾经说过您曾经尝试过。

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, 10000); // 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
  }
}
<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 :(得分:0)

问题1:

在触发下一张图片之前,将以下内容更改为“等待”

  

setTimeout(showSlides,5000); //每5秒钟更改图片

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

如果您希望图像之间的过渡具有一定的延迟,请更改淡入淡出动画级别

  

-webkit-animation-duration:2秒;

  

动画持续时间:2秒;

问题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>