common_lazyload.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. window.lazySizesConfig = window.lazySizesConfig || {};
  2. window.lazySizesConfig.init = false;
  3. // 图片懒加载 lazysizes
  4. $(document).ready(function () {
  5. lazySizes.init();
  6. /*
  7. 手动加载不在可视区范围的图片
  8. 1:直接给img添加class="lazypreload"
  9. $("img").addClass('lazypreload');
  10. 2: 调用lazySizes的Api
  11. lazySizes.loader.unveil(Element);
  12. */
  13. // 页面加载之后 加载不在可视区的所有使用懒加载的图
  14. setTimeout(function(){
  15. var images = document.querySelectorAll('.lazyload');
  16. images.forEach((item)=>{
  17. lazySizes.loader.unveil(item);
  18. });
  19. },1000)
  20. });
  21. // <!--图片懒加载 IntersectionObserver-->
  22. // var io = new IntersectionObserver(callback, {
  23. //
  24. // });
  25. // let images = document.querySelectorAll('[data-src]');
  26. // images.forEach((item)=>{ // io.observe接受一个DOM元素,添加多个监听 使用forEach
  27. // io.observe(item);
  28. // });
  29. // function callback(entries){
  30. // entries.forEach((item) => { // 遍历entries数组
  31. // if(item.isIntersecting){ // 当前元素可见
  32. // item.target.src = item.target.dataset.src; // 替换src
  33. // io.unobserve(item.target) // 停止观察当前元素 避免不可见时候再次调用callback函数
  34. // }
  35. // })
  36. // }