当前位置: 首页 > 知识库问答 >
问题:

导致幻灯片放映消失的上一个按钮

姬雪松
2023-03-14

我拍摄了一个由用户Sinisake编辑的简单的w3学校幻灯片,编辑了样式以删除幻灯片计数器和标题,编辑了prev和next按钮以占据幻灯片每边的一半,光标改变为点击。

唯一的问题是,当您在第一张幻灯片上单击“previousElementSibling”时,previousElementSibling调用了一个不在那里的元素,整个幻灯片将消失。我知道这是因为我改变了前一个和下一个位置,但我不知道为什么。

另外,我用display:none隐藏了这些点,但是如果我从html和javascript中删除它们,幻灯片就会中断。

我对CSS和html略有了解,但对Javascript还是个新手,所以我很感谢大家的耐心和帮助。

我的代码在下面,页面也托管在这里:http://dominicfortunato.com/template%20studio%20site/index-updated-slideshow-2.html

null

(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') {

 	 	if (curr_slide.previousElementSibling.classList.contains('mySli‌​des')) {
          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 {
	z-index: 300;
	max-width: 80%;
	float: center;
	text-align: left;
	position: relative;
	margin-right: auto;
	margin-left: auto;
	padding-bottom: 2%;
	font-family:"GT Walsheim", Calibri, sans-serif;
}

.project-description {
	width: 100%;
	font-size: 36px;
	margin-left: 20px;
	font-family:"GT Walsheim", Calibri, sans-serif;
	display: inline;
}

.project-title {
	width: 100%;
	display: inline;
	margin-left: 150px;
	text-decoration: underline;
	font-size: 36px; 
}
<div class="slideshow-container">
  <a class="prev" ></a>
  <a class="next"></a>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_nature_wide.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="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
  </div>

  <div class="mySlides fade">
    <img src="http://www.w3schools.com/howto/img_nature_wide.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">
	<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>

null

共有1个答案

章光华
2023-03-14

这对我起作用:(你可能忘记了第二个滑块中的一个点)。查看这个小提琴:https://jsfidle.net/prtuxoxx/

<div class="slideshow-container">
        <a class="prev" ></a>
        <a class="next"></a>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_nature_wide.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="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
        </div>

        <div class="mySlides fade">
          <img src="http://www.w3schools.com/howto/img_nature_wide.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');
            }

          }

        }

      }
    })();
 类似资料:
  • 我制作了一个jquery幻灯片,下面是代码: HTML CSS jQuery “下一个”按钮工作,但当我点击“上一个”图像消失!有人能帮我吗? 这是小提琴。

  • 我正在尝试用Java编写一个幻灯片放映程序,并希望使实现尽可能简单。 目标是显示一系列幻灯片,每个幻灯片都有导航按钮,此外还有依赖于幻灯片内容的其他按钮。(显示文本的幻灯片将有,而带有图像的幻灯片将没有此按钮。)

  • 有没有办法把一个幻灯片从一个幻灯片完全复制到另一个幻灯片,包括所有的方框位置,颜色,字体等等。? 参考食谱,我有下面 然而,背景与“from”pptx不匹配,并且一些文本框被移动了。我猜颜色是因为我没有修改母版,而移动是因为与锚相关的东西。我也尝试了我认为会迫使一张幻灯片的母版转移到另一张幻灯片上的方法,但它的作用与上面的相同。

  • 播放幻灯片秀 自动依顺序显示每张图像。 选择频道或图像时按下按钮,再选择选项选单的[幻灯片秀]。 提示 显示图像时若按下START(开始)按钮,即会开始幻灯片秀。 幻灯片秀可与音乐同时播放。详细内容请参阅(相片) > [幻灯片秀] > [同时播放音乐与幻灯片秀]。

  • 播放幻灯片秀 自动依顺序显示每张图像。选择图标并按下START(开始)按钮后,即会开始播放幻灯片秀。显示之内容可能会因选择之图示而异。 提示 亦可透过操作接口或选项选单,启动幻灯片秀。 亦可进入(相片) > (相片设定),改变显示图像的速度。

  • 问题内容: 首先,我知道在回答不能解决我的问题之前就已经问过这个问题了,所以我想再问一遍:我使用了“ W3school”的幻灯片代码,该代码提供了很好的动画Jquery幻灯片。不幸的是,我的页面上需要多个,第二个页面使第一个页面无法正常工作。甚至在这里找到此解决方案之前,我都尝试过更改不同幻灯片对象的CSS和HTML名称,从而成功地解决了一部分问题(第二个未显示),但是如前所述,第一个问题停止了工