Warning: fopen(/a15f344d0d1c5df6a6ca7180818dc45ef2cba8cc) [function.fopen]: failed to open stream: Permission denied in /var/sites/s/seo-creative.co.uk/public_html/wp-content/plugins/fv-code-highlighter/Includes/Cache.php on line 137
Warning: fopen(/b6758eb16c6ea701b4ed67bdeef27b668778fd01) [function.fopen]: failed to open stream: Permission denied in /var/sites/s/seo-creative.co.uk/public_html/wp-content/plugins/fv-code-highlighter/Includes/Cache.php on line 137
I recently came across a web design issue where I basically wanted two columns, where one of which had a dynamic height. The height of this container would be worked out via a piece of jQuery. This piece of jQuery would check the height of one div (the div that contains our blog feed in this case) and then, if larger than the css height, expand the div height so they are equal length. There may be many reasons why you would want to do this, the one I have given was the situation I was in.
First off as I was working with WordPress I added the jQuery source into my wordpress header by placing the following code in thesection of the header.php file.
<?php wp_enqueue_script("jquery"); ?>
Next up we need to go to our footer.php file and copy in the jQuery that is going to do the work for us:
1 2 3 4 5 6 7 8 9 10 11
Lets break that code down so that we can understand it. Firstly in the above code all that will need changing are the div locators. In my example ‘#sidebar’ is the div that I want to auto expand and ‘#main’ is the div that jQuery will get is height from. Remember if you need to change these locators # is for ID and . is for class.
We need to wrap out code within a function otherwise it will never get called, for this type of script we are best to put it within the document ready function. In this instance when the page is loaded/refreshed this script will be ran.
The Math function is basically saying “Calculate.whichismore(locate(#sidebar).grabheight,locate(#main).grabheight)” Hopefully that makes it easier to understand. Either the height of #sidebar or #main will be stored within the variable ‘highestCol’ depending on which is largest.
Next up the jQuery will locate the div #sidebar and change the height property to whatever value is stored within the variable ‘highestCol’.
This should work like a dream, Happy coding!