Autoresize div to fit inner image
Hello,
I write a plugin to resize the outer div to fit the image (so that the caption wraps properly). The markup is something like this:
- <div class="image-container image-center">
- <a href="url/to/orig"><img src="url/to/scaled" alt="Caption"/></a>
- <div>Caption</div>
- </div>
My jQuery code is:
- // Auto resize div container of image
- (function($){
- $.fn.autoresize = function() {
- if (this.width() > 0) {
- this.parent().parent().width(this.width());
- }
- else {
- setTimeout(this.autoresize(), 500));
- }
- };
- })(jQuery);
- Drupal.behaviors.autoResize = function (context) {
- $('.image-container img').autoresize();
- };
But it sometimes it doesn't work as expected: the div width can be 0 or 28px (I don't know from where this comes). The Drupal.behaviors is called after the dom ready.
I'm a newcomer to jQuery. Please help.