javascript - 如何将自动定时器设置为内容幻灯片?

标签 javascript jquery html css

我将以下内容用作内容 slider ,但不确定是否要单击按钮来播放幻灯片,但又想取消它并让幻灯片在加载时播放?

DEMO

$(document).ready(function() {

var height = 300,
    width = 600,

    tabs = 3,

    $tabs = $('.tab'),
    contentNum = 1,

    delay = 2000, // time in milliseconds to pause between tabs
    timer;

$('.play').click(function(){
    var $t = $(this);
    if ($t.hasClass('playing')) {
        // stop
        clearTimeout(timer);
        $t.removeClass('playing').html('play');
    } else {
        // play
        timer = setInterval(function(){
            contentNum++; // change to contentNum--; to go left
            if (contentNum > tabs){ contentNum = 1; } // loop right
            if (contentNum < 1) { contentNum = tabs; } // loop left
            $tabs.eq(contentNum-1).find('a').trigger('click');
        }, delay);
        $t.addClass('playing').html('stop');
    }
});

$('.main_inner').css({
    width: tabs * width
});

$('a.tab_link').click(function() {

    $tabs.filter('.active').removeClass('active');
    $(this).parent().addClass('active');

    // make sure contentNum is a number and not a string
    contentNum = parseInt( $(this).attr('rel'), 10 );

    $('.main_inner').animate({
        marginLeft: '-' + (width * contentNum - width)
    }, 600);

    return false;

});


});

最佳答案

不太确定你的意思。

如果您不想在页面加载时自动播放并继续使用“播放”按钮

$('.play').trigger('click');

如果你不想在没有播放/停止按钮替换的情况下自动播放

$('.play').click(function(){
  // you current play/pause button code
});

简单地通过

// play
setInterval(function(){
    contentNum++; // change to contentNum--; to go left
    if (contentNum > tabs){ contentNum = 1; } // loop right
    if (contentNum < 1) { contentNum = tabs; } // loop left
    $tabs.eq(contentNum-1).find('a').trigger('click');
}, delay);

关于javascript - 如何将自动定时器设置为内容幻灯片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666531/

相关文章:

javascript - 文本区域的边距内的 CSS 宽度 100%

javascript - AngularJS ng-class 和 ng-style 一起工作

javascript - Ajax 或 Javascript 在向导第二步中不起作用

html - 更改 WordPress 页面/帖子标记

java - 需要将Java中的数组传递给javascript

javascript - 从 Google map 中删除路线

jQuery onclick 删除表格行

jquery - 在 ASP.NET WebForms 中使用 jQuery 调用 'WebMethod'

html - 如何删除背景图像的边框

html - 更改 NavBar 上的背景图像单击