小程序如何改变元素的样式

小程序开发小程序开发 2023-08-31 18:56:48 762
摘要: 1、获取元素的样式使用wx.createSelectorQuery()来获取元素这里需要注意的是:获取元素样式的函数应该放在onReady:function(){}这个里面letthat=thisconstquery=wx.createSelectorQuery()query.select('.de...

1、获取元素的样式

使用wx.createSelectorQuery()来获取元素

这里需要注意的是:获取元素样式的函数应该放在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这个数组来修改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中文网,大量免费小程序开发教程,欢迎学习!

以上就是小程序如何改变元素的样式的详细内容,更多请关注其它相关文章!