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

2020-08-17 09:11:09

1、获取元素的样式

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

这里需要注意的是:获取元素样式的函数应该放在onReady:function(){}这个里面

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