Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

prepare pull request #26 to be merged with updated codebase

  • Loading branch information...
commit c461135cf088b5bca0e906ae54568ab162a7999f 1 parent 1c2bac7
benatkin authored July 07, 2012

Showing 1 changed file with 27 additions and 4 deletions. Show diff stats Hide diff stats

  1. 31  src/retina.js
31  src/retina.js
@@ -70,11 +70,12 @@ root.RetinaImage = RetinaImage;
70 70
 // Applies the dimensions of the existing image
71 71
 // to the image with the new image path
72 72
 RetinaImage.prototype.swap = function(path) {
  73
+  var that = this, hidden = false, width, height;
  74
+
73 75
   if (typeof path == 'undefined') path = this.path.at_2x_path;
74 76
 
75 77
   // We wrap this in a named self-executer so we can reference 
76 78
   // it in a setTimeout if the image has not loaded yet.
77  
-  var that = this;
78 79
   function load() {
79 80
     if (! that.el.complete) {
80 81
       // Check that the image has loaded.
@@ -85,12 +86,34 @@ RetinaImage.prototype.swap = function(path) {
85 86
       // and still have the script detect image loads reliably and efficiently.
86 87
       setTimeout(load, 5);
87 88
     } else {
  89
+      // If offsetWidth is 0, the image is probably hidden
  90
+      width = that.el.offsetWidth;
  91
+      if (!width || width == "0") {
  92
+        // try to get width from the image attribute
  93
+        width = el.getAttribute('width')
  94
+        if (!width || width == "0") {
  95
+          hidden = true;
  96
+        }
  97
+      }
  98
+
  99
+      // same for height
  100
+      height = that.el.offsetHeight;
  101
+      if (!height || height == "0") {
  102
+        // try to get width from the image attribute
  103
+        height = el.getAttribute('height')
  104
+        if (!height || height == "0") {
  105
+          hidden = true;
  106
+        }
  107
+      }
  108
+
88 109
       // Once the the image has loaded we know we 
89 110
       // can grab the proper dimensions of the original image
90 111
       // and go ahead and swap in the new image path and apply the dimensions
91  
-      that.el.setAttribute('width', that.el.offsetWidth);
92  
-      that.el.setAttribute('height', that.el.offsetHeight);
93  
-      that.el.setAttribute('src', path);
  112
+      if (! hidden) {
  113
+        that.el.setAttribute('width', width);
  114
+        that.el.setAttribute('height', height);
  115
+        that.el.setAttribute('src', path);
  116
+      }
94 117
     }
95 118
   }
96 119
   load();

0 notes on commit c461135

Please sign in to comment.
Something went wrong with that request. Please try again.