jquery如何每5秒触发相同的事件

2024-03-08

我构建了一个带有左右滚动的简单轮播。现在我想每 5 秒自动滚动一次。这是我的代码:

function carousel(){
        $j('#carousel_ul li:first').before($j('#carousel_ul li:last'));

        $j('#right_scroll img').click(function(){

            var item_width = $j('#carousel_ul li').outerWidth() + 10;

            var left_indent = parseInt($j('#carousel_ul').css('left')) - item_width;

            $j('#carousel_ul:not(:animated)').animate({'left' : left_indent},800, 'easeOutExpo',function(){

                $j('#carousel_ul li:last').after($j('#carousel_ul li:first'));

                $j('#carousel_ul').css({'left' : '-750px'});
            });
        });

        $j('#left_scroll img').click(function(){

            var item_width = $j('#carousel_ul li').outerWidth() + 10;

            var left_indent = parseInt($j('#carousel_ul').css('left')) + item_width;

            $j('#carousel_ul:not(:animated)').animate({'left' : left_indent},800, 'easeOutExpo',function(){

            $j('#carousel_ul li:first').before($j('#carousel_ul li:last'));

            $j('#carousel_ul').css({'left' : '-750px'});
            });

        });
}

我该如何实现这一目标? 提前致谢 :)

Mauro


您可以使用 setInterval。看:

window.setInterval(event, 5000);

函数事件将是

function event() {
 $j("#right_scroll img").click();
}

EDIT:

谢谢@cris! setTimeout 仅调用一次。我改成setInterval了。

谢谢@bears!现在我将一个函数传递给 setInterval。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jquery如何每5秒触发相同的事件 的相关文章

随机推荐