随机切换背景颜色

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42460570/article/details/83416541

思路很简单用 background: rgba({{r}}, {{g}}, {{b}}, {{a}}) 给这四个变量0~255之间的随机整数即可

 html

<view style="height:200px;width: 200px;  background: rgba({{r}}, {{g}}, {{b}}, {{a}})">

</view>
<view class="btn-area">
    <button size="mini" bindtap="tap">changeColour</button>
</view>

js

Page({
  data: {
    r: 150,
    g: 150,
    b: 150,
    a: 150
  },
  tap: function (e) {
    this.setData({
      r: Math.floor(Math.random() * 255),
      g: Math.floor(Math.random() * 255),
      b: Math.floor(Math.random() * 255),
      a: Math.floor(Math.random() * 255),
      
    });
  },
 
})

猜你喜欢

转载自blog.csdn.net/weixin_42460570/article/details/83416541
今日推荐