2014-09-29 26 views
3

如果第一个或最后一个选项卡显示为用户,我希望将类.disabled添加到左侧和/或右侧控件(.tab-left,.tab-right)可以看到他们已经到达最后,不能再点击任何东西。在每次点击和添加类别期间检查位置

现在我这样做,以防止用户过去的结束。

if (tabs are at the end) { 
    return; 
} 

这适用于用户无法点击过去的结束,但如果我返回之前添加类,问题是.disabled类不会被添加,直到标签已经到达终点和用户再次点击。

if (tabs are at the end) { 
    $('.tab-right').addClass('disabled'); 
    return; 
} 

我需要在显示最后一个选项卡时添加禁用的类,而不是在用户尝试单击末尾时添加。

下面是JS的小提琴链接:http://jsfiddle.net/uue6pgcx/

+1

所以基本上你想要的JavaScript返回前要添加的类,因此用户并不需要点击第二次? – GSaunders 2014-09-29 17:10:33

+0

是的,这正是我想要的。 – user3123174 2014-09-29 17:11:11

回答

3

一种选择,你可以尝试是启用/禁用的左,右按键一旦动画完成。

$ul.filter(':not(:animated)').animate({ 
    "left": dir + liWidth 
    }, { 
    complete: function() { 
    // Calculate the number of items in the container (without left and right navigation buttons). 
    var lisContainer = Math.round(($container.width() - $left.outerWidth() - $right.outerWidth())/liWidth); 
    // Disable right button when list is moved to the left a number of items 
    // such as the remaining number of them is less or equal than the number 
    // of items that fit in the container. 
    $right.toggleClass('disabled', $li.length + $ul.position().left/liWidth <= lisContainer); 
    // Disable left button when list is in the origin. 
    $left.toggleClass('disabled', $ul.position().left === 0); 
    } 
}); 

声明:根据jQuery outerWidth additional notes,通过尺寸相关的API,包括.outerWidth(返回数量),可以是在某些情况下是分数。代码不应该认为它是一个整数。。所以让我们希望Math.round就足以得到正确的数字。 也许有更好的方法来计算是否必须禁用/启用右按钮,而不是依赖容器中的项目数量。

这是你的代码与上面的修改: http://jsfiddle.net/0Lsepxeu/

+0

这正是我所期待的。谢谢! – user3123174 2014-09-29 22:52:33