【问题标题】:Add a Carousel Slideshow gallery添加轮播幻灯片库
【发布时间】:2021-09-15 08:03:15
【问题描述】:

我正在学习 HTML、CSS 和 Java。 我正在尝试向我的网站添加幻灯片,但我不明白为什么当我尝试运行它时什么都看不到。我找到了这个,我正在尝试让它工作,但我看不到图像,我只能看到下一步或返回的指针。

    <!-- Slideshow container -->
<div class="slideshow-container">

    <!-- Full-width images with number and caption text -->
    <div class="mySlides fade">
      <div class="numbertext">1 / 9</div>
      <img src="FotoGP.jpg" style="width:100%">
      
    </div>
  
    <div class="mySlides fade">
      <div class="numbertext">2 / 9</div>
      <img src="FotoSalaPesi.jpg" style="width:100%">
      <div class="text">Sala Pesi</div>
    </div>
  
    <div class="mySlides fade">
      <div class="numbertext">3 / 9</div>
      <img src="FotoSalaCardio.jpg" style="width:100%">
      <div class="text">Cardio</div>
    </div>

    <div class="mySlides fade">
        <div class="numbertext">4 / 9</div>
        <img src="FotoSalaCircuiti.jpg" style="width:100%">
        <div class="text">Circuiti Funzionali</div>
      </div>
  
      <div class="mySlides fade">
        <div class="numbertext">5 / 9</div>
        <img src="FotoSalaAddominali.jpg" style="width:100%">
      </div>

      <div class="mySlides fade">
        <div class="numbertext">6 / 9</div>
        <img src="FotoSalaGambe.jpg" style="width:100%">
      </div>

      <div class="mySlides fade">
        <div class="numbertext">7 / 9</div>
        <img src="FotoSalaBilanceri.jpg" style="width:100%">
        <div class="text">Sala Pesi</div>
      </div>

      <div class="mySlides fade">
        <div class="numbertext">8 / 9</div>
        <img src="FotoAperto.jpg" style="width:100%">
        <div class="text">All'aperto</div>
      </div>

      <div class="mySlides fade">
        <div class="numbertext">9 / 9</div>
        <img src="FotoUfficio.jpg" style="width:100%">
      </div>


    <!-- Next and previous buttons -->
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
  </div>
  <br>
  
  <!-- The dots/circles -->
  <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>
    <span class="dot" onclick="currentSlide(4)"></span>
    <span class="dot" onclick="currentSlide(5)"></span>
    <span class="dot" onclick="currentSlide(6)"></span>
    <span class="dot" onclick="currentSlide(7)"></span>
    <span class="dot" onclick="currentSlide(8)"></span>
    <span class="dot" onclick="currentSlide(9)"></span>
  </div>

这是 CSS 部分

    * {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
  max-width: 100%;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.mySlides {
  display: none;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  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;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* 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: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  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}
}

还有 JS

    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";
}


var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}
  slides[slideIndex-1].style.display = "block";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}

我错过了什么?感谢您的帮助!

【问题讨论】:

  • 您的代码没有问题,只需检查图像的路径

标签: javascript html jquery css carousel


【解决方案1】:

你的图片路径好像不对

看看它与占位符一起使用

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";
}


var slideIndex = 0;
showSlides();

function showSlides() {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  slideIndex++;
  if (slideIndex > slides.length) {
    slideIndex = 1
  }
  slides[slideIndex - 1].style.display = "block";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}
* {
  box-sizing: border-box
}


/* Slideshow container */

.slideshow-container {
  max-width: 100%;
  position: relative;
  margin: auto;
}


/* Hide the images by default */

.mySlides {
  display: none;
}


/* Next & previous buttons */

.prev,
.next {
  cursor: pointer;
  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;
  user-select: none;
}


/* Position the "next button" to the right */

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}


/* On hover, add a black background color with a little bit see-through */

.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}


/* 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: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active,
.dot:hover {
  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
  }
}
<div class="slideshow-container">

  <!-- Full-width images with number and caption text -->
  <div class="mySlides fade">
    <div class="numbertext">1 / 9</div>
    <img src="https://via.placeholder.com/150" style="width:100%">

  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 9</div>
    <img src="https://via.placeholder.com/150" style="width:100%">
    <div class="text">Sala Pesi</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 9</div>
    <img src="https://via.placeholder.com/150" style="width:100%">
    <div class="text">Cardio</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">4 / 9</div>
    <img src="https://via.placeholder.com/150" style="width:100%">
    <div class="text">Circuiti Funzionali</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">5 / 9</div>
    <img src="https://via.placeholder.com/150" style="width:100%">
  </div>

  <div class="mySlides fade">
    <div class="numbertext">6 / 9</div>
    <img src="FotoSalaGambe.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <div class="numbertext">7 / 9</div>
    <img src="FotoSalaBilanceri.jpg" style="width:100%">
    <div class="text">Sala Pesi</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">8 / 9</div>
    <img src="FotoAperto.jpg" style="width:100%">
    <div class="text">All'aperto</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">9 / 9</div>
    <img src="FotoUfficio.jpg" style="width:100%">
  </div>


  <!-- Next and previous buttons -->
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>

<!-- The dots/circles -->
<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>
  <span class="dot" onclick="currentSlide(4)"></span>
  <span class="dot" onclick="currentSlide(5)"></span>
  <span class="dot" onclick="currentSlide(6)"></span>
  <span class="dot" onclick="currentSlide(7)"></span>
  <span class="dot" onclick="currentSlide(8)"></span>
  <span class="dot" onclick="currentSlide(9)"></span>
</div>

【讨论】:

  • 你说得对,我犯了一些错误!非常感谢您回答我!
【解决方案2】:

我不知道您对 HTML 有多陌生,但这里有一些常见的修复方法:

  • 请务必仔细检查您的图片来源是否正确。如果您在另一个目录中,请包含图像的相对路径。例如:&lt;img src="images/FotoAperto.jpg"&gt;

  • 确保您的 css 导入正确。 &lt;link rel="stylesheet" href="style.css"&gt;

  • 检查你的JS脚本是否导入正确,并把&lt;script&gt;标签放在结束&lt;/body&gt;-标签&lt;script src="index.js"&gt;&lt;/script&gt;之后。

希望对你有帮助:)

【讨论】:

  • 是的,我的 JS 脚本没有以正确的方式导入。非常感谢!如果可以的话,我会再问你一个问题。现在幻灯片可以工作了,但我不能像我想要的那样设置时间。默认值为 1 秒,照片切换没问题,如果我在运行代码时设置为 5 秒,我可以看到 1 秒的照片和 4 秒的白屏,然后切换到下一张照片。我该如何解决?
  • 很高兴我能帮上忙!随时将我的答案标记为已接受的解决方案。 :) 关于您的下一个问题,我尝试将 setTimeout(showSlides, 5000); 更改为 5000,这对我来说符合预期。幻灯片和图像每 5 秒更换一次。
猜你喜欢
  • 1970-01-01
  • 2016-10-27
  • 2018-01-24
  • 2019-12-02
  • 2021-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
相关资源
最近更新 更多