在鼠标悬停时反转文本颜色 [英] Invert text color on mouse hover
问题描述
我希望在使用自定义-black-光标悬停时将蓝色文本反转。
此GIF演示效果:
I'd like to invert a black text while hovering it with a custom -black- cursor. This GIF demonstrates the effect:
无法解决这个问题,使用CSS& JS。我想有些混合混合模式,剪切蒙版,伪元素和滤镜。
Couldn't wrap my head around to make this work with CSS & JS. Some combination with mix-blend-modes, clipping masks, pseudo elements and filters I guess.
以下代码使光标变为白色但不会使黑色文本变为白色。听起来很抽象?这是一个演示。
The following code makes the cursor white but doesn't alow the black text to be turned white. Sounds abstract? Here's a demo.
mix-blend-mode: difference;
filter: invert(1) grayscale(1) contrast(2);
我已经设置了 Codepen上的游乐场乱七八糟,但还没有找到解决方案。
I've setup a playground on Codepen to mess around with, but didn't found a solution yet.
这个悬停效果怎么样?用CSS和Javascript重新创建?
How could this hover effect be recreated with CSS and Javascript?
推荐答案
这是一个使用 clip-path $ c的想法$ C>。诀窍是复制文本以使用不同的文本颜色在彼此之上具有两个层然后使用
剪辑路径
显示最上面的一个,我通过移动来调整鼠标。
Here is an idea using clip-path
. The trick is to duplicate the text to have two layers above each other with different text color then I reveal the top one using the clip-path
that I adjust with the move of the mouse.
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-20+'px';
c.style.top=e.clientY-20+'px';
/*Adjust the clip-path*/
h.style.setProperty('--x',(e.clientX-p.top)+'px');
h.style.setProperty('--y',(e.clientY-p.left)+'px');
}
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,-40px) var(--y,-40px));
}
.cursor {
position:absolute;
width:40px;
height:40px;
background:#000;
border-radius:50%;
top:-40px;
left:-40px;
z-index:-2;
}
<h1 data-text="WORK">WORK</h1>
<span class="cursor"></span>
这是另一个想法,使用径向渐变
而不重复文本:
Here is Another idea using radial-gradient
and without duplicating the text:
var h =document.querySelector('h1');
var p= h.getBoundingClientRect();
var c= document.querySelector('.cursor');
document.body.onmousemove = function(e) {
/*Adjust the position of the cursor*/
c.style.left=e.clientX-20+'px';
c.style.top=e.clientY-20+'px';
/*Adjust the radial-gradient*/
h.style.setProperty('--x',(e.clientX-p.top)+'px');
h.style.setProperty('--y',(e.clientY-p.left)+'px');
}
body {
cursor:none;
}
h1 {
display:inline-block;
margin:50px;
background: radial-gradient(circle at var(--x,-40px) var(--y,-40px), #fff 20px,black 21px);
background-clip: text;
-webkit-background-clip: text;
color:transparent;
-webkit-text-fill-color: transparent;
}
.cursor {
position:absolute;
width:40px;
height:40px;
background:#000;
border-radius:50%;
top:-40px;
left:-40px;
z-index:-2;
}
<h1>WORK</h1>
<span class="cursor"></span>
这是第三种方法,它依赖于比 clip-path
和/或<更多支持的技术code> background-clip:text :
Here is a third method that rely on more supported techniques than clip-path
and/or background-clip:text
:
var h =document.querySelector('h1.title');
var p= h.getBoundingClientRect();
var c= document.querySelector('.cursor');
document.querySelector('.cursor h1').style.left=p.left+'px';
document.querySelector('.cursor h1').style.top=p.top+'px';
document.body.onmousemove = function(e) {
/*Adjust the position of the cursor*/
c.style.left=e.clientX-20+'px';
c.style.top=e.clientY-20+'px';
}
body {
cursor:none;
margin:0;
}
h1.title {
display:inline-block;
margin:50px;
}
.cursor {
position:absolute;
width:40px;
height:40px;
background:#000;
border-radius:50%;
top:-40px;
left:-40px;
z-index:2;
overflow:hidden;
}
.cursor > * {
position:fixed;
color:#fff;
margin:0;
}
<h1 class="title">WORK</h1>
<div class="cursor">
<h1>WORK</h1>
</div>
我使上面的例子变得简单但我们可以添加更多的JS来复制文本并放入光标内部并使其更灵活。
I made the above example simple but we can add more JS in order to duplicate the text and put inside the cursor and have something more flexible.
这篇关于在鼠标悬停时反转文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!