vue自定义指令实现图片懒加载

Vue.directive("lazyload", {
        bind: function (el, binding) {
          console.log(el);
          let lazyLoadObser = new IntersectionObserver((entries, observer) => {
            entries.forEach((entry, idx) => {
              let lazyImg = entry.target;
              if (entry.intersectionRatio > 0) {
                lazyImg.src = binding.value;
                lazyLoadObser.unobserve(lazyImg); //卸载监听
              }
            });
          });
          lazyLoadObser.observe(el);
        },
      });

你可能感兴趣的:(vue自定义指令实现图片懒加载)