Solved

jquery script to center image

Posted on 2013-11-29
4
349 Views
Last Modified: 2013-12-01
hi i wrote a jquery script to add padding on images to center them within their container.

The script gets the containers height and the images height to then gets the difference in height

the difference in height is then applied as padding on the container to push the image down into the center of the container.

my problem is that it some times breaks and adds half of the containers height with out subtracting the images height.

this is a page that has the script on it:
http://toucanecoclean.co.uk/full-product-range/

all of the product blocks have there own script handling the padding with dynamically generated ids to target each image container.

sometimes it works and other times the images are messed up can any one tell me whats wrong with my code. thanks

the code:

								        <script>
												onResize = function() {
													setTimeout(function() {
													    var imageHeight = $('.image<?php echo $random; ?>').height();
													    var imageContainer = $('.image-inner<?php echo $random; ?>').height();
														
														

													    var padding = imageContainer - imageHeight;
														//alert (padding);
													    var paddingHalf = padding /2

													   $(".image-inner<?php echo $random; ?>").css('padding-top', paddingHalf);
													}, 100);
												}

												$(document).ready(onResize);
												//$(window).bind('resize load', onResize); // resize load !!!!
										</script>

Open in new window

0
Comment
Question by:helpchrisplz
4 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 39685665
Try changing height to innerHeight on imageContainer
Tho I think there is probably an easier CSS way to do this.
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 250 total points
ID: 39686251
Why not just style the container with

display:table-cell;vertical-align:middle;

so you don't need the scripting to manage variable dimensions or page re-sizing?

See this example of a div styled as a cell for detailed code if you need it.

Cd&
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39686421
Try out this one
.C5wrap img {
    height: auto;
    margin-top: -100px;
}
0
 
LVL 1

Author Closing Comment

by:helpchrisplz
ID: 39688462
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question