简体   繁体   English

为什么我的图像滑块不自动启动?

[英]Why does my image slider not automatically start?

I hope to get some help here. 我希望在这里得到一些帮助。 I think the solution can be very easy but I can't find the problem. 我认为解决方案可能非常简单,但我找不到问题。

I created an image slider with changing buttons on click. 我创建了一个图像滑块,并在单击时更改了按钮。 I have the code from the internet and it works fine. 我有来自互联网的代码,并且工作正常。 The problem is that the carousel doesn't start automatically on the website starter page. 问题是轮播不会在网站启动器页面上自动启动。 The slideshow loop only starts if I click on the first picture. 仅当我单击第一张图片时,幻灯片循环才会开始。

This is how my .js code looks like. 这就是我的.js代码的样子。 Regards :) 问候 :)

/*SLIDESHOW*/
var slideIndex = 0;
showSlide(slideIndex);

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


function currentSlide(n) {
showSlide(slideIndex = n);
}


function showSlide(n){
var i;
var slides = document.getElementsByClassName("myslides fade");
var dots = document.getElementsByClassName("dots");
if (n > slides.length) { slideIndex = 3};
if (n < 3) { slideIndex = slides.length};
for (i=0;i<slides.length;i++)



 {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) 
  {slideIndex = 1}
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";
setTimeout(showSlide, 3000);

}

Add your code inside a document.ready function and try, 将您的代码添加到document.ready函数中,然后尝试,

$( document ).ready(function() {

     //Your Code

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM