W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
基礎(chǔ)庫(kù) 1.7.0 開始支持本方法,低版本需做兼容處理。
返回一個(gè) selectorQuery 對(duì)象實(shí)例。
SelectorQuery
const query = tt.createSelectorQuery();
query.select("#the-id").boundingClientRect();
query.selectViewport().scrollOffset();
query.exec(function(res) {
res[0].top; // #the-id節(jié)點(diǎn)的上邊界坐標(biāo)
res[1].scrollTop; // 顯示區(qū)域的豎直滾動(dòng)位置
});
SelectorQuery 對(duì)象的方法列表
方法名 | 輸入 | 數(shù)據(jù)類型 | 返回值 | |
---|---|---|---|---|
in | component 對(duì)象 | 自定義對(duì)象實(shí)例 | SelectorQuery | |
select | selector | String | NodesRef | |
selectAll | selector | String | NodesRef | |
selectViewport | NodesRef | |||
exec | callback | Function |
將選擇器的選取范圍更改為自定義組件 component 內(nèi)(初始時(shí),選擇器僅選取頁(yè)面范圍的節(jié)點(diǎn),不會(huì)選取任何自定義組件中的節(jié)點(diǎn))。
Component({
queryMultipleNodes() {
const query = tt.createSelectorQuery().in(this);
query
.select("#the-id")
.boundingClientRect(function(res) {
res.top; // 這個(gè)組件內(nèi) #the-id 節(jié)點(diǎn)的上邊界坐標(biāo)
})
.exec();
}
});
在當(dāng)前頁(yè)面下選擇第一個(gè)匹配選擇器 selector 的節(jié)點(diǎn),返回一個(gè) NodesRef 對(duì)象實(shí)例,可以用于獲取節(jié)點(diǎn)信息。
在當(dāng)前頁(yè)面下選擇匹配選擇器 selector 的所有節(jié)點(diǎn),返回一個(gè) NodesRef 對(duì)象實(shí)例,可以用于獲取節(jié)點(diǎn)信息。
選擇顯示區(qū)域??捎糜讷@取顯示區(qū)域的尺寸、滾動(dòng)位置等信息。
執(zhí)行所有的請(qǐng)求。請(qǐng)求結(jié)果按請(qǐng)求次序構(gòu)成數(shù)組,在 callback 的第一個(gè)參數(shù)中返回。
屬性 | 數(shù)據(jù)類型 | 描述 |
---|---|---|
id | String | 節(jié)點(diǎn)的 ID |
dataset | Object | 節(jié)點(diǎn)的 dataset |
left | Number | 節(jié)點(diǎn)的左邊界坐標(biāo) |
right | Number | 節(jié)點(diǎn)的右邊界坐標(biāo) |
top | Number | 節(jié)點(diǎn)的上邊界坐標(biāo) |
bottom | Number | 節(jié)點(diǎn)的下邊界坐標(biāo) |
width | Number | 節(jié)點(diǎn)的寬度 |
height | Number | 節(jié)點(diǎn)的高度 |
添加節(jié)點(diǎn)的布局位置的查詢請(qǐng)求。相對(duì)于顯示區(qū)域,以像素為單位。其功能類似于 DOM 的 getBoundingClientRect。返回 nodesRef 對(duì)應(yīng)的 SelectorQuery。如果提供了 callback 回調(diào)函數(shù),在執(zhí)行 selectQuery.exec 方法后,節(jié)點(diǎn)信息會(huì)在 callback 中返回。
Page({
getRect() {
tt.createSelectorQuery()
.select("#the-id")
.boundingClientRect(function(rect) {
rect.id; // 節(jié)點(diǎn)的ID
rect.dataset; // 節(jié)點(diǎn)的dataset
rect.left; // 節(jié)點(diǎn)的左邊界坐標(biāo)
rect.right; // 節(jié)點(diǎn)的右邊界坐標(biāo)
rect.top; // 節(jié)點(diǎn)的上邊界坐標(biāo)
rect.bottom; // 節(jié)點(diǎn)的下邊界坐標(biāo)
rect.width; // 節(jié)點(diǎn)的寬度
rect.height; // 節(jié)點(diǎn)的高度
})
.exec();
},
getAllRects() {
tt.createSelectorQuery()
.selectAll(".class-name")
.boundingClientRect(function(rects) {
rects.forEach(function(rect) {
rect.id; // 節(jié)點(diǎn)的ID
rect.dataset; // 節(jié)點(diǎn)的dataset
rect.left; // 節(jié)點(diǎn)的左邊界坐標(biāo)
rect.right; // 節(jié)點(diǎn)的右邊界坐標(biāo)
rect.top; // 節(jié)點(diǎn)的上邊界坐標(biāo)
rect.bottom; // 節(jié)點(diǎn)的下邊界坐標(biāo)
rect.width; // 節(jié)點(diǎn)的寬度
rect.height; // 節(jié)點(diǎn)的高度
});
})
.exec();
}
});
添加節(jié)點(diǎn)的滾動(dòng)位置查詢請(qǐng)求。以像素為單位。節(jié)點(diǎn)必須是 scroll-view 或者 viewport,返回 nodesRef 對(duì)應(yīng)的 SelectorQuery。
返回參數(shù):
屬性 | 數(shù)據(jù)類型 | 描述 |
---|---|---|
id | String | 節(jié)點(diǎn)的 ID |
dataset | Object | 節(jié)點(diǎn)的 dataset |
scrollLeft | Number | 節(jié)點(diǎn)的水平滾動(dòng)位置 |
scrollTop | Number | 節(jié)點(diǎn)的豎直滾動(dòng)位置 |
Page({
getScrollOffset: function() {
tt.createSelectorQuery()
.selectViewport()
.scrollOffset(function(res) {
res.id; // 節(jié)點(diǎn)的ID
res.dataset; // 節(jié)點(diǎn)的dataset
res.scrollLeft; // 節(jié)點(diǎn)的水平滾動(dòng)位置
res.scrollTop; // 節(jié)點(diǎn)的豎直滾動(dòng)位置
})
.exec();
}
});
獲取節(jié)點(diǎn)的相關(guān)信息。需要獲取的字段在 fields 中指定。返回值是 nodesRef 對(duì)應(yīng)的 selectorQuery。
返回參數(shù):
屬性 | 數(shù)據(jù)類型 | 默認(rèn)值 | 屬性 | 描述 |
---|---|---|---|---|
id | Boolean | false | optional | 是否返回節(jié)點(diǎn) id |
dataset | Boolean | false | optional | 是否返回節(jié)點(diǎn) dataset |
rect | Boolean | false | optional | 是否返回節(jié)點(diǎn)布局位置(left right top bottom) |
size | Boolean | false | optional | 是否返回節(jié)點(diǎn)尺寸(width height) |
Page({
getFields: function() {
tt.createSelectorQuery()
.select("#the-id")
.fields(
{
id: true,
dataset: true,
size: true
},
function(res) {
res.id; //節(jié)點(diǎn)的ID
res.dataset; // 節(jié)點(diǎn)的dataset
res.width; // 節(jié)點(diǎn)的寬度
res.height; // 節(jié)點(diǎn)的高度
}
)
.exec();
}
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: