jQuery //为什么这个if语句不正确?

时间:2022-09-22 20:28:02

In my html I have a div with 7 images listed but in the div "fit" only 5, so I created 2 buttons (bt1 and bt2) to "scroll horizontally" through these images.

在我的html中,我有一个div,其中列出了7个图像,但在div“fit”中只有5个,所以我创建了2个按钮(bt1和bt2)来“水平滚动”这些图像。

What I want is when I click on bt2 AND the first image is on the first position (in this case when left is 0px), the images shouldn't be able to scroll further, else they just scroll normally.

我想要的是当我点击bt2并且第一个图像位于第一个位置时(在这种情况下左边是0px),图像不能进一步滚动,否则它们只是正常滚动。

Code snippet

$("#bt2").click(function()
 {
    if (($("#symbole").find("img").first().css({"left:0px"}))==true)
          {         
          }
    else{
          $("img").animate({
          "left":"+=100px"
          },
        420);
    }
 });

4 个解决方案

#1


5  

The css method does not return boolean true / false so that you can compare it with true as you did. You need to get css value for left but you are setting it

css方法不会返回布尔值true / false,因此您可以将其与true进行比较。您需要获取左侧的css值,但您正在设置它

if (($("#symbole").find("img").first().css("left") === "0px")

OR

if($("#symbole img:eq(0)").css("left") === "0px") 

#2


2  

or use parseInt in order to get the value without px

或者使用parseInt获取没有px的值

if (parseInt($("#symbole").find("img").first().css("left"), 10) === 0)

Updated with suggestions.

更新了建议。

#3


1  

Currently you are setting the css. You need to get the css attribute left and then compare it.

目前你正在设置CSS。您需要保留css属性,然后进行比较。

 if ($("#symbole").find("img").first().css("left") == "0px")

#4


0  

Css does not return true/false.you need to compare it with value like:

Css不返回true / false。您需要将其与值进行比较,如:

if (($("#symbole").find("img").first().css("left") === "0px")

Try using this instead of your if.

尝试使用此而不是if。

#1


5  

The css method does not return boolean true / false so that you can compare it with true as you did. You need to get css value for left but you are setting it

css方法不会返回布尔值true / false,因此您可以将其与true进行比较。您需要获取左侧的css值,但您正在设置它

if (($("#symbole").find("img").first().css("left") === "0px")

OR

if($("#symbole img:eq(0)").css("left") === "0px") 

#2


2  

or use parseInt in order to get the value without px

或者使用parseInt获取没有px的值

if (parseInt($("#symbole").find("img").first().css("left"), 10) === 0)

Updated with suggestions.

更新了建议。

#3


1  

Currently you are setting the css. You need to get the css attribute left and then compare it.

目前你正在设置CSS。您需要保留css属性,然后进行比较。

 if ($("#symbole").find("img").first().css("left") == "0px")

#4


0  

Css does not return true/false.you need to compare it with value like:

Css不返回true / false。您需要将其与值进行比较,如:

if (($("#symbole").find("img").first().css("left") === "0px")

Try using this instead of your if.

尝试使用此而不是if。