简体   繁体   English

非常简单的图像滑块可自动滑动

[英]Very simple image slider to auto slide

Hey guys I have an image slider that has controls but I also want to intergrate an auto scroll into the JS too. 大家好,我有一个带有控件的图像滑块,但我也想将自动滚动集成到JS中。 I have looked around but cannot seem to understand where to insert the code I need and exact what code it is.. 我环顾四周,但似乎无法理解在哪里插入我需要的代码以及确切的代码是什么。

$(document).ready(function() {
    $('.sp').first().addClass('active');
    $('.sp').hide();    
    $('.active').show();

    $('#button-next').click(function() {
        $('.active').removeClass('active').addClass('oldActive');

        if ( $('.oldActive').is(':last-child')) {
            $('.sp').first().addClass('active');
        }
        else {
            $('.oldActive').next().addClass('active');
        }

        $('.oldActive').removeClass('oldActive');
        $('.sp').fadeOut();
        $('.active').fadeIn();
    });

    $('#button-previous').click(function() {
        $('.active').removeClass('active').addClass('oldActive');    
        if ( $('.oldActive').is(':first-child')) {
            $('.sp').last().addClass('active');
        }
        else {
            $('.oldActive').prev().addClass('active');
        }
        $('.oldActive').removeClass('oldActive');
        $('.sp').fadeOut();
        $('.active').fadeIn();
    });
});

So it has literally a next and previous nav which works fine I would like it to just auto scroll through the images. 因此,它实际上具有下一个和上一个导航,可以正常工作,我希望它可以自动滚动图像。

You will need to create 2 new buttons Start and stop . 您将需要创建2个新按钮“ Start和“ stop It's up to you if you want to style them to hide one and show the other. 如果要设置它们的样式以隐藏其中一个并显示另一个,则取决于您。 Here's a sample code 这是一个示例代码

var slideshow;

$('#start').click(function(){
    slideshow = setInterval(function(){
        $('#button-next').click();
    }, 1000);
});

$('#stop').click(function(){
    clearInterval(slideshow);
});

I just wrote this so there may be some issues, in which case be sure to leave a comment! 我只是写了这篇,所以可能会有一些问题,在这种情况下,请务必发表评论! :P

EDIT 编辑

Here's for automatic start 这是自动启动

setInterval(function(){
    $('#button-next').click();
}, 1000);

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

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