KouiK
KouiK

Reputation: 116

Trying to change 'margin-top' attribute

This is what I'm trying to do :

$(function(){           
  $('div.item').css('margin-top',-(closest('img').height())/2+'px'); 
});

I want my '<div class="item"'> to get style="margin-top:XXXXXX; top:50%" where 'XXXXXX' is equal to (('div.item a img').height())/2

======================================================

EDIT : ( solved ) Final code for those who are interested :

<script type="text/javascript">

$(function(){
theWindow.resize(function() {
$('div.item').each(function(i){          
  $(this).css('top',(($(window).height())/2)-(($(this).first('img').height())/2)+'px'); 
});
}).trigger("resize");
});    
</script>

( i used only top attribute instead of top + margin-top ) Thanks for help

Upvotes: 1

Views: 2167

Answers (2)

Jason Gennaro
Jason Gennaro

Reputation: 34855

I think this might be what you need, especially if you have more than one div.item

$('div.item').each(function(){
    var r = $(this).find('img').first().height();
    $(this).css('margin-top', r/2 + 'px'); 
}); 

EDIT

You need to .find() the img and then select the .first() one.

Example: http://jsfiddle.net/jasongennaro/JNMhC/3/

Upvotes: 2

gion_13
gion_13

Reputation: 41533

From what I can tell, you are trying to do something like this :

$(function(){           
  $('div.item').css('margin-top',-($('div.item').closest('img').height())/2+'px'); 
});

But the image selector depends on your html configuration.
If you post some html markup, maby I could provide a better solution.

Upvotes: 0

Related Questions