在鼠标悬停时反转文本颜色

dan*_*szz 14 html javascript css css3

我想用自定义的黑色光标悬停它时反转黑色文本.这个GIF证明了这个效果:

无法用头脑来解决这个问题,使用CSS和JS.我想有些混合混合模式,剪切蒙版,伪元素和滤镜.

以下代码使光标变为白色,但不会将黑色文本变为白色.听起来很抽象?这是一个演示.

mix-blend-mode: difference;
filter: invert(1) grayscale(1) contrast(2);
Run Code Online (Sandbox Code Playgroud)

我已经在Codepen上安装了一个游乐场,但是还没有找到解决方案.

如何使用CSS和Javascript重新创建这种悬停效果?

Tem*_*fif 20

这是一个使用的想法clip-path.诀窍是复制文本以使用不同的文本颜色在彼此之上具有两个层然后使用clip-path我随鼠标移动调整的那个来显示顶部文本.

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');
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
<h1 data-text="WORK">WORK</h1>

<span class="cursor"></span>
Run Code Online (Sandbox Code Playgroud)

这是使用radial-gradient和不重复文本的另一个想法:

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');
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
<h1>WORK</h1>

<span class="cursor"></span>
Run Code Online (Sandbox Code Playgroud)


这是第三种方法,它依赖于比clip-path和/或更多支持的技术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';
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
<h1 class="title">WORK</h1>

<div class="cursor">
  <h1>WORK</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

我使上面的例子变得简单,但是我们可以添加更多的JS来复制文本并放入光标并使其更灵活.


foc*_*yle 5

制造使用的解决方案mix-blend-mode: difference;.cursor

优点是 - 我们不需要向我们用圆形鼠标悬停的所有元素添加任何类或 javascript。

缺点是 - 这种解决方案不太稳定,因为mix-blend-mode它有点原始技术。它需要heightbodyand 中设置html,并且background-color对于 来说是严格的body

与部分.cursorCSS和JavaScript -我借了部分形式@Temani阿菲夫的解决方案。谢谢你,希望你不要介意,因为试图把它写得更好是没有意义的。但是我在滚动时添加了+window.scrollX+window.scrollY正确的.cursor工作。

更多关于mix-blend-mode你可以在这里阅读https://caniuse.com/#search=mix-blend-modehttps://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode

document.body.onmousemove = function(e) {
  document.documentElement.style.setProperty('--x', (e.clientX+window.scrollX) + 'px');
  document.documentElement.style.setProperty('--y', (e.clientY+window.scrollY) + 'px');
}
Run Code Online (Sandbox Code Playgroud)
html {
  height: 100%; /* requires for stable body height */
}

body {
  min-height: 100%; /* requires for 'mix-blend-mode' */
  cursor: none;
  color: #000;
  background-color: #fff; /* requires for 'mix-blend-mode' */
}

.cursor {
  position: absolute;
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  top: var(--y, 0);
  left: var(--x, 0);
  transform: translate(-50%, -50%);
  z-index: 2;
  mix-blend-mode: difference;
}
Run Code Online (Sandbox Code Playgroud)
<h1>WORK</h1>
<span class="cursor"></span>
Run Code Online (Sandbox Code Playgroud)