Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. // HTML
  2.  
  3. <a href="#" class="lazyimage">
  4. <div class="placeholder" data-large="fullsize.jpg">
  5. <img src="small.jpg" class="img-small">
  6. <div style="padding-bottom: 39.47%;"></div>
  7. <!-- % calculated from ratio eg. 450/1140px -->
  8. </div>
  9. </a>
  10.  
  11.  
  12. // CSS
  13.  
  14. .lazyimage {
  15. .placeholder {
  16. background-color: #ccc;
  17. background-size: cover;
  18. background-repeat: no-repeat;
  19. position: relative;
  20. overflow: hidden;
  21. }
  22.  
  23. .placeholder img {
  24. position: absolute;
  25. opacity: 0;
  26. top: 0;
  27. left: 0;
  28. width: 100%;
  29. transition: opacity 1s linear;
  30. }
  31.  
  32. .placeholder img.loaded {
  33. opacity: 1;
  34. }
  35.  
  36. .img-small {
  37. filter: blur(50px);
  38. transform: scale(1);
  39. }
  40. }
  41.  
  42.  
  43. // JS
  44.  
  45. if($(".lazyimage").length) {
  46. $(".lazyimage").each(function(index) {
  47.  
  48. var placeholder = $(this).find($(".placeholder")),
  49. small = $(this).find($(".img-small"));
  50.  
  51. var img = new Image();
  52. img.src = small.src;
  53. img.onload = function () {
  54. small.classList.add('loaded');
  55. };
  56.  
  57. var imgLarge = new Image();
  58. imgLarge.src = placeholder.attr("data-large");
  59. imgLarge.onload = function () {
  60. imgLarge.classList.add('loaded');
  61. };
  62. placeholder.append(imgLarge);
  63. });
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement