Reputation: 441
I've been trying to tweak things a bit to get this to work and so far I haven't been able to use jquery to check a div
's left margin and on the condition it is in the left: 0px
position, to output whatever action, in my case to apply opacity to a separate div. Here's my jfiddle. In the jfiddle I want to check the gray div "trim", and on the condition it has marginLeft: 0px
, to apply the opacity to the div "l1". Let me know if I need to explain this more.
Upvotes: 0
Views: 3982
Reputation: 144689
jQuery object doesn't have marginLeft
property, you should use css
method, also note that =
is a assignment operator which valuates to true
, you should use ==
or ===
comparison operators instead.
$(document).ready(function(){
if ( $('#trim').css('marginLeft') == '0px' ){
$('#l1').animate({opacity:".5"});
} else {
$('#l1').animate({opacity:"1"});
}
}); // You are missing `)` here
Upvotes: 3
Reputation: 603
Well, in the fiddle you're doing
$('#trim').marginLeft
How about this instead:
$('#trim').attr('marginLeft')
Upvotes: -1
Reputation: 11382
Use the jQuery .css() method:
$(document).ready(function(){
if($('#trim').css("margin-left") == "0px"){
$('#l1').animate({opacity:".5"});
}else{
$('#l1').animate({opacity:"1"});
}
});
Upvotes: 1