屏幕分辨率更改时,间隔加速的Jquery滑块

时间:2016-03-09 18:57:26

标签: javascript jquery

所以我的滑块工作得很好;切换evey 3秒并在鼠标悬停时停止或者单击箭头。如果我不管它,它会重新启动。

一切正常。

然而,当我改变分辨率以开始响应时,开始加速并且不会响应鼠标悬停。

$var main = function(){
$('.dropdown-toggle').click(function(){
    $('.dropdown-menu').toggle();

    });


$('.arrow-next').click(function(){
    var currentSlide = $('.active-slide');
    var nextSlide = currentSlide.next();
    if(nextSlide.length == 0){
        nextSlide = $('.slide').first();
        };
    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');

    var currentDot = $('.active-dot');
    var nextDot = currentDot.next();
    if(nextDot.length == 0){
        nextDot = $('.dot').first();
        };
    currentDot.removeClass('active-dot');
    nextDot.addClass('active-dot');
    });
};

$('.arrow-prev').click(function(){
    currentSlide = $('.active-slide');
    prevSlide = currentSlide.prev();
    if(prevSlide.length == 0){
        prevSlide = $('.slide').last();
        };
    currentSlide.fadeOut(600).removeClass('active-slide');
    prevSlide.fadeIn(600).addClass('active-slide');

    var currentDot = $('.active-dot');
    var prevDot = currentDot.prev();
    if(prevDot.length == 0){
        prevDot = $('.dot').last();
        };
    currentDot.removeClass('active-dot');
    prevDot.addClass('active-dot');


    });


$(document).ready(main);

$(document).ready( function() {

var auto = setInterval(function() {

  var currentSlide = $('.active-slide');
    var nextSlide = currentSlide.next();
    if(nextSlide.length == 0){
        nextSlide = $('.slide').first();
        };
    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');

    var currentDot = $('.active-dot');
    var nextDot = currentDot.next();
    if(nextDot.length == 0){
        nextDot = $('.dot').first();
        };
    currentDot.removeClass('active-dot');
    nextDot.addClass('active-dot');
}, 3000);


 $('.slider, .arrow-prev, .arrow-next').mouseover(function(){
clearInterval(auto);
}).mouseout(function(){
var auto = setInterval(function() {

  var currentSlide = $('.active-slide');
    var nextSlide = currentSlide.next();
    if(nextSlide.length == 0){
        nextSlide = $('.slide').first();
        };
    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');

    var currentDot = $('.active-dot');
    var nextDot = currentDot.next();
    if(nextDot.length == 0){
        nextDot = $('.dot').first();
        };
    currentDot.removeClass('active-dot');
    nextDot.addClass('active-dot');
}, 3000);
})

});        

1 个答案:

答案 0 :(得分:0)

我想通了......因为我宣布" var"在函数内部,它不是全局变量,所以它堆叠起来。现在我在函数外部添加了var auto并删除了var inside函数,它与resize完美配合;最小化,以及一切:D

我大约3周前开始查看js和jquery,之前有0个知识,所以我的代码可能是...怎么说... le crap!但至少我很高兴我把它想出来了!

我欢迎有关此滑块的一般建议。