0

我有一个在网页上推进滚动图像的功能。可以在这里看到:

http://leadgenixhosting.com/~intmed/

右箭头是您单击以推进图像的方向。我在使用 .unbind() 的地方遇到了这个问题,因此在图像使用 .animate() 完成之前,箭头是不可点击的,这样它们就不会不同步。但我不完全确定如何使用 .bind() 使箭头再次可点击。

这是函数当前的样子:

$('#in-right').click(
        function(){
            $('#in-right').unbind('click');
            imageSwitch();
            $('#in-right').bind('click');
        }
);

我显然错误地实现了绑定。我怎样才能正确实施它?

这是我的 imageSwitch() 函数:

function imageSwitch(){

        current++;
        current_image++;
        previous_image++;
        next++;
        two_prev++

        if(two_prev >= divs.length){
            two_prev = 0;   
        }

        if(current >= gallery_images.length){
            current = 0;    
        }

        if(previous_image >= divs.length){
            previous_image = 0; 
        }

        if(current_image >= divs.length){
            current_image = 0;
        }

        if(next >= divs.length){
            next = 0;
        }   

        $('#'+divs[current_image]+'').animate({left:'-=1020px'},{queue:false,duration:1000})
        $('#'+divs[current_image]+'').css('background-image','url('+gallery_images[current]+')');

        $('#'+divs[previous_image]+'').animate({left:'-=1020px'},{queue:false,duration:1000});

        $('#'+divs[next]+'').animate({left: '+=1020px', top: '-=10000px'}, 1000);

        $('#'+divs[two_prev]+'').animate({left: '+=1020px', top: '+=10000px'}, 1000);
    }
4

1 回答 1

5

.bind要求您将函数作为第二个参数传递给它。

function clickFunc(){  // Give the event function a name
    $('#in-right').unbind('click');
    imageSwitch();
    $('#in-right').click(clickFunc);  // .click is shorthand for `.bind('click',`
}
$('#in-right').click(clickFunc);

编辑:你应该让你的imageSwich函数接受一个回调,这样它就可以在完成动画后重新绑定函数。

function imageSwitch(callback){
    // code...
}

function clickFunc(){  // Give the event function a name
    $('#in-right').unbind('click');
    imageSwitch(function(){
        $('#in-right').click(clickFunc);
    });
}
$('#in-right').click(clickFunc);

至于确保所有动画完成后回调运行,我们可以使用 jQuery 的deferreds

基本上,您希望将返回的对象保存.animate到变量中。然后使用$.when, 和.then在正确的时间运行回调。

(这是未经测试的,顺便说一句)

function imageSwitch(callback) {
    var animations = [];

    current++;
    current_image++;
    previous_image++;
    next++;
    two_prev++

    if (two_prev >= divs.length) {
        two_prev = 0;
    }

    if (current >= gallery_images.length) {
        current = 0;
    }

    if (previous_image >= divs.length) {
        previous_image = 0;
    }

    if (current_image >= divs.length) {
        current_image = 0;
    }

    if (next >= divs.length) {
        next = 0;
    }

    animations.push($('#' + divs[current_image] + '').animate({
        left: '-=1020px'
    }, {
        queue: false,
        duration: 1000
    }));

    $('#' + divs[current_image] + '').css('background-image', 'url(' + gallery_images[current] + ')');

    animations.push($('#' + divs[previous_image] + '').animate({
        left: '-=1020px'
    }, {
        queue: false,
        duration: 1000
    }));

    animations.push($('#' + divs[next] + '').animate({
        left: '+=1020px',
        top: '-=10000px'
    }, 1000));

    animations.push($('#' + divs[two_prev] + '').animate({
        left: '+=1020px',
        top: '+=10000px'
    }, 1000));

    if (typeof callback === 'function') {
        // Apply is used here because `$.when` expects to passed multiple parameters
        $.when.apply($,animations).then(callback);
    }
}

延迟和动画演示:http: //jsfiddle.net/M58KK/

于 2012-08-10T16:09:48.590 回答