W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
解釋: 獲取節(jié)點(diǎn)的相關(guān)信息,需要獲取的字段在 fields 中指定。返回值是 nodesRef 對(duì)應(yīng)的 selectorQuery ??芍付ǐ@取的字段包括:
Object fields, Function callback
參數(shù)名 | 類型 | 必填 | 默認(rèn)值 | 說明 |
---|---|---|---|---|
id |
Boolean |
否 |
是否返回節(jié)點(diǎn) id |
|
dataset |
Boolean |
否 |
是否返回節(jié)點(diǎn) dataset |
|
rect |
Boolean |
否 |
是否返回節(jié)點(diǎn)布局位置(left right top bottom) |
|
size |
Boolean |
否 |
是否返回節(jié)點(diǎn)尺寸(width height) |
|
scrollOffset |
Boolean |
否 |
是否返回節(jié)點(diǎn)的 scrollLeft scrollTop ,節(jié)點(diǎn)必須是 scroll-view 或者 viewport 。 |
|
properties |
Array.<string> |
否 |
指定屬性名列表,返回節(jié)點(diǎn)對(duì)應(yīng)屬性名的當(dāng)前屬性值(只能獲得組件文檔中標(biāo)注的常規(guī)屬性值, id class style 和事件綁定的屬性值不可獲?。?/p> |
|
computedStyle |
Array.<string> |
否 |
指定樣式名列表,返回節(jié)點(diǎn)對(duì)應(yīng)樣式名的當(dāng)前值。 |
<view class="wrap">
<view class="message">
<text s-if="appear">小球出現(xiàn)</text>
<text s-else>小球消失</text>
</view>
<scroll-view class="scroll-view" scroll-y>
<view class="scroll-area" style="{{appear ? 'background: #ccc' : ''}}">
<text class="notice">向下滾動(dòng)讓小球出現(xiàn)</text>
<view class="filling"></view>
<view class="ball"></view>
</view>
</scroll-view>
<button bindtap="queryNodeInfo">點(diǎn)擊獲取scrollview組件的nodeRef信息</button>
<view class="list-area border-bottom">
<view class="list-item-key-4">得到的信息:</view>
<view class="list-item-value">{{NodesRefData}}</view>
</view>
</view>
Page({
data: {
NodesRefData: ''
},
queryNodeInfo() {
swan.createSelectorQuery()
.select('.scroll-view')
.fields({
id: false,
dataset: false,
rect: true,
size: true,
scrollOffset: true,
properties: ['scrollX', 'scrollY'],
computedStyle: ['margin', 'backgroundColor']
}, res => {
console.log('節(jié)點(diǎn)信息', res);
this.setData('NodesRefData', JSON.stringify(res));
// res.dataset; // 節(jié)點(diǎn)的dataset
// res.width; // 節(jié)點(diǎn)的寬度
// res.height; // 節(jié)點(diǎn)的高度
// res.scrollLeft; // 節(jié)點(diǎn)的水平滾動(dòng)位置
// res.scrollTop; // 節(jié)點(diǎn)的豎直滾動(dòng)位置
// res.scrollX; // 節(jié)點(diǎn) scroll-x 屬性的當(dāng)前值
// res.scrollY; // 節(jié)點(diǎn) scroll-y 屬性的當(dāng)前值
// 此處返回指定要返回的樣式名屬性
// res.margin;
// res.backgroundColor;
}).exec();
}
});
.scroll-view {
height: 400rpx;
background: #fff;
margin-top: 20px;
}
.scroll-area {
display: flex;
flex-direction: column;
height: 1300rpx;
transition: .5s;
width: 1200rpx;
align-items: center;
}
.notice {
margin-top: 150rpx;
}
.ball {
width: 200rpx;
height: 200rpx;
border-radius: 50%;
background: #38f;
}
.filling {
height: 400rpx;
}
.message {
display: flex;
width: 100%;
margin: 50rpx 0;
justify-content: center;
}
.message text {
font-family: -apple-system-font, Helvetica Neue, Helvetica, sans-serif;
font-size: 40rpx;
}
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)系方式:
更多建議: