【问题标题】:How to display two slideshows on the same page?如何在同一页面上显示两个幻灯片?
【发布时间】:2021-02-17 03:26:02
【问题描述】:

我以这张幻灯片为例,效果很好。每个幻灯片之间都有共享类,这意味着当一个完成后才能开始下一个,我需要它们能够彼此独立工作,是否需要更改类。

这里只是一张幻灯片

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

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";
}
</script>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}

/* Slideshow container */
.slideshow-container {
  position: relative;
  background: #f1f1f1f1;
}

/* Slides */
.mySlides {
  display: none;
  padding: 80px;
  text-align: center;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -30px;
  padding: 16px;
  color: #888;
  font-weight: bold;
  font-size: 20px;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  position: absolute;
  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);
  color: white;
}

/* The dot/bullet/indicator container */
.dot-container {
    text-align: center;
    padding: 20px;
    background: #ddd;
}

/* 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;
}

/* Add a background color to the active dot/circle */
.active, .dot:hover {
  background-color: #717171;
}

/* Add an italic font style to all quotes */
q {font-style: italic;}

/* Add a blue color to the author */
.author {color: cornflowerblue;}
<div class="slideshow-container">

<div class="mySlides">
  <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
  <p class="author">- John Keats</p>
</div>

<div class="mySlides">
  <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
  <p class="author">- Ernest Hemingway</p>
</div>

<div class="mySlides">
  <q>I have not failed. I've just found 10,000 ways that won't work.</q>
  <p class="author">- Thomas A. Edison</p>
</div>

<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>

</div>

<div class="dot-container">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

当我添加第二个时,问题就来了,它们不能彼此独立工作

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

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";
}
</script>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}

/* Slideshow container */
.slideshow-container {
  position: relative;
  background: #f1f1f1f1;
}

/* Slides */
.mySlides {
  display: none;
  padding: 80px;
  text-align: center;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -30px;
  padding: 16px;
  color: #888;
  font-weight: bold;
  font-size: 20px;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  position: absolute;
  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);
  color: white;
}

/* The dot/bullet/indicator container */
.dot-container {
    text-align: center;
    padding: 20px;
    background: #ddd;
}

/* 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;
}

/* Add a background color to the active dot/circle */
.active, .dot:hover {
  background-color: #717171;
}

/* Add an italic font style to all quotes */
q {font-style: italic;}

/* Add a blue color to the author */
.author {color: cornflowerblue;}
<h1>Slideshow 1</h1>

<div class="slideshow-container">

<div class="mySlides">
  <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
  <p class="author">- John Keats</p>
</div>

<div class="mySlides">
  <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
  <p class="author">- Ernest Hemingway</p>
</div>

<div class="mySlides">
  <q>I have not failed. I've just found 10,000 ways that won't work.</q>
  <p class="author">- Thomas A. Edison</p>
</div>

<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>

</div>

<div class="dot-container">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

<h1>Slideshow 2</h1>
<div class="slideshow-container">

<div class="mySlides">
  <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
  <p class="author">- John Keats</p>
</div>

<div class="mySlides">
  <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
  <p class="author">- Ernest Hemingway</p>
</div>

<div class="mySlides">
  <q>I have not failed. I've just found 10,000 ways that won't work.</q>
  <p class="author">- Thomas A. Edison</p>
</div>

<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>

</div>

<div class="dot-container">
  <span class="dot" onclick="currentSlide(1)"></span> 
  <span class="dot" onclick="currentSlide(2)"></span> 
  <span class="dot" onclick="currentSlide(3)"></span> 
</div>

我宁愿脚本能够做到这一点,而不是必须更改每个类名并复制每个 CSS 类,这将如何仅在 Vanilla JS 中解决。循环第一个,然后循环第二个,如何独立处理?

提前致谢:)

【问题讨论】:

  • 不需要更改类,只需将逻辑本地化到每个.slide-container中的幻灯片。因此,与其抓取页面上的每张幻灯片,不如从第一个容器中抓取幻灯片并将其与第二个容器分开处理。应该只是一些选择器更改和函数调用差异。
  • 这听起来比复制更容易,但我不知道你会怎么做@RandyCasburn
  • prezlehammer 为您提供了一个很好的答案。您应该考虑支持该答案(就像我所做的那样)并接受它。

标签: javascript html css animation carousel


【解决方案1】:

我不知道我是否应该这样做...但是这里是面向对象的做事风格...

  1. 所有幻灯片创建单个
  2. 为每张幻灯片创建实例
  3. 为您想要的实例调用函数.. 我在您的 html 中编辑的内容是对 Instances(or objects) 的 onclick 调用,我将幻灯片和点都包装在一个部分 div 中。..

现在你有两个部分的幻灯片(幻灯片+点)独立工作..

let slide1 = new CreateSlide(0);
let slide2 = new CreateSlide(1);

function CreateSlide(index) {
    this.slideContainer = document.getElementsByClassName("section")[index];
    this.slideIndex = 1;

    this.plusSlides = function(n) {
        this.showSlides(this.slideIndex += n);
    };
    this.currentSlide = function(n) {
        this.showSlides(this.slideIndex = n);
    };
    this.showSlides = function(n) {
        var i;
        var slides = this.slideContainer.getElementsByClassName("mySlides");
        var dots = this.slideContainer.getElementsByClassName("dot");
        if (n > slides.length) {
            this.slideIndex = 1
        }
        if (n < 1) {
            this.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[this.slideIndex - 1].style.display = "block";
        dots[this.slideIndex - 1].className += " active";
    }
    this.showSlides(1);
}
* {
    box-sizing: border-box
}
body {
    font-family: Verdana, sans-serif;
    margin: 0
}
/* Slideshow container */
.slideshow-container {
    position: relative;
    background: #f1f1f1f1;
 }
 /* Slides */
        
 .mySlides {
     display: none;
     padding: 80px;
     text-align: center;
 }
 /* Next & previous buttons */
        
 .prev, .next {
     cursor: pointer;
     position: absolute;
     top: 50%;
     width: auto;
     margin-top: -30px;
     padding: 16px;
     color: #888;
     font-weight: bold;
     font-size: 20px;
     border-radius: 0 3px 3px 0;
     user-select: none;
 }
 /* Position the "next button" to the right */
        
 .next {
     position: absolute;
     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);
     color: white;
  }
  /* The dot/bullet/indicator container */
        
  .dot-container {
      text-align: center;
      padding: 20px;
      background: #ddd;
  }
  /* 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;
  }
  /* Add a background color to the active dot/circle */
        
  .active, .dot:hover {
      background-color: #717171;
  }
  /* Add an italic font style to all quotes */
        
  q {
      font-style: italic;
  }
  /* Add a blue color to the author */
        
  .author {
      color: cornflowerblue;
  }
    <h1>Slideshow 1</h1>
    <div class="section">
        <div class="slideshow-container">
            <div class="mySlides">
                <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
                <p class="author">- John Keats</p>
            </div>
            <div class="mySlides">
                <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
                <p class="author">- Ernest Hemingway</p>
            </div>
            <div class="mySlides">
                <q>I have not failed. I've just found 10,000 ways that won't work.</q>
                <p class="author">- Thomas A. Edison</p>
            </div>
            <a class="prev" onclick="slide1.plusSlides(-1)">❮</a>
            <a class="next" onclick="slide1.plusSlides(1)">❯</a>
        </div>
        <div class="dot-container">
            <span class="dot" onclick="slide1.currentSlide(1)"></span>
            <span class="dot" onclick="slide1.currentSlide(2)"></span>
            <span class="dot" onclick="slide1.currentSlide(3)"></span>
        </div>
    </div>

    <div class="section">
        <h1>Slideshow 2</h1>
        <div class="slideshow-container">
            <div class="mySlides">
                <q>I love you the more in that I believe you had liked me for my own sake and for nothing else</q>
                <p class="author">- John Keats</p>
            </div>
            <div class="mySlides">
                <q>But man is not made for defeat. A man can be destroyed but not defeated.</q>
                <p class="author">- Ernest Hemingway</p>
            </div>
            <div class="mySlides">
                <q>I have not failed. I've just found 10,000 ways that won't work.</q>
                <p class="author">- Thomas A. Edison</p>
            </div>
            <a class="prev" onclick="slide2.plusSlides(-1)">❮</a>
            <a class="next" onclick="slide2.plusSlides(1)">❯</a>
        </div>
        <div class="dot-container">
            <span class="dot" onclick="slide2.currentSlide(1)"></span>
            <span class="dot" onclick="slide2.currentSlide(2)"></span>
            <span class="dot" onclick="slide2.currentSlide(3)"></span>
        </div>
    </div>

【讨论】:

  • 这确实有效,但我对它的理解不足以与我所拥有的集成,我对 JS 完全陌生,我只能在 HTML 和 CSS 完成后更改脚本,我'如果你能看一下,我已经用我所拥有的所有内容更新了我的问题:)
  • 第一个循环通过,第二个循环通过..?相继??那是独立的吗???你是什​​么意思??无论如何,我还没有在这里发明一些抽象的东西......只是类......你必须以某种方式学习它们......为什么不现在呢? :D..
  • 实际的解释很难应用于你已经拥有的代码,完全重写它不是一个真正的选择。请看here,它更清晰。
  • 如果只使用 JS,也许你可以这样做... preventDefault 为每个 prev 和 next btns 和点 btns 点击... 并将上面的代码添加到您的 js... 在创建每个像上面这样的幻灯片(你不必理解实现)将 onclick 操作添加到它们的每个 prev next dot btns
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-04
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多