Skip to content

onIntersectionObserve

onIntersectionObserve(options, cb): void

监听元素

参数

NameTypeDefault valueDescription
optionsObjectundefined
options.marginsanyundefined距离参照物边距
options.observeranynull自定义 IntersectionObserver 实例
options.relativeToSelectoranyundefined指定参照节点,不传默认参照页面
options.selectorstringundefined元素选择器
cb[intersectionRatio: number, res: any]undefinedobserve 回调函数

返回值

void

示例

ts
onIntersectionObserve(
  {
    selector: '.card',
    margins: {bottom: 100},
  },
  intersectionRatio => {
    visible.value = intersectionRatio === 0
  }
)

// 更多参数
const observer = createIntersectionObserver(getCurrentInstance(), {
  thresholds: [0, 1],
  initialRatio: 1,
})

onIntersectionObserve(
  {
    selector: '.card',
    relativeToSelector: '.container',
    margins: {bottom: 100},
    observer: observer,
  },
  intersectionRatio => {
    visible.value = intersectionRatio === 0
  }
)

源码

page-instance-event.js