首先,我知道这个问题在答案没有解决我的问题之前被问过,所以我想再问一遍: 我使用了来自" W3school"的幻灯片代码。它提供了一个很好的动画Jquery幻灯片。不幸的是,我的页面需要不止一个,第二个停止第一个工作。甚至在此之前找到这个解决方案之前,我尝试更改不同幻灯片的对象的CSS和HTML名称,这些问题成功解决了部分问题(第二个没有显示),但如前所述,阻止了第一个工作。幻灯片显示在这里,但点击箭头不会做任何事情。 我稍微了解问题是什么,但无法修复。 以下是我修改第二个代码的方法:
var slideIndex = 7;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides2");
var dots = document.getElementsByClassName("dot2");
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(" active2", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active2";
}
如你所见,我刚刚添加了&#39; 2&#39;对于Jquery和CSS中的每种类型的对象。我想我必须给那些繁殖的命令赋予不同的名称,但是如何做呢?
修改 https://jsfiddle.net/mgb7k239/ 这是Jsfiddle中的一个例子,但我不明白为什么这里第一个甚至不是幻灯片,第二个也不起作用:/它在我的电脑上呢!我忘了在电脑上说,点击第一张幻灯片上的箭头改变第二张照片上的图片! 我用互联网上随机的图片替换了图片。
答案 0 :(得分:2)
他们的代码中的主要问题在于它是制作好的......对于一个幻灯片显示,当您尝试将其应用于多个幻灯片时,会出现一些严重的问题,正如您所经历的那样。他们使用全局变量(index,n),当你有一个幻灯片放映时没有问题,但有更多的幻灯片 - 难以处理的东西,并且很难将引用到当前幻灯片div 概念。
而且,是的,这是简单的javascript幻灯片,这里没有jQuery。我稍微改变了你的HTML和CSS(点div是INSIDE幻灯片div,只是为了更容易定位),而且,我已经删除了内联事件处理程序,只是为了让事情变得更容易和更清洁。
这可以通过多种方式解决,我选择了这个:
(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) {
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: 1000px;
position: relative;
margin: auto;
}
/* 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;
}
/* 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:20px;
width: 100%;
text-align: center;
}
.mySlides {
display:none;
}
.active-slide {
display:block;
}
/* 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: 13px;
width: 13px;
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">
<div class="mySlides fade">
<div class="numbertext">1 / 4</div>
<img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 4</div>
<img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 4</div>
<img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<div class="mySlides fade">
<div class="numbertext">4 / 4</div>
<img src="http://placehold.it/1000x350" style="width:100%">
<div class="text">Caption Four</div>
</div>
<a class="prev">❮</a>
<a class="next">❯</a>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot" ></span>
<span class="dot" ></span>
<span class="dot"></span>
</div>
</div>
<br>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
<div class="text">Caption Text</div>
</div>
<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>
<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>
<a class="prev" >❮</a>
<a class="next">❯</a>
<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>
<br>
因此,如果您保留幻灯片div的当前HTML结构,这应该有效,并且您可以无限制地播放独立幻灯片。这是非常老式的js,应该很清楚,如果事情不清楚,我可以解释更多......或者试着更好地解释。 :)
答案 1 :(得分:0)
我在这里创建了一个解决方案:
var sliderObjects = [];
createSliderObjects();
function plusDivs(obj, n) {
var parentDiv = $(obj).parent();
var matchedDiv;
$.each(sliderObjects, function(i, item) {
if ($(parentDiv[0]).attr('id') == $(item).attr('id')) {
matchedDiv = item;
return false;
}
});
matchedDiv.slideIndex=matchedDiv.slideIndex+n;
showDivs(matchedDiv, matchedDiv.slideIndex);
}
function createSliderObjects() {
var sliderDivs = $('.slider');
$.each(sliderDivs, function(i, item) {
var obj = {};
obj.id = $(item).attr('id');
obj.divContent = item;
obj.slideIndex = 1;
obj.slideContents = $(item).find('.mySlides');
showDivs(obj, 1);
sliderObjects.push(obj);
});
}
function showDivs(divObject, n) {
debugger;
var i;
if (n > divObject.slideContents.length) {
divObject.slideIndex = 1
}
if (n < 1) {
divObject.slideIndex = divObject.slideContents.length
}
for (i = 0; i < divObject.slideContents.length; i++) {
divObject.slideContents[i].style.display = "none";
}
divObject.slideContents[divObject.slideIndex - 1].style.display = "block";
}
<link href="http://www.w3schools.com/lib/w3.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="w3-center">Manual Slideshow</h2>
<div class="w3-content w3-display-container slider" id="div1">
<img class="mySlides" src="https://i.imgur.com/eLarayS.jpg" style="width:100%">
<img class="mySlides" src="https://i.imgur.com/xpOiMWh.jpg" style="width:100%">
<img class="mySlides" src="https://i.imgur.com/lgcC8Y5.jpg" style="width:100%">
<img class="mySlides" src="http://i.imgur.com/ufmiVTQ.jpg" style="width:100%">
<a class="w3-btn-floating w3-display-left" onclick="plusDivs(this,-1)">❮</a>
<a class="w3-btn-floating w3-display-right" onclick="plusDivs(this,1)">❯</a>
</div>
<div class="w3-content w3-display-container slider" id="div2">
<img class="mySlides" src="https://i.imgur.com/eLarayS.jpg" style="width:100%">
<img class="mySlides" src="https://i.imgur.com/xpOiMWh.jpg" style="width:100%">
<img class="mySlides" src="https://i.imgur.com/lgcC8Y5.jpg" style="width:100%">
<img class="mySlides" src="http://i.imgur.com/ufmiVTQ.jpg" style="width:100%">
<a class="w3-btn-floating w3-display-left" onclick="plusDivs(this, -1)">❮</a>
<a class="w3-btn-floating w3-display-right" onclick="plusDivs(this, 1)">❯</a>
</div>
现在,您可以为具有“滑块”类和唯一ID的滑块添加任意数量的div。
答案 2 :(得分:0)
问题通过数组和索引操作来解决
数组的大小应根据幻灯片的数量
var slideIndex = new Array(2);
slideIndex[0]=1;
slideIndex[1]=1;
showSlides(1, 0);
showSlides(1, 1);
function plusSlides(n, slideshownumber)
{
slideIndex[slideshownumber] = slideIndex[slideshownumber] + n;
showSlides( slideIndex[slideshownumber], slideshownumber );
}
function currentSlide(n, slideshownumber)
{
slideIndex[slideshownumber] = n;
showSlides(slideIndex[slideshownumber], slideshownumber);
}
function showSlides(n, slideshownumber)
{
var i;
在'slider'+ slideshownumer建立名称,基于0的计数(重要)
var slideshowname = "slider" + slideshownumber;
var slides = document.getElementsByName(slideshowname);
在'dot'+ slideshownumer建立名称,基于0的计数(重要)
var dotname = "dot" + slideshownumber;
var dots = document.getElementsByName(dotname);
if (n > slides.length)
{
slideIndex[slideshownumber] = 1;
}
if (n < 1)
{
slideIndex[slideshownumber] = 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[slideshownumber]-1].style.display = "block";
dots[slideIndex[slideshownumber]-1].className += " active";
}
幻灯片放映的示例,点和滑块名称应该进展到slider1,slider2,...
<div class="slideshow-container">
<div class="mySlides fade" name="slider0">
<div class="numbertext">1 / 2</div>
<img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
</div>
<div class="mySlides fade" name="slider0">
<div class="numbertext">2 / 2</div>
<img src="http://www.w3schools.com/howto/img_fjords_wide.jpg" style="width:100%">
</div>
plusSlides(返回-1,幻灯片0) plusSlides(1表示转发,幻灯片0)
<a class="prev" onclick="plusSlides(-1,0)">❮</a>
<a class="next" onclick="plusSlides(1,0)">❯</a>
</div>
初始化
<script> currentSlide(1,0)</script>
function currentSlide(slidenumber,slideshownumber) 对于点和上一个/下一个按钮!
<div style="text-align:center">
<span class="dot" name="dot0" onclick="currentSlide(1,0)"></span>
<span class="dot" name="dot0" onclick="currentSlide(2,0)"></span>
</div>
下一个名为slider1的滑块
<div class="slideshow-container">
<div class="mySlides fade" name="slider1">
<div class="numbertext">1 / 2</div>
<img src="http://www.w3schools.com/howto/img_nature_wide.jpg" style="width:100%">
</div>
<div class="mySlides fade" name="slider1">
<div class="numbertext">2 / 2</div>
<img src="http://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%">
</div>
立即更改了slider1的参数
<a class="prev" onclick="plusSlides(-1,1)">❮</a>
<a class="next" onclick="plusSlides(1,1)">❯</a>
</div>
<script> currentSlide(1,1)</script>
<div style="text-align:center">
<span class="dot" name="dot1" onclick="currentSlide(1,1)"></span>
<span class="dot" name="dot1" onclick="currentSlide(2,1)"></span>
</div>