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

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.

How to scroll to the top or bottom of the page in Vue.js

Publish Date:2025/03/01 Views:96 Category:Vue

Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this

Display element or text on mouse hover in vue

Publish Date:2025/03/01 Views:179 Category:Vue

Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following

What is the use of the immediate property of watch in Vue?

Publish Date:2025/03/01 Views:67 Category:Vue

In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang

Setting up checkbox functionality in Vue

Publish Date:2025/03/01 Views:186 Category:Vue

In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.

What happens if a child component changes the data in props in Vue?

Publish Date:2025/03/01 Views:140 Category:Vue

In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t

How to refresh the page in Vue

Publish Date:2025/03/01 Views:91 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial