Reputation: 2399
Hey community im doing this centering of pictures in a frame so the center of the image is in the center of the frame despite its size. (frame is fixed)
$('.item-box').each(function () {
var fwidth = $('.gmask').width();
var iwidth = $('.gmask img').width();
var fheight = $('.gmask').height();
var iheight = $('.gmask img').height();
$('.gmask img').css('margin-left', Math.floor((fwidth - iwidth) / 2) - 3);
$('.gmask img').css('margin-top', Math.floor((fheight - iheight) / 2) - 3);
});
I wanna do this as a loop for each of my '.item-box' can anyone tell me what im doing wrong
*EDIT
btw im doing jquery template
<script id="resultTemplate" type="text/x-jquery-tmpl ">
{{for Product}}
<div class="item-box" id="{{if #index < 5 }}itemboxTeaser{{else}}itemboxRest{{/if}}{{:#index+1}}">
<div class="visual-gallery">
<div class="gmask">
<ul class="replaceClass">
<a href="/lejemaal/{{:Urlname}}/#/Lejemaal">
<img src="{{:PhysicalPathToFrontPhotoUseWebFront300Px}}" alt="">
</a>
</ul>
</div>
</div>
</div>
{{/for}}
</script>
Upvotes: 3
Views: 132
Reputation: 206121
I suppose .gmask
and it's relative img
are children of $(this)
(your .item-box
)
Try like:
$('.item-box').each(function () {
var $gmask = $(this).find('.gmask');
var $gmaskImg = $gmask.find('img');
var fwidth = $gmask.width();
var iwidth = $gmaskImg.width();
var fheight = $gmask.height();
var iheight = $gmaskImg.height();
$gmaskImg.css('margin-left', Math.floor((fwidth - iwidth) / 2) - 3);
$gmaskImg.css('margin-top', Math.floor((fheight - iheight) / 2) - 3);
});
EDIT after reading OP comment
$(window).load(function(){
$('.item-box').find('.gmask').each(function () {
var $gmask = $(this);
var $gmaskImg = $gmask.find('img');
var fwidth = $gmask.width();
var iwidth = $gmaskImg.width();
var fheight = $gmask.height();
var iheight = $gmaskImg.height();
$gmaskImg.css('margin-left', Math.floor((fwidth - iwidth) / 2) - 3);
$gmaskImg.css('margin-top', Math.floor((fheight - iheight) / 2) - 3);
});
});
Upvotes: 4
Reputation: 76395
Since the img is what you're after, why not .each
-loop over those elements?
$('.item-box .gmask img').each(function ()
{
var parent, fwidth,fheight,iwidth,iheight;
parent = $(this);
while(!parent.attr('class').match(/\bgmask\b/))
{//get the .gmask parent of current img
parent = parent.parent();
}
iwidth = $(this).width();
iheight = $(this).height;
fwidth = parent.width();
fheight = parent.height();
$(this).css('margin-left', Math.floor((fwidth - iwidth) / 2) - 3);
$(this).css('margin-top', Math.floor((fheight - iheight) / 2) - 3);
});
This reduces the amount of selectors used, thus increasing overall performance. IMHO, it's also easier to read and maintain.
Upvotes: 0
Reputation: 2658
Those lines are taking the first element of the page, whatever the current .item-box :
var fwidth = $('.gmask').width();
var iwidth = $('.gmask img').width();
var fheight = $('.gmask').height();
var iheight = $('.gmask img').height();
You need to do $(this).find('.element');
Upvotes: 0
Reputation: 9691
I guess you want to center the image inside each .item-box, so you need to refer to 'this' :
$('.item-box').each(function () {
var fwidth = $(this).find('.gmask').width();
var iwidth = $(this).find('.gmask img').width();
var fheight = $(this).find('.gmask').height();
var iheight = $(this).find('.gmask img').height();
$(this).find('.gmask img').css('margin-left', Math.floor((fwidth - iwidth) / 2) - 3);
$(this).find('.gmask img').css('margin-top', Math.floor((fheight - iheight) / 2) - 3);
});
Upvotes: 0