From a149b03a2ff5c9d62236c98431f315cd9e16cd73 Mon Sep 17 00:00:00 2001 From: jeremija Date: Sun, 14 Sep 2014 09:33:33 +0200 Subject: [PATCH] Fixes variable image width bug closes #95 - added onload event listeners to images --- assets/js/index.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 8df69da..64b0191 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -37,19 +37,22 @@ var $postContent = $(".post-content"); $postContent.fitVids(); - var casperFullImg = function () { - $("img").each(function () { - var $this = $(this), - contentWidth = $postContent.outerWidth(), // Width of the content - imageWidth = $this[0].naturalWidth; // Original image resolution + function updateImageWidth() { + var $this = $(this), + contentWidth = $postContent.outerWidth(), // Width of the content + imageWidth = this.naturalWidth; // Original image resolution - if (imageWidth >= contentWidth) { - $this.addClass('full-img'); - } else { - $this.removeClass('full-img'); - } - }); - }; + if (imageWidth >= contentWidth) { + $this.addClass('full-img'); + } else { + $this.removeClass('full-img'); + } + } + + var $img = $("img").on('load', updateImageWidth); + function casperFullImg() { + $img.each(updateImageWidth); + } casperFullImg(); $(window).smartresize(casperFullImg);