Comparing version 0.1.3 to 0.1.5
{ | ||
"name": "lazyimg", | ||
"version": "0.1.3", | ||
"version": "0.1.5", | ||
"description": "图片懒加载", | ||
@@ -12,3 +12,3 @@ "main": "src/adapter.js", | ||
{ | ||
"name": "mr.raindrop", | ||
"name": "_danz", | ||
"email": "tekkahs@gmail.com" | ||
@@ -15,0 +15,0 @@ } |
/* | ||
lib-img-adpter | ||
Author: kongshi.wl@alibaba-inc.com | ||
Date: Dec,2015 | ||
lazyimg | ||
Author: _danz | ||
*/ | ||
@@ -31,2 +30,15 @@ ; | ||
function preLoadImg (src, loadCallback, errorCallback) { | ||
var img = new Image(); | ||
img.src = src | ||
img.onload = loadCallback ? loadCallback.bind(img) : null | ||
img.onerror = errorCallback ? errorCallback.bind(img) : null | ||
} | ||
function dispatchEvent (item, type, data) { | ||
var evt = document.createEvent('CustomEvent') | ||
evt.initCustomEvent(type, false, false, data) | ||
item.dispatchEvent(evt) | ||
} | ||
function applySrc(item, processedSrc, placeholderSrc) { | ||
@@ -46,7 +58,12 @@ if (!processedSrc) { | ||
} else { | ||
var bgStr = 'url("' + processedSrc + '")'; | ||
if (placeholderSrc) { | ||
bgStr += ',url("' + placeholderSrc + '")'; | ||
} | ||
item.style.backgroundImage = bgStr; | ||
preLoadImg(processedSrc, function () { | ||
item.style.backgroundImage = 'url("' + processedSrc + '")'; | ||
item.naturalWidth = this.width | ||
item.naturalHeight = this.height | ||
dispatchEvent(item, 'load') | ||
}, function () { | ||
preLoadImg(placeholderSrc, function () { | ||
item.style.backgroundImage = 'url("' + placeholderSrc + '")'; | ||
}) | ||
}) | ||
} | ||
@@ -53,0 +70,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7679
99