jQuery滑块,带有控件和自动滚动 [英] jquery slider with controls and automatic scrolling

查看:82
本文介绍了jQuery滑块,带有控件和自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我只是出于教育目的而尝试做随机jQuery的东西,这是我非常简单的滑块.我希望它可以自动运行,也可以与控件一起运行(小箭头滚动到下一个/上一个滑块).我现在唯一的问题是,当您按箭头键时,每5秒自动切换幻灯片的功能仍在计算这5000毫秒,因此下一张幻灯片的显示速度比预期的要快.我想要的是使这些箭头重置计时器,因此您按箭头->出现下一张幻灯片->仅在5秒钟后再次切换幻灯片. 对不起,我的解释很草率,希望我已经说得足够清楚了.

Basically I am just trying to do random jQuery stuff for educational purpose, and here is my very simple slider. I want it to work automatically and also with controls (little arrows to scroll to next/previous slider). The only problem that I have right now is that when you press the arrow, the function that automatically switches slides every 5 seconds is still counting these 5000 ms, so the next slide appears faster then desired. What I want is to make those arrows reset the timer, so you press the arrow -> next slide appears -> only after 5 seconds later the slide switches again. Sorry for sloppy explanation, hope I made it clear enough.

这是jsfiddle: http://jsfiddle.net/cA9aW/

Here's the jsfiddle: http://jsfiddle.net/cA9aW/

这是代码

HTML

<body>
<div id="wrapper">
    <header>
         <h1>Simplest Sliding Image Slider</h1>

    </header>
    <div id="content">
        <div id="slider_container">
            <div id="slider">
                <div class="slides" id="slide1">
                    <img src="http://s2.postimg.org/5uxqi0mgl/cats1.jpg" alt="" />
                </div>
                <div class="slides" id="slide2">
                    <img src="http://s2.postimg.org/66f6us2wl/cats2.jpg" alt="" />
                </div>
                <div class="slides" id="slide3">
                    <img src="http://s2.postimg.org/ai3sjs9th/cats3.jpg" alt="" />
                </div>
            </div>
        </div>
    </div>
    <footer></footer>
    </div>
</body>       

JS

jQuery(document).ready(function($) {

// start slider function
startSlider();

// set width and step variables and add active class to first slider
var slideWidth = $('.slides').width();
$('#slide1').addClass('slides active');

// actual function
function startSlider() {

looper = setInterval(function() {
  // remove and add class active
  $('.active').removeClass('active').next().addClass('active');

  // animation expression
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);

  // return to first slide after the last one
  if($('.active').length == 0) {
    $('#slide1').addClass('active');
    $('.slides').animate({'left': 0}, 500);

  }
}, 5000); // interval


// adding controls
$('.slides').append("<div class='controls'><a class='control_left' href='#'></a><a class='control_right' href='#'></a></div>");

// remove unnecessary controlls on first and last slides
$('.slides:nth-child(1) a.control_left').remove();
$(".slides:nth-child(" + $('.slides').length + ") a.control_right").remove();

// add functionality to controls
$('.control_left').on('click', function() {
  $('.active').removeClass('active').prev().addClass('active');
  $('.active').animate({'left': '+=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '+=' + (slideWidth) + 'px'}, 500);
});

$('.control_right').on('click', function() {
  $('.active').removeClass('active').next().addClass('active');
  $('.active').animate({'left': '-=' + (slideWidth) + 'px'}, 500);
  $('.active').siblings().animate({'left': '-=' + (slideWidth) + 'px'}, 500);
});



}

});

提前很多

推荐答案

您需要执行以下操作来清除按钮单击的间隔并重新开始间隔.

What you need to do it to clear the interval in the button clicks and start interval again.

function resetInterval(){ //add this method which wil reset the timer
        window.clearInterval(looper); //clear current interval
        looper = setInterval(autoSlide, 5000); //start auto slide again.
}
function autoSlide(){ //move this to a function from being anonymous
        // remove and add class active
        $('.active').removeClass('active').next().addClass('active');
        // animation expression
        $('.active').animate({
            'left': '-=' + (slideWidth) + 'px'
        }, 500);
        $('.active').siblings().animate({
            'left': '-=' + (slideWidth) + 'px'
        }, 500);

        // return to first slide after the last one
        if ($('.active').length === 0) {
            $('#slide1').addClass('active');
            $('.slides').animate({
                'left': 0
            }, 500);
        }
}

 $('.control_left').on('click', function () {
        resetInterval(); //reset it
 ....

$('.control_right').on('click', function () {
        resetInterval(); //reset it
....

演示

Demo

这篇关于jQuery滑块,带有控件和自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆