IntersectionObserver.relativeToViewport

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

方法参数

Object object

margins参数说明: 用来扩展(或收缩)参照节点布局区域的边界

属性名类型必填默认值说明
leftnumber节点布局区域的左边界
rightnumber节点布局区域的右边界
topnumber节点布局区域的上边界
bottomnumber节点布局区域的下边界

示例

在开发者工具中预览效果

扫码体验

IntersectionObserver.relativeToViewport - 图1请使用百度APP扫码

图片示例

IntersectionObserver.relativeToViewport - 图2

IntersectionObserver.relativeToViewport - 图3

IntersectionObserver.relativeToViewport - 图4

代码示例

  1. <scroll-view class="scroll-view" scroll-y>
  2. <view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}">
  3. <text class="notice">向下滚动让小球出现</text>
  4. <!-- 占位元素 -->
  5. <view class="filling"></view>
  6. <!-- 小球 -->
  7. <view class="ball"></view>
  8. </view>
  9. </scroll-view>
Page({
    data: { },
    onReady() {
        const intersectionObserver = swan.createIntersectionObserver(this);
        intersectionObserver
        .relativeToViewport({bottom: 100})
        console.log(intersectionObserver) // {selector: null, margins: {bottom: 100}}
        swan.showModal({
            title: 'relativeToViewport',
            content: JSON.stringify(intersectionObserver._relativeInfo)
        });
    },
    onUnload() {
        this.intersectionObserver && this.intersectionObserver.disconnect();
    }
});