gpt4 book ai didi

javascript - slider 在多个 div 中不起作用

转载 作者:行者123 更新时间:2023-11-28 05:32:38 25 4
gpt4 key购买 nike

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

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";
}
.mySlides {display:none}
<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>

最佳答案

我知道这会很难看,但请将所有内容复制到一个新的,例如:Myslide1 是第一个 slider ,第二个是 Myslide2。因为你说 Myslide 的 img 应该改变....是的,有多个 Myslide。

关于javascript - slider 在多个 div 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com