获取图像滑块以自动播放

发布于 2024-09-14 22:56:58 字数 1017 浏览 10 评论 0原文

我试图让我自己的图像滑块以 4000 毫秒的间隔自动播放。我尝试过使用 setTimeout 但我似乎无法让它工作。如果有人能帮助我,我将不胜感激。以下是滑块的示例:

http://www.matthewruddy.com/slider-intervalissue/

有什么想法可以让它自动播放吗?

这是 jQuery 代码:

$(document).ready(function(){
    var images = $('.slider li');
    var slides = images.length;
    var sliderwidth = 500;
    var currentimage = 0;
    var containerwidth =  sliderwidth*slides;
    var autoplay = 4000;

    $('.slider').css('width', containerwidth);

    $('#next').bind('click',nextimage);

    function nextimage(){
        if(currentimage<slides-1){
            currentimage++;
            $('.slider').animate({left:-sliderwidth*(currentimage)},{duration:800,easing:'easeInOutCubic'});
        }
        else{
            currentimage = 0;
            $('.slider').animate({left:0},{duration:800,easing:'easeInOutCubic'})
        }
    };

});

谢谢, 马修

I am trying to get my own image slider to auto play with an interval of 4000 milliseconds. I've tried using setTimeout but I cannot seem to get it to work. I would appreciate it if someone could help me out. Here is example of the slider:

http://www.matthewruddy.com/slider-intervalissue/

Any ideas how I can get it to auto play?

Here is the jQuery code:

$(document).ready(function(){
    var images = $('.slider li');
    var slides = images.length;
    var sliderwidth = 500;
    var currentimage = 0;
    var containerwidth =  sliderwidth*slides;
    var autoplay = 4000;

    $('.slider').css('width', containerwidth);

    $('#next').bind('click',nextimage);

    function nextimage(){
        if(currentimage<slides-1){
            currentimage++;
            $('.slider').animate({left:-sliderwidth*(currentimage)},{duration:800,easing:'easeInOutCubic'});
        }
        else{
            currentimage = 0;
            $('.slider').animate({left:0},{duration:800,easing:'easeInOutCubic'})
        }
    };

});

Thanks,
Matthew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2024-09-21 22:56:58

我能够使用 setTimeout 和递归的组合来设置运行得非常好的自动播放。

我给了 nextimage() 函数一个参数 autoplay,它是一个布尔值。然后我在函数的末尾添加了以下行:

if (autoplay) { setTimeout(function() { nextimage(true); }, 4000); }

最后,我在脚本的末尾添加了这一行,以使自动播放循环继续:

setTimeout(function() { nextimage(true); }, 4000);

查看此处的演示,主要根据您提供的内容构建,并进行了一些修改进行自动播放: http://jsfiddle.net/bVjkP/

简而言之,您将一个布尔值传递给nextimage() 函数,告诉它是否开始自动播放。然后,您只需调用初始 setTimeout 即可开始工作。这个方法的技巧是你必须通过setTimeout调用一个匿名函数,该函数会在autoplay设置为true的情况下调用nextimage函数,因为用setTimeout调用函数时不能传递参数。这类似于使用 jQuery 将函数绑定到单击或悬停等事件的方法。

I was able to set up an autoplay that runs quite nicely, using a combination of setTimeout and recursion.

I gave the nextimage() function a parameter autoplay, which is a boolean. Then I added the following line at the end of the function:

if (autoplay) { setTimeout(function() { nextimage(true); }, 4000); }

Finally I added this line at the end of the script, to get the autoplay loop going:

setTimeout(function() { nextimage(true); }, 4000);

Check out the demo here, constructed mostly from what you provided, with a few modifications to do the autoplaying: http://jsfiddle.net/bVjkP/

In short, you pass a boolean into the nextimage() function, to tell it whether or not to begin an autoplay. Then you simply call an initial setTimeout to get the ball rolling. The trick with this method is that you have to call an anonymous function through the setTimeout, which calls the nextimage function with autoplay set to true, because you can't pass parameters when calling a function with setTimeout. This is similar to the method for binding functions to events such as click or hover using jQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文