支付寶小程序API 節(jié)點(diǎn)查詢

2020-09-14 15:38 更新

my.createIntersectionObserver

簡介

my.createIntersectionObserver 用于創(chuàng)建并返回一個 IntersectionObserver 對象實(shí)例。需在 page.onReady 之后執(zhí)行 my.createIntersectionObserver()

使用限制

基礎(chǔ)庫 1.11.0 或更高版本;支付寶客戶端 10.1.32 或更高版本,若版本較低,建議采取 兼容處理

示例代碼

<!-- .axml -->
<view class="logo" style='width: 200px;height: 200px;background-color:blue'>11</view>
// .js


Page({
  onReady() {
    my.createIntersectionObserver().relativeToViewport({top: 100, bottom: 100}).observe('.logo', (res) => {
      console.log(res, 'intersectionObserver');
      console.log(res.intersectionRatio); // 相交區(qū)域占目標(biāo)節(jié)點(diǎn)的布局區(qū)域的比例
      console.log(res.intersectionRect);  // 相交區(qū)域
      console.log(res.relativeRect);      // 參照區(qū)域的邊界
      console.log(res.boundingClientRect); // 目標(biāo)邊界
      console.log(res.time); // 時間戳
      console.log(res.id); 
    });
  }
})

入?yún)?/h4>

入?yún)?Object 類型,屬性如下:

屬性 類型 描述
thresholds Array< Number> 一個數(shù)值數(shù)組,包含所有閾值。默認(rèn)值為 [0]。
initialRatio Number 初始的相交比例,如果調(diào)用時檢測到的相交比例與這個值不相等且達(dá)到閾值,則會觸發(fā)一次監(jiān)聽器的回調(diào)函數(shù)。默認(rèn)值為 0。
selectAll Boolean 是否同時觀測多個目標(biāo)節(jié)點(diǎn)(而非一個),如果設(shè)為 true ,observe 的 targetSelector 將選中多個節(jié)點(diǎn)(注意:同時選中過多節(jié)點(diǎn)將影響渲染性能)。默認(rèn)值為 false。

返回值

返回值為 IntersectionObserver。

IntersectionObserver 概覽

更新時間:2019-12-18 11:45:26

IntersectionObserver 對象,用于推斷某些節(jié)點(diǎn)是否可以被用戶看見、有多大比例可以被用戶看見。

方法

名稱 描述
IntersectionObserver.disconnect 停止監(jiān)聽。
IntersectionObserver.observe 指定目標(biāo)節(jié)點(diǎn),并開始監(jiān)聽相交狀態(tài)變化情況。
IntersectionObserver.relativeTo 使用選擇器指定一個節(jié)點(diǎn),作為參照區(qū)域之一。
IntersectionObserver.relativeToViewport 指定頁面顯示區(qū)域作為參照區(qū)域之一。

my.createSelectorQuery

簡介

my.createSelectorQuery 是用于返回一個 SelectorQuery 對象實(shí)例的 API。

使用限制

基礎(chǔ)庫 1.4.0 或更高版本;支付寶客戶端 10.1.8 或更高版本,若版本較低,建議采取 兼容處理。

掃碼體驗

節(jié)點(diǎn)查詢.jpeg

示例代碼

<!-- API-DEMO page/API/create-selector-query/create-selector-query.axml-->
<view class="page">
  <view class="page-description">節(jié)點(diǎn)查詢 API</view>
  <view class="page-section">
    <view className="all">節(jié)點(diǎn) all1</view>
    <view className="all">節(jié)點(diǎn) all2</view>
    <view id="one">節(jié)點(diǎn) one</view>
    <view id="scroll" style="height:200px;overflow: auto">
      <view style="height:400px">獨(dú)立滾動區(qū)域</view>
    </view>
    <button type="primary" onTap="createSelectorQuery">節(jié)點(diǎn)查詢</button>
  </view>
</view>
// API-DEMO page/API/create-selector-query/create-selector-query.js
Page({
  createSelectorQuery() {
    my.createSelectorQuery()
      .select('#non-exists').boundingClientRect()
      .select('#one').boundingClientRect()
      .selectAll('.all').boundingClientRect()
      .select('#scroll').scrollOffset()
      .selectViewport().boundingClientRect()
      .selectViewport().scrollOffset().exec((ret) => {
      console.log(ret);
      my.alert({
        content: JSON.stringify(ret, null, 2),
      });
    })
  },
});
ret 結(jié)構(gòu)

[
  null,
  {
    "x": 1,
    "y": 2,
    "width": 1367,
    "height": 18,
    "top": 2,
    "right": 1368,
    "bottom": 20,
    "left": 1
  },
  [
    {
      "x": 1,
      "y": -34,
      "width": 1367,
      "height": 18,
      "top": -34,
      "right": 1368,
      "bottom": -16,
      "left": 1
    },
    {
      "x": 1,
      "y": -16,
      "width": 1367,
      "height": 18,
      "top": -16,
      "right": 1368,
      "bottom": 2,
      "left": 1
    }
  ],
  {
    "scrollTop": 0,
    "scrollLeft": 0
  },
  {
    "width": 1384,
    "height": 360
  },
  {
    "scrollTop": 35,
    "scrollLeft": 0
  }
]

返回值

返回值為 SelectorQuery。

SelectorQuery 概覽

節(jié)點(diǎn)查詢對象類。

方法

名稱 描述
SelectorQuery.boundingClientRect 將當(dāng)前選擇節(jié)點(diǎn)的位置信息放入查詢結(jié)果。
SelectorQuery.exec 將查詢結(jié)果放入 Callback 回調(diào)中。
SelectorQuery.scrollOffset 將當(dāng)前選擇節(jié)點(diǎn)的滾動信息放入查詢結(jié)果。
SelectorQuery.select 選擇當(dāng)前第一個匹配選擇器的節(jié)點(diǎn)。
SelectorQuery.selectAll 選擇所有匹配選擇器的節(jié)點(diǎn)。
SelectorQuery.selectViewport 選擇窗口對象。
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號