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

2020-09-14 15:38 更新

my.createIntersectionObserver

簡介

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

使用限制

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

示例代碼

  1. <!-- .axml -->
  2. <view class="logo" style='width: 200px;height: 200px;background-color:blue'>11</view>
  1. // .js
  2. Page({
  3. onReady() {
  4. my.createIntersectionObserver().relativeToViewport({top: 100, bottom: 100}).observe('.logo', (res) => {
  5. console.log(res, 'intersectionObserver');
  6. console.log(res.intersectionRatio); // 相交區(qū)域占目標(biāo)節(jié)點(diǎn)的布局區(qū)域的比例
  7. console.log(res.intersectionRect); // 相交區(qū)域
  8. console.log(res.relativeRect); // 參照區(qū)域的邊界
  9. console.log(res.boundingClientRect); // 目標(biāo)邊界
  10. console.log(res.time); // 時(shí)間戳
  11. console.log(res.id);
  12. });
  13. }
  14. })

入?yún)?/h4>

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

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

返回值

返回值為 IntersectionObserver。

IntersectionObserver 概覽

更新時(shí)間: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 使用選擇器指定一個(gè)節(jié)點(diǎn),作為參照區(qū)域之一。
IntersectionObserver.relativeToViewport 指定頁面顯示區(qū)域作為參照區(qū)域之一。

my.createSelectorQuery

簡介

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

使用限制

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

掃碼體驗(yàn)

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

示例代碼

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

  1. [
  2. null,
  3. {
  4. "x": 1,
  5. "y": 2,
  6. "width": 1367,
  7. "height": 18,
  8. "top": 2,
  9. "right": 1368,
  10. "bottom": 20,
  11. "left": 1
  12. },
  13. [
  14. {
  15. "x": 1,
  16. "y": -34,
  17. "width": 1367,
  18. "height": 18,
  19. "top": -34,
  20. "right": 1368,
  21. "bottom": -16,
  22. "left": 1
  23. },
  24. {
  25. "x": 1,
  26. "y": -16,
  27. "width": 1367,
  28. "height": 18,
  29. "top": -16,
  30. "right": 1368,
  31. "bottom": 2,
  32. "left": 1
  33. }
  34. ],
  35. {
  36. "scrollTop": 0,
  37. "scrollLeft": 0
  38. },
  39. {
  40. "width": 1384,
  41. "height": 360
  42. },
  43. {
  44. "scrollTop": 35,
  45. "scrollLeft": 0
  46. }
  47. ]

返回值

返回值為 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)前第一個(gè)匹配選擇器的節(jié)點(diǎn)。
SelectorQuery.selectAll 選擇所有匹配選擇器的節(jié)點(diǎn)。
SelectorQuery.selectViewport 選擇窗口對象。
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號