loadimg.js 753 B

12345678910111213141516171819202122232425262728293031
  1. var aImages = document.getElementById("loadimg").getElementsByTagName('img');
  2. loadImg(aImages);
  3. window.onscroll = function() {
  4. loadImg(aImages);
  5. };
  6. function loadImg(arr) {
  7. for(var i = 0, len = arr.length; i < len; i++) {
  8. if(arr[i].getBoundingClientRect().top < document.documentElement.clientHeight && !arr[i].isLoad) {
  9. arr[i].isLoad = true;
  10. //arr[i].style.cssText = "opacity: 0;";
  11. (function(i) {
  12. setTimeout(function() {
  13. if(arr[i].dataset) {
  14. aftLoadImg(arr[i], arr[i].dataset.imgurl);
  15. } else {
  16. aftLoadImg(arr[i], arr[i].getAttribute("data-imgurl"));
  17. }
  18. arr[i].style.cssText = "transition: 1s; opacity: 1;"
  19. }, 500)
  20. })(i);
  21. }
  22. }
  23. }
  24. function aftLoadImg(obj, url) {
  25. var oImg = new Image();
  26. oImg.onload = function() {
  27. obj.src = oImg.src;
  28. }
  29. oImg.src = url;
  30. }