Posted By

digitalclubb on 04/16/12


Tagged

image plugin jquery javscript loader


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hurcy


unveil.js - a lightweight version of the 'lazy load' plugin


 / Published in: JavaScript
 

Via @lmgalmeida

Usage:

data-src="image.jpg" src="loader.gif"

$("img").unveil();

  1. (function( $ ) {
  2. $.fn.unveil = function () {
  3. var images = this, loaded, inview;
  4.  
  5. this.one("unveil", function(){
  6. this.setAttribute( "src", this.getAttribute( "data-src" ) );
  7. this.removeAttribute( "data-src" );
  8. });
  9.  
  10. function unveil () {
  11. inview = images.filter(function(){
  12. var $e = $(this),
  13. $w = $(window),
  14. wt = $w.scrollTop(),
  15. wb = wt + $w.height(),
  16. et = $e.offset().top,
  17. eb = et + $e.height();
  18.  
  19. return eb >= wt && et <= wb;
  20. });
  21.  
  22. loaded = inview.trigger("unveil");
  23. images = images.not( loaded );
  24. }
  25.  
  26. $(window).scroll(unveil);
  27. unveil();
  28.  
  29. return this;
  30. };
  31. })( jQuery );

Report this snippet  

You need to login to post a comment.