15 |
16 | ```
17 |
18 | 3) Add CSS3 magic for an animated fade-in:
19 |
20 | ```css
21 | .lazy-load, .lazy-loaded {
22 | -webkit-transition: opacity 0.3s;
23 | -moz-transition: opacity 0.3s;
24 | -ms-transition: opacity 0.3s;
25 | -o-transition: opacity 0.3s;
26 | transition: opacity 0.3s;
27 | opacity: 0;
28 | }
29 |
30 | .lazy-loaded { opacity: 1; }
31 | ```
32 |
33 | # Demo
34 |
35 | http://kaizau.github.com/Lazy-Load-Images-without-jQuery/
36 |
--------------------------------------------------------------------------------
/javascripts/lazyload.min.js:
--------------------------------------------------------------------------------
1 | /*!
2 | * Lazy Load Images without jQuery
3 | * http://kaizau.github.com/Lazy-Load-Images-without-jQuery/
4 | *
5 | * Original by Mike Pulaski - http://www.mikepulaski.com
6 | * Modified by Kai Zau - http://kaizau.com
7 | */
8 | !function(){function d(a){var b=0;if(a.offsetParent){do b+=a.offsetTop;while(a=a.offsetParent);return b}}var a=window.addEventListener||function(a,b){window.attachEvent("on"+a,b)},b=window.removeEventListener||function(a,b){window.detachEvent("on"+a,b)},c={cache:[],mobileScreenSize:500,addObservers:function(){a("scroll",c.throttledLoad),a("resize",c.throttledLoad)},removeObservers:function(){b("scroll",c.throttledLoad,!1),b("resize",c.throttledLoad,!1)},throttleTimer:(new Date).getTime(),throttledLoad:function(){var a=(new Date).getTime();a-c.throttleTimer>=200&&(c.throttleTimer=a,c.loadVisibleImages())},loadVisibleImages:function(){for(var a=window.pageYOffset||document.documentElement.scrollTop,b=window.innerHeight||document.documentElement.clientHeight,e={min:a-200,max:a+b+200},f=0;fA totally lame name for a totally useful script.
42 | 43 |View the Project on GitHub kaizau/Lazy-Load-Images-without-jQuery
44 | 45 |Lazy load your images without the overhead of a framework. Optionally, send mobile-optimized images to smaller screens. Tested on IE7+, Firefox, Chrome, iOS.
55 | 56 |Based on code from Mike Pulaski.
57 | 58 |Include lazyload.min.js or inline it.
Add .lazy-load and data-src to each of your <img> tags. Optionally add data-src-mobile, a placeholder src, and a fallback image.
<img class="lazy-load" data-src="lazy.jpg" data-src-mobile="lazy-small.jpg" src="blank.gif" />
66 | <noscript><img src="lazy.jpg" /></noscript>
67 |
68 | Add CSS3 for an animated fade-in.
72 |.lazy-load, .lazy-loaded {
74 | -webkit-transition: opacity 0.3s;
75 | -moz-transition: opacity 0.3s;
76 | -ms-transition: opacity 0.3s;
77 | -o-transition: opacity 0.3s;
78 | transition: opacity 0.3s;
79 | opacity: 0;
80 | }
81 |
82 | .lazy-loaded { opacity: 1; }
83 |
84 | Open up your Developer Tools / Firebug to peek at the network.
91 |On mobile, image order will be swapped (different images used).
92 |Images courtesy of NASA.
93 | 94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |