


Invert Text Color on Mouse Hover
The goal is to invert the color of a text element while hovering over it with a black cursor. The effect should be similar to the following GIF:
[GIF demonstrating the effect]
To achieve this effect with CSS and JavaScript, we'll use a combination of techniques:
- Create a duplicate text layer: Duplicate the text element and position it directly on top of the original. Invert the color of the duplicate text layer to white.
- Use clip-path to reveal the top layer: Use a clip-path to define a circular area that will reveal the inverted text layer.
- Listen for mouse movements: When the user moves the cursor over the text, adjust the clip-path's position to match the cursor's movement.
Here's an example implementation:
<code class="javascript">var h = document.querySelector('h1'); var p = h.getBoundingClientRect(); var c = document.querySelector('.cursor'); document.body.onmousemove = function(e) { // Adjust the cursor position c.style.left = e.clientX + 'px'; c.style.top = e.clientY + 'px'; // Adjust the clip-path h.style.setProperty('--x', (e.clientX - p.top) + 'px'); h.style.setProperty('--y', (e.clientY - p.left) + 'px'); };</code>
<code class="css">body { cursor: none; } h1 { color: #000; display: inline-block; margin: 50px; text-align: center; position: relative; } h1:before { position: absolute; content: attr(data-text); color: #fff; background: #000; clip-path: circle(20px at var(--x, -100%) var(--y, -100%)); } .cursor { position: fixed; width: 40px; height: 40px; background: #000; border-radius: 50%; top: 0; left: 0; transform: translate(-50%, -50%); z-index: -2; }</code>
<code class="html"><h1 id="WORK">WORK</h1> <span class="cursor"></span></code>
By combining these techniques, we can create the desired text color inversion effect on mouse hover.
The above is the detailed content of How to create an inverted text color effect on mouse hover with CSS and JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!
