Solved

jquery script to center image

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now