点击R G B改变view颜色
<!--components/test/test.wxml--> <view style="background-color:rgb({{fullColor}});" class="colorBox">颜色值:{{fullColor}}</view> <button size="mini" type="default" bindtap="changeR">R</button> <button size="mini" type="primary" bindtap="changeG">G</button> <button size="mini" type="warn" bindtap="changeB">B</button> <!-- <view>{{rgb.r}},{{rgb.g}},{{rgb.b}}</view> -->
// components/test/test.js Component({ properties: { }, data: { rgb:{ r:0, g:0, b:0 }, fullColor:'0, 0, 0' }, observers:{ 'rgb.r,rgb.g,rgb.b':function(r,g,b){ this.setData({ fullColor:`${r}, ${g}, ${b}` }) } // 'rgb.**':function(obj){ // this.setData({ // fullColor:`${obj.r}, ${obj.g}, ${obj.b}` // }) // } }, methods: { changeR() { this.setData({ 'rgb.r': this.data.rgb.r + 5 > 255 ? 255 : this.data.rgb.r + 5 }) }, changeG() { this.setData({ 'rgb.g': this.data.rgb.g + 5 > 255 ? 255 : this.data.rgb.g + 5 }) }, changeB() { this.setData({ 'rgb.b': this.data.rgb.b + 5 > 255 ? 255 : this.data.rgb.b + 5 }) } } })
/* components/test/test.wxss */ .colorBox{ line-height: 200rpx; font-size: 24rpx; color: white; text-shadow: 0rpx 0rpx 2rpx black; text-align: center; }
app.json中全局引用
"usingComponents": { "my-test":"/components/test/test" }
组件使用:
<!--pages/home/home.wxml--> <my-test></my-test>