javascript - 使用jQuery模拟下一个按钮点击

标签 javascript jquery

不确定这是否可行,但我的网站上有一个幻灯片,当单击一个按钮时,相关幻灯片就会滑入。

我想要做的是添加一个计时器,以便在 3 秒后单击下一个按钮,使我的幻灯片自动播放。

$('#button a').click(function(){
    var integer = $(this).attr('rel');
    $('#myslide .cover').animate({left:-720*(parseInt(integer)-1)})  /*----- Width of div mystuff (here 160) ------ */
    $('#button a').each(function(){
    $(this).removeClass('active');
        if($(this).hasClass('button'+integer)){
            $(this).addClass('active')}
    });


});

我添加了一个 fiddle ... http://jsfiddle.net/5jVtK/

最佳答案

最简单的方法是使用 setTimeout(延迟后发生一次)或 setInterval(每隔一段时间发生一次)。

setTimeout( function() { $( '#button a' ).trigger( 'click' ) }, 3000 );

setInterval( function() { $( '#button a' ).trigger( 'click' ) }, 3000 );

一旦你实现了这个,你可能想要考虑一些其他的细节,比如当用户的鼠标悬停在下一个按钮或幻灯片上时停止自动前进(因为这意味着对当前显示的内容感兴趣)和在鼠标移出时恢复自动前进。

下一步:听起来您需要弄清楚如何动态地找到正确的按钮来触发以在多张幻灯片中继续前进。这是一种方法:

`

function click() {
    // Find the button for the next slide in relationship to the currently active button
    var $next = $( '#button' ).find( '.active' ).next( 'a' );

    // If there isn't one, go to the beginning
    if ( ! $next.length ) {
        $next = $( '#button' ).find( 'a' ).first();
    }

    // Trigger the click
    $next.trigger( 'click' );

    setTimeout(click, 3000);
}

setTimeout(click, 3000);

这是一个 fiddle 的链接,显示了这个 Action :

http://jsfiddle.net/5jVtK/1/

关于javascript - 使用jQuery模拟下一个按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8794235/

相关文章:

javascript - .spread 带有数据重定向

javascript - AngularJS:如何随机/合并来自不同 ng-repeat 的内容

javascript - HTML5 Canvas 多行文本框

javascript - 让 HTML5 推送通知适用于所有网络浏览器和移动浏览器?

javascript - 如何用JS输出显示在外部网页上的值

javascript - 如何在 DropZone.js 上为上传的图片创建缩略图?

javascript - React.js : Confuse about `this` keyword context

javascript - 无法查看 label 标签内的 span 标签?

jquery - CSS :empty - DOM state sensitive without JS is it possible?

javascript - 如何在关闭浏览器时触发卸载方法?