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中文网,大量免费小程序开发教程,欢迎学习!

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

点赞(167)

评论列表共有 0 条评论

立即
投稿
返回
顶部