简体   繁体   English

Javascript轮播-问题添加队列

[英]Javascript Carousel - Issue adding Queue

I am using a jQuery carousel to display 5 list items at a time, when clicked left or right the code will move the list the appropriate width to display a new item. 我正在使用一个jQuery轮播来一次显示5个列表项,当向左或向右单击代码时,代码会将列表移动适当的宽度以显示新项。 The problem I have is when the user clicks the left or right button multiple times quickly the function will move in an ugly matter. 我的问题是,当用户多次单击向左或向右按​​钮时,该功能将很难处理。

stop().animate will not work because it stops the animation not the calculation stop()。animate将不起作用,因为它会停止动画而不是计算

I need to essientially queue the function everytime the user calls it or disable the call mechanism while the function is still running? 我需要在用户每次调用该函数时将其基本排队,或者在该函数仍在运行时禁用该调用机制吗? Any idea on how to accomplish this 关于如何做到这一点的任何想法

jQuery jQuery的

$(document).ready(function() {
    $('.brand-carousel-inner li:first').before($('.brand-carousel-inner li:last'));
});

function brandSlide(direction) {
    //Create width variables
    var brand_item_total_width = 196;

    //Create movement variable by adjusting the LEFT position +/-
    if (direction == 'left') {
        var movement = parseInt($('.brand-carousel-inner').css('left')) + brand_item_total_width;
    } else {
        var movement = parseInt($('.brand-carousel-inner').css('left')) - brand_item_total_width;
    }

    //Apply movement variable, animate carousel and reposition UL
    $('.brand-carousel-inner').animate({
        'left': movement
    }, 700).promise().done(function() {
        if (direction == 'left') {
            $('.brand-carousel-inner li:first').before($('.brand-carousel-inner li:last'));
        } else {
            $('.brand-carousel-inner li:last').after($('.brand-carousel-inner li:first'));
        }
        $('.brand-carousel-inner').css({
            'left': '-196px'
        });
    });
}

HTML 的HTML

<section id="brand-carousel">
        <a href="#" onclick="brandSlide('left'); return false" class="nav-left"></a>
        <a href="#" onclick="brandSlide('right'); return false" class="nav-right"></a>
        <div class="brand-mask">
           <ul class="brand-carousel-inner">
                    <li><img src="image/image.jpg" /></li>
                    /*20 more list items*/
           </ul>
        </div>
     </section>  

You need to define the variable movement on a higher scope as it is clearly inaccessible in the animation: 您需要在更高的范围内定义变量movement ,因为在动画中显然无法访问它:

$(document).ready(function() {
    $('.brand-carousel-inner li:first').before($('.brand-carousel-inner li:last'));
});

function brandSlide(direction) {
    //Create width variables
    var brand_item_total_width = 196;

    //Create movement variable by adjusting the LEFT position +/-
    var movement = null;

    if (direction === 'left') {
        movement = parseInt($('.brand-carousel-inner').css('left')) + brand_item_total_width;
    } else {
        movement = parseInt($('.brand-carousel-inner').css('left')) - brand_item_total_width;
    }

    //Apply movement variable, animate carousel and reposition UL
    $('.brand-carousel-inner').animate({
        'left': movement
    }, 700).promise().done(function() {
        if (direction === 'left') {
            $('.brand-carousel-inner li:first').before($('.brand-carousel-inner li:last'));
        } else {
            $('.brand-carousel-inner li:last').after($('.brand-carousel-inner li:first'));
        }
        $('.brand-carousel-inner').css({
            'left': '-196px'
        });
    });
}

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

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