Solved

jquery script to center image

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now