1、獲取元素的樣式
使用wx.createSelectorQuery()來獲取元素
這里需要注意的是:獲取元素樣式的函數(shù)應(yīng)該放在onReady:function(){}這個里面
let that = this const query = wx.createSelectorQuery() query.select('.detail-card').boundingClientRect() query.select('.detail-header').boundingClientRect() query.exec((res => { that.setData({ cardHeight: res[0].height, headerHeight: res[1].height }) }))
2、修改元素的樣式
這里思路是WXML中元素的樣式利用style給一個變量,通過修改topList這個數(shù)組來修改top屬性
<view class="detail-card" style="top:{{topList[index]+'px'}};z-index:{{index}}" id="{{'card'+index}}" wx:for="{{people}}" wx:key="{{index}}" catchtap="cardClick" > </view>
PHP中文網(wǎng),大量免費小程序開發(fā)教程,歡迎學(xué)習(xí)!