How can I hide an element using jQuery based on the value of its margin-left in CSS?
I am developing a carousel style slider and want to hide one of the controls when the left margin ul
is -3480px. Here's my code:
$(function(){
if($("ul#listName").css("margin-left") == "-3480px"){
$(".nextButton").hide();
}
});
It doesn't do anything and I'm not sure what to do next. Anyone got any directions or suggestions?
source to share
Depending on which style property you are animating, you should check this. Also make sure to convert the value to int before comparing, because the method css()
will give one (px / em ..) along with its value as well.
if(parseInt($("ul#listName").css("margin-left"), 10) == -3480){
$(".nextButton").hide();
}
If you are executing this code in any animation callback, I suggest you check <=
instead ==
, because this value might not be perfect during animation. Try it.
if(parseInt($("ul#listName").css("margin-left"), 10) <= -3480){
$(".nextButton").hide();
}
source to share
I don't have your complete script, but I would recommend doing console.log()
on $("ul#listName").css("margin-left")
to see if it actually outputs what you think. I would also use <= in case you haven't reached this exact value.
I'm just working on the assumptions here, but hope this helps.
source to share