简体   繁体   English

Javascript图像滑块setInterval()

[英]Javascript image slider setInterval()

I am making a simple image slider that runs through a directory containing images. 我正在制作一个简单的图像滑块,它运行在包含图像的目录中。 The images is called "img_1", "img_2" and "img_3", so the point is to just iterate through them. 图像被称为“img_1”,“img_2”和“img_3”,因此重点是迭代它们。 The thing I don't get is how to implement a interval though. 我没有得到的是如何实现间隔。 I want the images appear for 3 seconds, then change. 我希望图像显示3秒钟,然后更改。 The function "imgSlider" is on body load. 函数“imgSlider”是在体载上。

Edit: I am looking for simple solutions (in pure JS) as I am currently learning JS. 编辑:我正在寻找简单的解决方案(纯JS),因为我正在学习JS。 Sorry for not clarifying it in the question. 很抱歉没有在问题中澄清它。 Lots of good answers here though! 虽然这里有很多好的答案!

function changeImg(img, i){
 img.setAttribute("src", "img/slider/img_" + i + ".jpg");
}

function imgSlider(){
 var img = document.createElement("img");
 var targetDiv = document.getElementById("slider");

 img.setAttribute("width", "100%");
 img.setAttribute("height", "70%");

 targetDiv.appendChild(img);

 for(i=1; i<4; i++){
     setInterval(changeImg(img, i), 3000);
  }
}

What you need to do is simple, you have to change the setInterval code like this: 你需要做的很简单,你必须改变setInterval代码,如下所示:

 $(function () { var curImg = 1; setInterval(function () { $("img").attr("src", "//placehold.it/100?text=" + ++curImg); }, 2000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="//placehold.it/100?text=1" /> 

Here, I have given an interval of 2 seconds for checking. 在这里,我给了2秒的间隔进行检查。 I have used jQuery only for the updating of image. 我只使用jQuery来更新图像。 If you want it in plain JavaScript, it is possible through your code. 如果您想在纯JavaScript中使用它,则可以通过您的代码实现。

Pure JS Version 纯JS版

 var curImg = 1; setInterval(function () { document.getElementsByTagName("img")[0].src = "//placehold.it/100?text=" + ++curImg; }, 2000); 
 <img src="//placehold.it/100?text=1" /> 

You can do it this way. 你可以这样做。 Initialize a counter, that will keep a track of current image to being displayed. 初始化计数器,将跟踪当前图像的显示。 Now all we need to is call showImage function that will show the image corresponding to the current value of the counter. 现在我们需要的是调用showImage函数,它将显示与计数器当前值对应的图像。

Repeat this every 3sec. 每3秒重复一次。

var count = 0; 
setInterval( function() { 
   showImage(count); 
   count = (count+1)%total_no_of_images
}, 3000)

setInterval takes a function, you need to pass it a reference to a function that it will run at the appropriate interval. setInterval接受一个函数,你需要传递一个函数的引用,它将以适当的间隔运行。

Also, note that as of this writing, the accepted answer assumes there are infinite images and has an "infinite loop" style of interval, which should be avoided if at all possible. 此外,请注意,在撰写本文时,接受的答案假定存在无限图像并且具有“无限循环”的间隔样式,如果可能的话应该避免。 Here, I stop the clock after the fifth image. 在这里,我在第五张图像后停止时钟。

const changeImg = (img, i) => {
   img.setAttribute('src', `img/slider/img_${i}.jpg`);
};

const imgSlider = () => {
    const img = document.createElement('img');
    const targetDiv = document.getElementById('slider');

    img.setAttribute('width', '100%');
    img.setAttribute('height', '70%');

    targetDiv.appendChild(img);

    let i = 0;

    const interval = setInterval(
        () => {
            changeImg(img, i);
            i += 1;
            if (i > 4) {
                clearInterval(interval);
            }
        },
        3000
    );
};

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

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