停止js幻灯片缓存点击量

时间:2011-06-15 20:26:23

标签: javascript jquery

是否有人知道如何阻止此幻灯片显示“缓存”幻灯片中的点击次数?如果我点击箭头10次,那么10张幻灯片将会滑过,有没有办法阻止它?或者,如果您点击相反的箭头取消另一个的“缓存”点击次数?

http://www.switchonthecode.com/tutorials/jquery-creating-a-slideshow

谢谢!

2 个答案:

答案 0 :(得分:1)

使用one()功能可能会满足您的需求。将代码调整为以下内容:

$(document).ready(function(){
    // The "one()" function means that this action is disconnected
    // from the element once the action occurs. In other words, click
    // it twice, and the second one does nothing.
    $("#slideshow-previous").one("click", showPreviousSlide);
    $("#slideshow-next").one("click", showNextSlide);
    ...
});

然而,这样做还不够。动画完成后,我们必须再次连接事件处理程序。使用animate()的回调函数:

function updateContentHolder()
{
    ...
    $("#slideshow-scroller").animate({scrollLeft: scrollAmount}, 1000, function() {
        $("#slideshow-previous").one("click", showPreviousSlide);
        $("#slideshow-next").one("click", showNextSlide);
    });
}

正如评论中指出的那样,将showPreviousSlide和showNextSlide多次附加到未按下任何按钮的问题。你可以通过做更多的工作来解决这个问题:

function showPreviousSlide()
{
    currentSlide--;
    updateContentHolder($(this).attr("id"), showPreviousSlide);
    updateButtons();
}

function showNextSlide()
{
    currentSlide++;
    updateContentHolder($(this).attr("id"), showNextSlide);
    updateButtons();
}

...

function updateContentHolder(id, callback)
{
    ...
    $("#slideshow-scroller").animate({scrollLeft: scrollAmount}, 1000, function() {
        $("#" + id).one("click", callback);
    });
}

答案 1 :(得分:0)

只要点击箭头isPending,就可以将标志设置为true。然后在单击箭头时检查此项,如果为true,则忽略该单击。然后,当动画结束时,将标志设置为false。