Home > Web Front-end > JS Tutorial > How to modify the view label background color in WeChat mini program

How to modify the view label background color in WeChat mini program

亚连
Release: 2018-06-22 15:24:43
Original
10576 people have browsed it

This article mainly introduces the WeChat applet to implement the function of clicking the button to modify the background color of the view label, involving the WeChat applet event response and numerical calculation to realize the relevant operation skills of dynamically setting the view background color style. Friends in need can refer to the following

The example in this article describes the WeChat applet's function of changing the background color of the view label by clicking a button. Share it with everyone for your reference, the details are as follows:

Operation steps:

① Data binding view style background attribute value
② Set the background through a logical file Attribute initial value
③ Modify the background attribute value by clicking the button

Key code

index.wxml file:

<view style="background:{{viewBg}};color:white;height:100px;">我是view标签</view>
<button type="default" bindtap="changeBg">点击我修改view标签的背景颜色</button>
Copy after login

index.js file:

var num=0;
Page({
  data:{
    viewBg:&#39;green&#39;
  },
  changeBg(){
    num++;
    var result=num/2;
    if(num%2==0){      
      this.setData({
        viewBg:&#39;green&#39;
      })
    }else{
      this.setData({
        viewBg:&#39;blue&#39;
      })
    }
    console.log(num)
    console.log(result)
  }
})
Copy after login

In the code, the increment number of num is set, and then the remainder operation is performed to determine the odd and even number of num for each event response, and then the setting style="background: {{viewBg}};color:white;height:100px;"The effect of switching the background color value of viewBg between green and blue.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed introduction to updating objects in mongoose

How to implement AOP in JavaScript

How to deploy https using nginx node

There is a detailed introduction to updating objects in mongoose

In JS Detailed introduction to setTimeout in the function

The above is the detailed content of How to modify the view label background color in WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template