Skip to content

Commit cb69785

Browse files
committed
Update to handle background images
1 parent 8aa8f06 commit cb69785

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

jquery.cacheImages.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @author Paul Prins
88
* @link http://paulprins.net
9-
* @version 1.5.0
9+
* @version 1.6.0
1010
* @requires jQuery v1.7 or later
1111
*
1212
* Official jQuery plugin page:
@@ -43,10 +43,24 @@
4343
// Check if we can actually continue with this
4444
$.fn.cacheImages.storageAvailable( $(img), i, img, function(i, img){
4545
var $this = $(img),
46-
src = $this.prop('src') || $this.data('cachedImageSrc');
47-
if( self.cacheImagesConfig.url !== null ){ // URL set in the opts
48-
src = self.cacheImagesConfig.url;
49-
$this.prop('src', '');
46+
src;
47+
48+
if( $this.prop("tagName") === 'img' ){
49+
$this.data('cachedImageType', 'src');
50+
51+
var src = $this.prop('src') || $this.data('cachedImageSrc');
52+
if( self.cacheImagesConfig.url !== null ){ // URL set in the opts
53+
src = self.cacheImagesConfig.url;
54+
$this.prop('src', '');
55+
}
56+
}else{
57+
$this.data('cachedImageType', 'css');
58+
59+
var src = $this.css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "") || $this.data('cachedImageSrc');
60+
if( self.cacheImagesConfig.url !== null ){ // URL set in the opts
61+
src = self.cacheImagesConfig.url;
62+
$this.css('background-image', 'url()');
63+
}
5064
}
5165

5266

@@ -62,7 +76,12 @@
6276
if( localSrcEncoded && /^data:image/.test( localSrcEncoded ) ) {
6377
// Check if the image has already been cached, if it has lets bounce out of here
6478
this.data('cachedImageSrc', src);
65-
this.prop('src', localSrcEncoded);
79+
if( this.data('cachedImageType') == 'src' ){
80+
this.prop('src', localSrcEncoded );
81+
}else{
82+
this.css('background-image', 'url(' + localSrcEncoded + ')')
83+
}
84+
6685
self.cacheImagesConfig.done.call( this, localSrcEncoded );
6786
self.cacheImagesConfig.always.call( this );
6887
return;
@@ -73,7 +92,12 @@
7392
return; // stop running
7493
}else{
7594
// The image has not yet been cached, so we need to get on that.
76-
this.prop('src', ''); // This will cancel the request if it hasn't already been finished
95+
96+
if( this.data('cachedImageType') == 'src' ){ // This will cancel the request if it hasn't already been finished
97+
this.prop('src', '' );
98+
}else{
99+
this.css('background-image', 'url()');
100+
}
77101
var imgType = src.match(/\.(jpg|jpeg|png|gif)$/i); // Break out the filename to get the type
78102
if( imgType && imgType.length){ // Get us the type of file
79103
imgType = imgType[1].toLowerCase() == 'jpg' ? 'jpeg' : imgType[1].toLowerCase();
@@ -94,7 +118,12 @@
94118
newSrc = $.fn.cacheImages.base64EncodeCanvas( img ); // Process the image
95119
$.fn.cacheImages.set( this, key, newSrc ); // Save the media
96120
if( src.substring(0,5) === 'data:' ){
97-
this.prop('src', newSrc );
121+
if( this.data('cachedImageType') == 'src' ){
122+
this.prop('src', newSrc );
123+
}else{
124+
this.css('background-image', 'url(' + newSrc + ')')
125+
}
126+
98127
if( this.is('.cacheImagesRemove') ){
99128
this.remove();
100129
}
@@ -115,11 +144,14 @@
115144
$.fn.cacheImages.set( thisElem, key, newSrc, function( key, encodedString ){
116145
// Default processing of the response
117146
if( encodedString.length !== 0 && encodedString !== 'data:image/' + imgType + ';base64,'){ // it appended image data
118-
this.prop('src', encodedString );
119-
if( this.is('.cacheImagesRemove') ){
120-
this.remove();
147+
console.log( this, this.data('cachedImageType'), newSrc );
148+
if( this.data('cachedImageType') == 'src' ){
149+
this.prop('src', encodedString );
150+
}else{
151+
this.css('background-image', 'url(' + encodedString + ')')
121152
}
122153

154+
if( this.is('.cacheImagesRemove') ){ this.remove(); }
123155
self.cacheImagesConfig.done.call( this, false );
124156
self.cacheImagesConfig.always.call( this );
125157
return;
@@ -133,7 +165,12 @@
133165
this.cacheImages(self.cacheImagesConfig);
134166
return; // stop progression
135167
}else{
136-
this.prop('src', self.cacheImagesConfig.defaultImage );
168+
if( this.data('cachedImageType') == 'src' ){
169+
this.prop('src', self.cacheImagesConfig.defaultImage );
170+
}else{
171+
this.css('background-image', 'url(' + self.cacheImagesConfig.defaultImage + ')')
172+
}
173+
137174
self.cacheImagesConfig.fail.call( this );
138175
self.cacheImagesConfig.always.call( this );
139176
}

jquery.cacheImages.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)