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

Various ways to format dates in Vue

Publish Date:2025/02/26 Views:68 Category:Vue

Vue is a popular front-end framework that provides many features, including multiple ways to format dates. In this article, we will introduce several methods that can help you format dates in Vue. Using moment.js moment.js is a popular

The difference between strict mode and non-strict mode in Vue

Publish Date:2025/02/26 Views:169 Category:Vue

Vue.js is a very popular JavaScript framework. Its design goal is to help developers build efficient and flexible web applications. Vue.js provides two different modes: strict mode and non-strict mode. These two modes are used in the develop

Introducing the bootstrap framework into the vue project

Publish Date:2025/02/26 Views:148 Category:Vue

Bootstrap is a popular front-end framework that provides a wealth of CSS and JavaScript components to help developers quickly build modern responsive websites and applications. Introducing the Bootstrap framework in a Vue project can provide

Using iframe to nest pages in Vue

Publish Date:2025/02/26 Views:177 Category:Vue

With the development of the Internet, websites are becoming more and more complex, and various technologies are needed to implement them. Among them, iframe technology is a commonly used technology that can embed a web page into another web

Different ways to send POST requests in Vue

Publish Date:2025/02/26 Views:82 Category:Vue

In Vue, sending POST requests is a very common operation, which can be achieved in many ways. This article will introduce several methods of sending POST requests in Vue, and will explain them in detail with examples. Method 1: Use Vue-resou

Importing JSON files into Vue

Publish Date:2025/02/26 Views:100 Category:Vue

Vue.js is a popular JavaScript framework that can quickly build single-page applications (SPA). In Vue.js, we can use JSON files to store data, which is very useful in development. This article will introduce how to introduce JSON files in V

Creating a sticky footer in Vue

Publish Date:2025/02/26 Views:116 Category:Vue

In web design, the footer is one of the most important parts of the page. It usually contains copyright information, contact information, and other relevant information about the website. However, when users scroll the page, the footer may d

How to introduce style files in Vue

Publish Date:2025/02/26 Views:181 Category:Vue

Vue is a popular front-end framework that allows developers to build single-page applications in a componentized way. During the development process, we usually need to introduce style files to beautify the page. This article will introduce

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial