javascript - jQuery plugin vertical align images setTimeout -


i have created quick , simple plugin vertical aligns images have used in number of websites although working new cms automatically resizes images creates delay in loading resized images causing plugin return null height. happens on first load of page.

i thought fix timeout although causes parent.height return null.

//vertically allign images jquery.fn.valign = function() {     return this.each(function(){         settimeout(function(){             var $strip = jquery(this);             var ah = $strip.height();             var ph = $strip.parent().height();             alert('height = '+ah+' parent = '+ph);             //height = 429 parent = null             var mh = math.ceil((ph-ah) / 2);             $strip.css('margin-top', mh);         },1000);     });  }; 

inside settimeout function, this window object (the settimeout function runs in global scope), , $(this).parent() empty jquery object (because window has no parent).

it best if can load images using javascript can assign load event handler vertical alignment, like:

jquery.fn.valign = function () {     return this.bind('load', function () {         // alignment     }); };  $('<img />')     .valign() // call before assigning src     .appendto('body')     .attr('src', '...'); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -