NodesRef.fields

2020-08-22 11:17 更新

解釋: 獲取節(jié)點(diǎn)的相關(guān)信息,需要獲取的字段在 fields 中指定。返回值是 nodesRef 對(duì)應(yīng)的 selectorQuery ??芍付ǐ@取的字段包括:

方法參數(shù)

Object fields, Function callback

fields 參數(shù)說明

參數(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)前值。

示例 

在開發(fā)者工具中打開


圖片示例



代碼示例

<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;
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)