简体   繁体   English

Intersection Observer API:回调 function 未在交叉路口触发

[英]Intersection Observer API: Callback function is not triggered on intersection

There is a Vue custom directive for lazy loading有一个用于延迟加载的 Vue 自定义指令

 export default {
    mounted: el => {
      function loadImage() {
        console.log("loadImage called!")
        el.src = el.dataset.src
      }

      //loadImage();
  
      function handleIntersect(entries, observer) {
        entries.forEach(entry => {
          if (entry.isIntersecting) {
            loadImage();
            console.log("loadImage called! from handleIntersect")
            observer.unobserve(el);
          }
        });
      }

      function createObserver() {
        const options = {
          root: null,
          threshold: 0
        };  
        const observer = new IntersectionObserver(handleIntersect, options);
        observer.observe(el);
      }

      if (window["IntersectionObserver"]) {
        createObserver();
      } else {
        loadImage();
      }
    }
  };

Which is registered in Vue 3 component locally在本地注册在 Vue 3 组件中

<template>
    <figure>
      <img :data-src="src" @load="imageLoaded = true" v-show="imageLoaded">
     <Skeletor height="160" v-if="!imageLoaded"/>
    </figure>
    
</template>
<script>
import lazyload  from "../../../directives/lazyload"
export default {
  
  ...
  directives: {
    lazyload
  },
  data() {
    return {
      imageLoaded: false
    }
  }

}
</script>

The callback function handleIntersect is not triggered on Intersection and the images always have the data-src with the image url when I inspect them in the DevTools回调 function handleIntersect不会在 Intersection 上触发,当我在 DevTools 中检查图像时,图像始终具有带有图像 url 的 data-src

However, when I uncomment calling of loadImage() function in the directive, first it is called by each elements (144 altogether) and then I see that loadImage() is called by the callback function handleIntersect as the images enter the viewport.但是,当我在指令中取消注释调用loadImage() function 时,首先它被每个元素(总共 144 个)调用,然后我看到当图像进入视口时,回调 function handleIntersect调用了loadImage()

The example below has 3 images in the viewport下面的示例在视口中有 3 个图像

在此处输入图像描述

What is wrong with the code?代码有什么问题? Why callback function is called on Intersection once loadImages is fired for all elements and not fired at all when it is commented (console shows no output of "loadImage called?")?为什么一旦对所有元素触发 loadImages 并且在评论时根本不触发回调 function 在 Intersection 上调用(控制台显示没有“调用 loadImage 的 output?”)?

Appreciate any support!感谢任何支持!

The reason for event not being fired was that the img was zero width/heigh or just simply put non-existent (DevTools showed style='display: none;') for Intersection Observer.未触发事件的原因是img的宽度/高度为零,或者只是简单地为 Intersection Observer 设置了不存在(DevTools 显示 style='display: none;')。 When I uncommented loadImage() in the custom directive, it became visible and event was fired.当我在自定义指令中取消注释 loadImage() 时,它变得可见并且事件被触发。

The solution was to move v-lazyload to figure and search by img tag for the target element in the custome directive, so that figure always hosts a skeletor (component from vue-skeletor package)解决方案是将 v-lazyload 移动到 figure 并通过 img 标签搜索 custome 指令中的目标元素,以便该 figure 始终托管一个骨架(来自 vue-skeletor 包的组件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM