Solved

jquery script to center image

Posted on 2013-11-29
4
347 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

831 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