From 2458838d8f60a17f0e81dfc51231d765d83046bc Mon Sep 17 00:00:00 2001 From: tzws <147955613@qq.com> Date: Wed, 31 May 2017 18:26:39 +0800 Subject: [PATCH 1/3] fall back to default image when the image src is invalid --- jquery.unveil.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jquery.unveil.js b/jquery.unveil.js index 8cfeb25..916f494 100644 --- a/jquery.unveil.js +++ b/jquery.unveil.js @@ -25,6 +25,15 @@ if (source) { this.setAttribute("src", source); if (typeof callback === "function") callback.call(this); + } else if(errorSrc){ + this.setAttribute("src", errorSrc); + } + + this.onError = function () { + if(errorSrc) + { + this.setAttribute("src", errorSrc); + } } }); From fbfc9c50ec576ac6f7a1b6153690d3f7f8c76dc1 Mon Sep 17 00:00:00 2001 From: tzws <147955613@qq.com> Date: Wed, 31 May 2017 18:29:44 +0800 Subject: [PATCH 2/3] fall back to default image when the image src is invalid --- jquery.unveil.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.unveil.js b/jquery.unveil.js index 916f494..7aa92e7 100644 --- a/jquery.unveil.js +++ b/jquery.unveil.js @@ -10,7 +10,7 @@ ;(function($) { - $.fn.unveil = function(threshold, callback) { + $.fn.unveil = function(threshold, callback, errorSrc) { var $w = $(window), th = threshold || 0, From f077553385a1a207419864bac0fa42aa574eef9c Mon Sep 17 00:00:00 2001 From: tzws <147955613@qq.com> Date: Thu, 1 Jun 2017 10:28:31 +0800 Subject: [PATCH 3/3] fix the error hanle for invalid src & add a demo for empty src and invliad src --- Invalid-src-demo.html | 149 ++++++++++++++++++++++++++++++++++++++++++ jquery.unveil.js | 7 +- 2 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 Invalid-src-demo.html diff --git a/Invalid-src-demo.html b/Invalid-src-demo.html new file mode 100644 index 0000000..2b38329 --- /dev/null +++ b/Invalid-src-demo.html @@ -0,0 +1,149 @@ + + +
+ + + + + + +A very lightweight plugin to lazy load images for jQuery or Zepto.js
+ + + + Tweet +Compatible with All Browsers and IE7+.
+ +Download the script from the project page on GitHub.
+If you have any suggestion or bug to report please use github issues or get it touch with me on twitter - @lmgalmeida.
+ +Unveil is licensed under the MIT license.
+ + + + + diff --git a/jquery.unveil.js b/jquery.unveil.js index 7aa92e7..d962329 100644 --- a/jquery.unveil.js +++ b/jquery.unveil.js @@ -19,6 +19,7 @@ images = this, loaded; + this.one("unveil", function() { var source = this.getAttribute(attrib); source = source || this.getAttribute("data-src"); @@ -26,10 +27,10 @@ this.setAttribute("src", source); if (typeof callback === "function") callback.call(this); } else if(errorSrc){ - this.setAttribute("src", errorSrc); - } - this.onError = function () { + this.setAttribute("src", errorSrc); + } + this.onerror = function () { if(errorSrc) { this.setAttribute("src", errorSrc);