user7263531
user7263531

Reputation:

Setting margin-left to a certain value if less than 0

I want to set an object's css margin-left property to 1000 if it's lower than 0, but it isn't working as I want.

This is the code I'm using:

$(".slide").each(function(a, b) {
  m=$(this).css("margin-Left");
  if (m <= "0px") {
    console.log($(this).css("margin-left"));
    $(this).css({"margin-left": width + "px"});
  }
});

Upvotes: 1

Views: 166

Answers (2)

Roham Tehrani
Roham Tehrani

Reputation: 2989

You can use setInterval() to change the margin-left more than one time:

setInterval(function(){
   $(".slide").each(function(a,b){
      m = parseInt($(this).css("margin-left"));
      if(m <= 0) {
         console.log($(this).css("margin-left"));
         $(this).css({"margin-left": "1000px"});
      }
   });
}, 500);

Upvotes: 0

Vitalii Chmovzh
Vitalii Chmovzh

Reputation: 2943

The issue here is that .css('margin-left') will return you a string. Change your code in the following way:

$(".slide").each(function(a,b){
   m = parseInt($(this).css("margin-left"));
   if(m <= 0) {
      console.log($(this).css("margin-left"));
      $(this).css({"margin-left":width+"px"});
   }
});

Upvotes: 2

Related Questions