JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Click the button to switch the background color in vue

Author:JIYIK Last Updated:2025/02/26 Views:

Vue is a popular JavaScript framework for building interactive web applications. Vue provides many powerful features, one of which is the ability to easily switch the background color when a button is clicked. In this article, we will show you how to implement this feature in Vue using simple code.

Step 1: Create a Vue application

First, we need to create a Vue application. We can use the Vue CLI to create a new Vue application. Open your terminal and enter the following command:

vue create my-app

Where my-appis your application name. After entering this command, Vue CLI will prompt you to select some configuration options, such as Babel, ESLint, and Linter/Formatter. Select the configuration options you want and wait for the application to be created.

Step 2: Add buttons and styles

Before we start writing code, we need to add a button and some styles. In src/App.vuethe ./blog/openwrt file, add the following code:

<template>
  <div class="container">
    <button @click="changeColor">Change Color</button>
  </div>
</template>

<style>
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

button {
  padding: 12px 24px;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  background-color: #007bff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>

In the code above, we added a button and some styles. The text of the button is "Change Color", and we added a click event to the button, which will be triggered when the button is clicked.

Step 3: Add data and methods

Next, we need to add data and methods to the Vue instance. In src/App.vuethe file, add the following code:

<script>
export default {
  data() {
    return {
      backgroundColor: "#f5f5f5"
    };
  },
  methods: {
    changeColor() {
      if (this.backgroundColor === "#f5f5f5") {
        this.backgroundColor = "#007bff";
      } else {
        this.backgroundColor = "#f5f5f5";
      }
    }
  }
};
</script>

In the code above, we use datathe backgroundcolor function to define a backgroundColorvariable called backgroundcolor that stores the value of the current background color. We also define a changeColormethod called backgroundcolor that will be called when the button is clicked. This method will check the value of the current background color and change it to blue if the current color is gray and vice versa.

Step 4: Binding Styles

Now that we have defined the data and methods, we need to bind the background color to the container element's style. In src/App.vuethe ./blog/docs/docs/src/main/container/style/common/lib/container/content ...

<div class="container" :style="{ backgroundColor: backgroundColor }">

In the code above, we use :stylethe binding syntax to backgroundColorbind the variable to the style property of the container element background-color.

Step 5: Run the Application

Now that we have all the code completed, we can run the application locally with the following command:

npm run serve

This command will start the development server and open the application in your browser. Now, when you click the Change Color button, the background color of the application will switch between gray and blue.

Summarize

In this article, we introduced how to use simple code in Vue to implement the function of switching the background color when clicking a button. We used Vue's data binding and methods to implement this function, and used style binding to bind the background color to the style of the container element. You can use these techniques to implement other similar functions, such as switching font color or text content.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

My understanding of webservice is this

Publish Date:2025/03/18 Views:150 Category:NETWORK

Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat

Which technology do you choose to implement the web chat room?

Publish Date:2025/03/18 Views:62 Category:NETWORK

With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio

How to redirect a website from HTTP to HTTPS

Publish Date:2025/03/16 Views:117 Category:NETWORK

HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi

How to avoid soft 404 in the website during SEO

Publish Date:2025/03/17 Views:136 Category:NETWORK

This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand

Webpack packages ES6 and CommonJs mixed React

Publish Date:2025/03/02 Views:179 Category:React

This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial