小程序开放平台

文档中心
基础
界面
网络
数据缓存
媒体
位置
开放接口
设备
XHSML
createIntersectionObserver
intersectionObserver
createSelectorQuery
SelectorQuery
nodeRef
性能
文件
数据分析
画布

xhs.intersectionObserver

开发
>
JS API
>
XHSML
>
intersectionObserver
>
更新时间:2024-11-13 19:05:31

IntersectionObserver

IntersectionObserver 对象,用于推断某些节点是否可以被用户看见、有多大比例可以被用户看见。

方法
说明
disconnect停止监听。回调函数将不再触发
observe指定目标节点并开始监听相交状态变化情况
relativeTo使用选择器指定一个节点,作为参照区域之一
relativeToViewport指定页面显示区域作为参照区域之一

示例代码

javascript
Page({
  onReady() {
    this.intersectionObserver = xhs.createIntersectionObserver(this);
    this.intersectionObserver
      .relativeTo('.scroll-view')
      .relativeToViewport({bottom: 100})
      .observe('.ball', res => {
          console.log('observe', res) 
      });
  },
  onUnload() {
    this.intersectionObserver && this.intersectionObserver.disconnect();
  }
});

IntersectionObserver.disconnect

与页面显示区域的相交区域并不准确代表用户可见的区域,因为参与计算的区域是“布局区域”,布局区域可能会在绘制时被其他节点裁剪隐藏(如遇祖先节点中 overflow 样式为 hidden 的节点)或遮盖(如遇 fixed 定位的节点)。

javascript
Page({
  data: {
      intersectionRect: ''
  },
  onReady() {
    this.intersectionObserver = xhs.createIntersectionObserver(this);
    this.intersectionObserver
        .relativeTo('.scroll-view')
        .observe('.ball', res => {
            this.setData('intersectionRect', res.intersectionRect);
            // 相交区域的左边界坐标
            console.log(res.intersectionRect.left); 
            // 相交区域的右边界坐标
            console.log(res.intersectionRect.right); 
            // 相交区域的上边界坐标
            console.log(res.intersectionRect.top); 
            // 相交区域的下边界坐标
            console.log(res.intersectionRect.bottom);
            // 相交区域的宽度 
            console.log(res.intersectionRect.width); 
            // 相交区域的高度
            console.log(res.intersectionRect.height); 
    });
  },
  disconnect() {
    this.intersectionObserver && this.intersectionObserver.disconnect();
  }
});

IntersectionObserver.observe

指定目标节点并开始监听相交状态变化情况。

  • 方法参数 String targetSelector, Function callback
  • targetSelector 参数说明 选择器
  • callback 参数说明 监听相交状态变化的回调函数。

callback 返回参数说明

属性
类型
说明
intersectionRatioNumber相交比例
intersectionRectObject相交区域的边界
boundingClientRectObject目标边界
relativeRectObject参照区域的边界
timeNumber相交检测时的时间戳

intersectionRect 参数说明

属性
类型
说明
leftNumber 左边界
rightNumber右边界
topNumber上边界
bottomNumber下边界
widthNumber相交区域的宽度
heightNumber相交区域的高度

boundingClientRect 参数说明

属性
类型
说明
leftNumber左边界
rightNumber右边界
topNumber上边界
bottomNumber下边界
widthNumber目标宽度
heightNumber目标高度

relativeRect 参数说明

属性
类型
说明
leftNumber左边界
rightNumber右边界
topNumber上边界
bottomNumber下边界

示例代码

javascript
Page({
  data: {
      intersectionRect: ''
  },
  onReady() {
    this.intersectionObserver = xhs.createIntersectionObserver(this);
    this.intersectionObserver
        .relativeTo('.scroll-view')
        .observe('.ball', res => {
            this.setData('intersectionRect', res.intersectionRect);
            // 相交区域的左边界坐标
            console.log(res.intersectionRect.left); 
            // 相交区域的右边界坐标
            console.log(res.intersectionRect.right); 
            // 相交区域的上边界坐标
            console.log(res.intersectionRect.top); 
            // 相交区域的下边界坐标
            console.log(res.intersectionRect.bottom);
            // 相交区域的宽度 
            console.log(res.intersectionRect.width); 
            // 相交区域的高度
            console.log(res.intersectionRect.height); 
    });
  },
  disconnect() {
    this.intersectionObserver && this.intersectionObserver.disconnect();
  }
});

IntersectionObserver.relativeTo

使用选择器指定一个节点,作为参照区域之一

  • 方法参数 String selector, Object margins
  • selector 参数说明 选择器

margins 参数说明

用来扩展(或收缩)参照节点布局区域的边界。

属性名
类型
必填
默认值
说明
leftNumber节点布局区域的左边界
rightNumber节点布局区域的右边界
topNumber节点布局区域的上边界
bottomNumber节点布局区域的下边界

示例代码

javascript
Page({
  onReady() {
    this.intersectionObserver = xhs.createIntersectionObserver(this);
    this.intersectionObserver
      .relativeTo('.scroll-view', 50);
      // {selector: ".scroll-view", margins: 50}
      console.log('实例:',  this.intersectionObserver); 
      xhs.showModal({
        title: 'relativeTo',
        content: JSON.stringify(this.intersectionObserver._relativeInfo)
      });
  },
  onUnload() {
    this.intersectionObserver && this.intersectionObserver.disconnect();
  }
});

IntersectionObserver.relativeToViewport

指定页面显示区域作为参照区域之一

  • 方法参数 Object margins

margins 参数说明

用来扩展(或收缩)参照节点布局区域的边界。

属性名
类型
必填
说明
leftNumber节点布局区域的左边界
rightNumber节点布局区域的右边界
topNumber节点布局区域的上边界
bottomNumber节点布局区域的下边界

示例代码

javascript
Page({
  onReady() {
    this.intersectionObserver = xhs.createIntersectionObserver(this);
    this.intersectionObserver
      .relativeTo('.scroll-view', 50);
      // {selector: ".scroll-view", margins: 50}
      console.log('实例:',  this.intersectionObserver); 
      xhs.showModal({
        title: 'relativeToViewport',
        content: JSON.stringify(this.intersectionObserver._relativeInfo)
      });
  },
  onUnload() {
    this.intersectionObserver && this.intersectionObserver.disconnect();
  }
});