滑块不能在多个div中工作

时间:2016-07-18 07:33:43

标签: javascript html css

我必须为四个不同的div部分调用滑块脚本(即)我的页面有四个滑块。它仅适用于第一个滑块。需要为所有滑块div部分重用该脚本。现在所有滑块都被视为单个滑块。



var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function currentDiv(n) {
  showDivs(slideIndex = n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("demo");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
     dots[i].className = dots[i].className.replace(" w3-red", "");
  }
  x[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " w3-red";
}
&#13;
.mySlides {display:none}
&#13;
<div class="w3-content" style="max-width:800px">

<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
</div>
</div>

<div class="w3-center">
  <div class="w3-section">
    <button class="w3-btn" onclick="plusDivs(-1)">Prev</button>
    <button class="w3-btn" onclick="plusDivs(1)">Next</button>
  </div>
  <button class="w3-btn demo" onclick="currentDiv(1)">1</button>
  <button class="w3-btn demo" onclick="currentDiv(2)">2</button>
  <button class="w3-btn demo" onclick="currentDiv(3)">3</button>
</div>

<div class="w3-content" style="max-width:800px">

<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
</div>
</div>

<div class="w3-center">
  <div class="w3-section">
    <button class="w3-btn" onclick="plusDivs(-1)">Prev</button>
    <button class="w3-btn" onclick="plusDivs(1)">Next</button>
  </div>
  <button class="w3-btn demo" onclick="currentDiv(1)">1</button>
  <button class="w3-btn demo" onclick="currentDiv(2)">2</button>
  <button class="w3-btn demo" onclick="currentDiv(3)">3</button>
</div>

<div class="w3-content" style="max-width:800px">

<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
       <img  src="image/cunsulting-marchandise.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-analytics.jpg" style="width:100%">
   </div>
</div>
<div class="mySlides">
   <div style="width:50%;float:left;">
       <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
   <div style="width:50%;float:right;">
      <img  src="image/cunsulting-store-solutions.jpg" style="width:100%">
   </div>
</div>
</div>

<div class="w3-center">
  <div class="w3-section">
    <button class="w3-btn" onclick="plusDivs(-1)">Prev</button>
    <button class="w3-btn" onclick="plusDivs(1)">Next</button>
  </div>
  <button class="w3-btn demo" onclick="currentDiv(1)">1</button>
  <button class="w3-btn demo" onclick="currentDiv(2)">2</button>
  <button class="w3-btn demo" onclick="currentDiv(3)">3</button>
</div>
<div class="w3-center">
  <div class="w3-section">
    <button class="w3-btn" onclick="plusDivs(-1)">Prev</button>
    <button class="w3-btn" onclick="plusDivs(1)">Next</button>
  </div>
  <button class="w3-btn demo" onclick="currentDiv(1)">1</button>
  <button class="w3-btn demo" onclick="currentDiv(2)">2</button>
  <button class="w3-btn demo" onclick="currentDiv(3)">3</button>
</div>
<div class="w3-center">
  <div class="w3-section">
    <button class="w3-btn" onclick="plusDivs(-1)">Prev</button>
    <button class="w3-btn" onclick="plusDivs(1)">Next</button>
  </div>
  <button class="w3-btn demo" onclick="currentDiv(1)">1</button>
  <button class="w3-btn demo" onclick="currentDiv(2)">2</button>
  <button class="w3-btn demo" onclick="currentDiv(3)">3</button>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我知道这会很丑陋,但复制一个新的例子:Myslide1是第一个滑块,第二个是Myslide2。因为你说Myslide的img应该改变....是的,有多个Myslides。