简体   繁体   中英

jQuery // why is this if-statement incorrect?

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.

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.

Code snippet

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

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

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

OR

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

or use parseInt in order to get the value without px

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

Updated with suggestions.

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

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

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

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

Try using this instead of your if.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM