Home Web Front-end JS Tutorial How to dynamically modify the hover color of HTML elements by clicking the button?

How to dynamically modify the hover color of HTML elements by clicking the button?

Apr 04, 2025 pm 03:42 PM
css Browser click event html element red

How to dynamically modify the hover color of HTML elements by clicking the button?

Using CSS variables and JavaScript to dynamically modify element hover color

This article introduces how to dynamically change the hover color of HTML elements by clicking a button to improve the web interactive experience. We will use CSS variables and JavaScript to implement this functionality.

The goal is: click the button to modify the existing element hover style. To do this, we will use CSS variables to define the hover color and modify the value of the CSS variable in the button click event through JavaScript.

Implementation plan:

First, the HTML structure contains the target element ( .element ) and multiple buttons ( .change-color ), each button corresponding to a hover color:

<div class="container">
  <div class="element"></div>
  <button class="change-color">red</button>
  <button class="change-color">green</button>
  <button class="change-color">blue</button>
</div>
Copy after login

CSS style defines the basic style of the element and uses the CSS variable --color to control the hover color, default red:

 .element {
  width: 100px;
  height: 100px; /* aspect-ratio is not compatible with all browsers, changed to fixed height*/
  margin: 20px;
  background-color: lightgray; /* Add background color to make it easier to observe the effect*/
}

.element:hover {
  background: var(--color, red);
}
Copy after login

Finally, the JavaScript code listens for each button click event. After clicking the button, get the button text content (color name) and assign the color value to the CSS variable --color using el.style.setProperty() method:

 const el = document.querySelector(".element");

document.querySelectorAll(".change-color").forEach(e => {
  e.addEventListener("click", () => {
    el.style.setProperty("--color", e.textContent);
  });
});
Copy after login

Through the collaborative work of HTML, CSS and JavaScript, you can realize the function of dynamically changing the hover color of HTML elements by clicking a button. This method is simple, efficient and easy to maintain.

The above is the detailed content of How to dynamically modify the hover color of HTML elements by clicking the button?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Which 2025 currency exchanges are more secure? Which 2025 currency exchanges are more secure? Apr 20, 2025 pm 06:09 PM

The top ten safe and reliable exchanges in the 2025 cryptocurrency circle include: 1. Binance, 2. OKX, 3. Gate.io (Sesame Open), 4. Coinbase, 5. Kraken, 6. Huobi Global, 7. Gemini, 8. Crypto.com, 9. Bitfinex, 10. KuCoin. These exchanges are rated as safe and reliable based on compliance, technical strength and user feedback.

HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

Why is the return value empty when using RedisTemplate for batch query? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Why can't JavaScript directly obtain hardware information on the user's computer? Why can't JavaScript directly obtain hardware information on the user's computer? Apr 19, 2025 pm 08:15 PM

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

See all articles