Created
January 7, 2017 12:15
-
-
Save AnkurVyas-BTC/155b59cf17f56d5f456a393533587ea2 to your computer and use it in GitHub Desktop.
Code for Progressive Image Loading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| window.onload = function() { | |
| var placeholder = $('.placeholder'); | |
| placeholder.each( function(index) { | |
| // 1: load small image and show it | |
| var smallImgElement = $(this).find('.img-small'); | |
| console.log(smallImgElement); | |
| var img = new Image(); | |
| img.src = smallImgElement.attr('src'); | |
| img.onload = function () { | |
| smallImgElement.addClass('loaded'); | |
| }; | |
| // 2: load large image | |
| var imgLarge = new Image(); | |
| imgLarge.src = $(this).data('large'); | |
| imgLarge.onload = function () { | |
| imgLarge.classList.add('loaded'); | |
| }; | |
| $(this).append(imgLarge); | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment