jQuery If语句-fiddle有效,网站不';T

jQuery If语句-fiddle有效,网站不';T,jquery,if-statement,addclass,jsfiddle,removeclass,Jquery,If Statement,Addclass,Jsfiddle,Removeclass,好的。我在这里完全被难住了 我有一个水平滚动列表,当你不能再滚动时,按钮/箭头会改变颜色(通过addClass removeClass)。我的小提琴(几乎)演奏得很好。另一方面,我的代码不 小提琴: jQuery: $(document).ready(function() { var $item = $('div.mainBodyContentListItem'), //Cache your DOM selector visible = 2,

好的。我在这里完全被难住了

我有一个水平滚动列表,当你不能再滚动时,按钮/箭头会改变颜色(通过addClass removeClass)。我的小提琴(几乎)演奏得很好。另一方面,我的代码不

小提琴:

jQuery:

$(document).ready(function() {

    var $item = $('div.mainBodyContentListItem'),
        //Cache your DOM selector
        visible = 2,
        //Set the number of items that will be visible
        index = 0,
        //Starting index
        endIndex = ($item.length / visible) - 1; //End index
    $('div.mainBodyContentArrowR').click(function() {
        if (index < endIndex) { //can scroll
            index++;
            $item.animate({
                'left': '-=592px'
            });
        }
    });

    $('div.mainBodyContentArrowR').click(function() {
        if (index > endIndex) { //can't scroll
            $('div.mainBodyContentArrowR').addClass('disable');
        }
    });

    $('div.mainBodyContentArrowR').click(function() {
        if (index < endIndex) { //can scroll
            $('div.mainBodyContentArrowL').removeClass('disable');
        }
    });



    $('div.mainBodyContentArrowL').click(function() {
        if (index > 0) { //can scroll
            index--;
            $item.animate({
                'left': '+=592px'
            });
        }
    });

    $('div.mainBodyContentArrowL').click(function() {
        if (index < 0) { //can't scroll
            $('div.mainBodyContentArrowL').addClass('disable');
        }
    });

    $('div.mainBodyContentArrowL').click(function() {
        if (index > 0) { //can scroll
            $('div.mainBodyContentArrowR').removeClass('disable');
        }
    });


});
$(文档).ready(函数(){
var$item=$('div.mainBodyContentListItem'),
//缓存DOM选择器
可见=2,
//设置将可见的项目数
指数=0,
//起始指数
endIndex=($item.length/visible)-1;//结束索引
$('div.mainBodyContentArrowR')。单击(函数(){
如果(indexendIndex){//无法滚动
$('div.mainBodyContentArrowR').addClass('disable');
}
});
$('div.mainBodyContentArrowR')。单击(函数(){
如果(index0){//可以滚动
索引--;
$item.animate({
“左”:“+=592px”
});
}
});
$('div.mainBodyContentArrowL')。单击(函数(){
如果(索引<0){//无法滚动
$('div.mainBodyContentArrowL').addClass('disable');
}
});
$('div.mainBodyContentArrowL')。单击(函数(){
如果(索引>0){//可以滚动
$('div.mainBodyContentArrowR').removeClass('disable');
}
});
});
这是它应该做的(除了还没有找到解决问题的方法,回到左边并再次点击滚动的末尾,就像页面加载时一样,不会重新添加类并更改颜色-请随意解决,但这不是这个线程的问题)

实际上,我的代码在此实例中正确地执行了此操作:

 $('div.mainBodyContentArrowR').click(function() {
            if (index < endIndex) { //can scroll
                $('div.mainBodyContentArrowL').removeClass('disable');
            }
        });
$('div.mainBodyContentArrowR')。单击(函数(){
如果(index
但没有别的地方。我被难住了。奇怪的是,我上面概述的这条线工作正常。“disable”类在第一次单击时被删除,然后那些addClass removeClass行什么都不做(来回滚动确实有效并正确停止)

请帮忙,谢谢。我觉得我只是盯着一堵50英尺高的砖墙,看不到自己的路。

如果你改变主意

if(index < endIndex)
改为

if(index >= 1)
我知道你在评论中提到的问题即将出现。要解决此问题,需要在计算结束索引的行之后添加以下行

if(($item.length % visible) == 0){
    enIndex = endIndex - 1;
}

您也可以这样尝试:

$(document).ready(function () {
    'use strict';
    var $item = $('div.mainBodyContentListItem'), //Cache your DOM selectors
        mbcArrowR = $('div.mainBodyContentArrowR'),
        mbcArrowL = $('div.mainBodyContentArrowL'),
        visible = 2, //Set the number of items that will be visible
        index = 0, //Starting index
        endIndex = ($item.length / visible) - 1; //End index
    mbcArrowR.click(function () {
        if (index < endIndex) { //can scroll
            index += 1;
            $item.animate({
                'left': '-=592px'
            });
            mbcArrowL.removeClass('disable');
        }
        mbcArrowR.toggleClass('disable', index >= endIndex);
    });
    mbcArrowL.click(function () {
        if (index > 0) { //can scroll
            index -= 1;
            $item.animate({
                'left': '+=592px'
            });
            mbcArrowR.removeClass('disable');
        }
        mbcArrowL.toggleClass('disable', index <= 0);
    });
});​
$(文档).ready(函数(){
"严格使用",;
var$item=$('div.mainBodyContentListItem'),//缓存DOM选择器
mbcArrowR=$('div.mainBodyContentArrowR'),
mbcArrowL=$('div.mainBodyContentArrowL'),
visible=2,//设置将可见的项目数
index=0,//开始索引
endIndex=($item.length/visible)-1;//结束索引
mbcArrowR.单击(函数(){
如果(index=endIndex);
});
mbcArrowL.click(函数(){
如果(索引>0){//可以滚动
指数-=1;
$item.animate({
“左”:“+=592px”
});
mbcArrowR.removeClass('disable');
}

mbcArrowL.toggleClass('disable',index这在哪里适用,我的错误正在发生?你能更具体一点吗?是~!!和否…(因为我必须在代码的水平滚动函数上实现这一点,似乎等式导致它“过度滚动”)如果没有其他内容,它需要停止,但它需要执行一个额外的步骤,并在没有显示任何内容的情况下进行动画,然后禁用。但这仍然非常接近。谢谢!添加了更多代码。这应该可以解决问题。请在我从电话中键入代码格式。它还需要一行,以解决以下关于禁用滚动的行左:如果(索引>=0)。现在它允许=0,它正在启用额外的滚动(你修复了向右的滚动)。但是如果你移除了它,它会与addClass项上的修复一起拧紧…嗯…太近了。我必须考虑这个问题。再次更新答案。将if语句中的0更改为1。你=那个人。谢谢Suhas!
if(($item.length % visible) == 0){
    enIndex = endIndex - 1;
}
$(document).ready(function () {
    'use strict';
    var $item = $('div.mainBodyContentListItem'), //Cache your DOM selectors
        mbcArrowR = $('div.mainBodyContentArrowR'),
        mbcArrowL = $('div.mainBodyContentArrowL'),
        visible = 2, //Set the number of items that will be visible
        index = 0, //Starting index
        endIndex = ($item.length / visible) - 1; //End index
    mbcArrowR.click(function () {
        if (index < endIndex) { //can scroll
            index += 1;
            $item.animate({
                'left': '-=592px'
            });
            mbcArrowL.removeClass('disable');
        }
        mbcArrowR.toggleClass('disable', index >= endIndex);
    });
    mbcArrowL.click(function () {
        if (index > 0) { //can scroll
            index -= 1;
            $item.animate({
                'left': '+=592px'
            });
            mbcArrowR.removeClass('disable');
        }
        mbcArrowL.toggleClass('disable', index <= 0);
    });
});​