1

我正在寻找答案或建议如何让我的滑块忽略箭头上的任何点击,直到完成向左或向右箭头的移动。

我在这里有一个链接,可以准确地向您展示正在发生的事情。

http://madaxedesign.co.uk/dev/index/

和下面的Jquery代码:

$(document).ready(function() {

    var nav = $('div.slider').css('overflow', 'hidden').children('ul'),
    ul = $('#container .slider ul'),
    count = $('#container .slider ul li').length,
    width = count * 854;    

    $('#container .slider ul').width(width);

function slideRight() {
        ul.animate({
            left: '0'
        }, 1000);
    }

    function moveRight() {
        $('#container .slider ul li:first').insertBefore($('#container .slider ul li:last'));           
        ul.css("left", "-854px");
    }

    function slideLeft() {
        ul.animate({
            left: '-=854'
        }, 1000, function() {
            $('#container .slider ul li:first').appendTo(ul);           
            ul.css("left", "0");
        });
    }

    var autoSlide;

    autoSlide = setInterval(function() {
        slideLeft();
    }, 3000);

    $('#container .slider_container').mouseenter(function() {
        clearInterval(autoSlide);
    }).mouseleave(function() {
        autoSlide = setInterval(function() {
            slideLeft();
        }, 3000);
    });

    $('#slider-nav').show();

    $('#slider-nav .left_arrow').click(function(slideLeft){
        event.stopPropagation();
    });

    $('#slider-nav .right_arrow').click(function() {
        moveRight();
        slideRight().delay(200);
    });
});
4

3 回答 3

1

使用:animated选择器检查动画是否正在运行。

$('#slider-nav .right_arrow').click(function() {
    if(ul.is(":animated")) return;
    ...
});
于 2013-02-26T14:02:11.857 回答
1

诀窍是保留一个变量,告诉您当前是否正在滑动,并且仅在它是false. 然后当滑动函数执行时,将其设置为true,并告诉回调将其设置为 false。我sliding在下面的代码中调用了变量:

$(document).ready(function() {
    // Flag to test on trigger
    var sliding = false;

    var nav = $('div.slider').css('overflow', 'hidden').children('ul'),
    ul = $('#container .slider ul'),
    count = $('#container .slider ul li').length,
    width = count * 854;    

    $('#container .slider ul').width(width);

function slideRight() {
        // Stop the function if we're already sliding
        if(sliding){
            return false;
        }

        // And now we're sliding
        sliding = true;

        ul.animate({
            left: '0'
        }, 1000, function(){
            // Slide over!
            sliding = false;
        });
    }

    function moveRight() {
        $('#container .slider ul li:first').insertBefore($('#container .slider ul li:last'));
        ul.css("left", "-854px");
    }

    function slideLeft() {
        // Stop the function if we're already sliding
        if(sliding){
            return false;
        }

        // And now we're sliding
        sliding = true;
        ul.animate({
            left: '-=854'
        }, 1000, function() {
            // Slide over!
            sliding = false;

            $('#container .slider ul li:first').appendTo(ul);
            ul.css("left", "0");
        });
    }

    var autoSlide;

    autoSlide = setInterval(function() {
        slideLeft();
    }, 3000);

    $('#container .slider_container').mouseenter(function() {
        clearInterval(autoSlide);
    }).mouseleave(function() {
        autoSlide = setInterval(function() {
            slideLeft();
        }, 3000);
    });

    $('#slider-nav').show();

    $('#slider-nav .left_arrow').click(function(slideLeft){
        event.stopPropagation();
    });

    $('#slider-nav .right_arrow').click(function() {
        moveRight();
        slideRight().delay(200);
    });
});
于 2013-02-26T14:05:28.113 回答
0

我认为你的问题是slideRight()没有返回任何东西......

function slideRight() {
    return ul.animate({
        left: '0'
    }, 1000);
}
于 2013-02-26T14:02:40.030 回答