I am building a custom child theme built on the Genesis Framework and I am enqueueing a jQuery script that calculates image bottom margin based on the height of the image (only in the “diary” category). The calculation seems to be working properly, but the CSS is being modified inconsistently. Sometimes when I navigate to the page it works properly, other times it doesn’t. If I refresh the page, it usually doesn’t work. This is happening in Safari, Chrome, Firefox (all on a Mac).
This is how I am enqueuing the script in functions.php:
add_action( 'wp_enqueue_scripts', 'lpt_enqueue_script' );
function lpt_enqueue_script() {
wp_enqueue_script( 'variable-margins', get_stylesheet_directory_uri() . '/js/variable-margins.js', array( 'jquery' ), '', false );
}
The script looks like this:
jQuery(document).ready(
function() {
if (jQuery('.category-diary img').length > 0){
var lineHeight = 21;
jQuery('.category-diary img').each(
function() {
var remainder = jQuery(this).height() % lineHeight;
jQuery(this).css({'margin-bottom': (remainder - 5 + (lineHeight)) + 'px'});
});
};
});
The url for one of the single pages where the images should be affected is: http://lovelandprovincetown.com/this-is-a-test-post/.
Any ideas why this isn't working properly?
I should mention that the desired outcome of this script is variable image bottom margins in order to preserve the line-height so that the text in the left and right columns are aligned with each other. Perhaps there is another method to achieve this?