鼠标悬停时反转文本颜色

2023-12-09

我想在使用自定义“黑色”光标悬停黑色文本时反转黑色文本。 这个GIF演示了效果:

我无法用 CSS 和 JS 来实现这个工作。我猜是与混合混合模式、剪贴蒙版、伪元素和过滤器的一些组合。

以下代码使光标变为白色,但不允许黑色文本变成白色。听起来很抽象?这是一个demo.

// All creds go to https://murmure.me ????

const cursor = $(".cursor"),
      body = $("body"),
      slider = $(".slider"),
      nav = $(".nav"),
      fail = $(".fail"),
      invert = $(".invert"),
      nav_open_menu = $(".navopen"),
      wwidth = $(window).width(),
      wheight = $(window).height(),
             
cursorMove = function() {
  var e, n;
  return (
    body.addClass("cursor-on"),
    cursor.css({
      transform: "matrix(1, 0, 0, 1, " + wwidth / 2 + ", " + wheight / 2 + ")"
    }),
    (e = wheight / 2),
    (n = 0.65 * wwidth / 2),
    n > e ? e : n,
    $(window).on("mousemove", function(e) {
      var n, t;
      if (
        ((window.x = e.clientX),
        (window.y = e.clientY),
        cursor.css({
          transform: "matrix(1, 0, 0, 1, " + x + ", " + y + ")"
        }),
        !nav.hasClass("overlay-visible"))
      )
        return (
          (n = Math.floor((x - 60) / 5)),
          (t = Math.floor((y - 60) / 5)),
          n < 20 && t < 20
            ? nav_open_menu.addClass("magnetize").css({
                transform: "scale(1.3) translate3d(" + n + "px, " + t + "px, 0)"
              })
            : nav_open_menu.removeClass("magnetize").attr("style", "")
        );
    })
  );
};
cursorBind = function() {
    var e, n, t;
    if (
      ((n = cursor.find("span")).removeClass("link external new"),
      (e = $(".focus")),
      (t = $(".slack")),
      $(window).on({
        mouseenter: function() {
          return n.removeClass("off");
        },
        mouseleave: function() {
          return n.addClass("off");
        }
      }),
      $("a, button, .cursor-link, .sort-listing th").on({
        mouseenter: function() {
          var e;
          return (
            (e = $(this).hasClass("external") ? "link external" : "link"),
            n.addClass(e)
          );
        },
        mouseleave: function() {
          return n.removeClass("link external");
        }
      }),
      $("h1").on({
        mouseenter: function() {
          var e;
          return (
            (e = $(this).hasClass("external") ? "invert external" : "invert"),
            n.addClass(e)
          );
        },
        mouseleave: function() {
          return n.removeClass("invert external");
        }
      }),
      e.length &&
        e.find("a").on({
          mouseenter: function() {
            return n.addClass("new");
          },
          mouseleave: function() {
            return n.removeClass("new");
          }
        }),
      slider.length &&
        slider.on({
          mouseenter: function() {
            var e;
            return (
              (e = $(this).hasClass("full") ? "click" : "drag"), n.addClass(e)
            );
          },
          mouseleave: function() {
            return n.removeClass("drag click");
          }
        }),
      t.length &&
        t.on({
          mouseenter: function() {
            return n.addClass("light");
          },
          mouseleave: function() {
            return n.removeClass("light");
          }
        }),
      fail.length)
    )
      return fail.on({
        mouseover: function() {
          return n.addClass("relol");
        },
        mouseleave: function() {
          return n.removeClass("relol");
        }
      });
  };

cursorMove();
cursorBind();
body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

h1 {
  font-weight: 500;
  font-size: 9vw;
}
 

*, *:before, *:after {
	 box-sizing: border-box;
}
 body {
	 min-height: 100vh;
	 background-color: #fff;
	 color: #000;
	 text-rendering: optimizelegibility;
	 line-height: 1.5;
	/* System stack font-family mentioned by @wooorm */
	 font-family: Roboto, -apple-system, BlinkMacSystemFont, Helvetica Neue, Segoe UI, Oxygen, Ubuntu, Cantarell, Open Sans, sans-serif;
}
 .cursor-on {
	 cursor: none;
}
 .cursor-on * {
	 cursor: none;
}
 .cursor-on .cursor {
	 position: fixed;
	 z-index: 10;
	 pointer-events: none;
	 top: -1rem;
	 left: -1rem;
}
 .cursor-on .cursor > span {
	 display: block;
	 width: 2rem;
	 height: 2rem;
	 border-radius: 2rem;
	 background: #f8e71c;
	 transition: transform 0.2s cubic-bezier(0.165, 0.84, 0.44, 1);
	 transform: scale(1);
}
 .cursor-on .cursor > span.off {
	 transform: scale(0);
}
 .cursor-on .cursor > span.link {
	 transform: scale(1.5);
	 background-color: #0000ed;
}
 .cursor-on .cursor > span.link.external:after {
	 content: "↗";
	 display: block;
	 white-space: pre;
	 color: #fff;
	 font-weight: 100;
	 font-size: 1rem;
	 text-align: center;
	 width: 100%;
	 line-height: 1;
	 padding-top: 0.6em;
}
 .cursor-on .cursor > span.light {
	 background: #fff;
}
 .cursor-on .cursor > span.drag, .cursor-on .cursor > span.click, .cursor-on .cursor > span.relol, .cursor-on .cursor > span.new {
	 transform: scale(2.5);
}
 .cursor-on .cursor > span.drag:after, .cursor-on .cursor > span.click:after, .cursor-on .cursor > span.relol:after, .cursor-on .cursor > span.new:after {
	 display: block;
	 content: "";
	 white-space: pre;
	 color: #fff;
	 font-size: 5px;
	 text-align: center;
	 width: 100%;
	 line-height: 1;
	 padding-top: calc((2rem / 2) - 2.5px);
	 color: #000;
}
 .cursor-on .cursor > span.drag:after {
	 content: "play";
}
 .cursor-on .cursor > span.click:after {
	 content: "click\A click";
}
 .cursor-on .cursor > span.relol:after {
	 content: "click\A me";
	 padding-top: calc((2rem / 2) - 5px);
}
 .cursor-on .cursor > span.new:after {
	 content: "new\A new";
}
 .cursor-on:active .cursor > span {
	 transform: scale(0.75);
}
 .cursor-on:active .cursor > span.link {
	 transform: scale(1);
}
 .cursor-on:active .cursor > span.drag, .cursor-on:active .cursor > span.click, .cursor-on:active .cursor > span.relol, .cursor-on:active .cursor > span.new {
	 transform: scale(2);
}
 .grid {
	 background-color: #fff;
}
 .grid--column--item {
	 background-color: #000;
}
 .grid--column.sticky .grid--column--item {
	 border-color: #000;
}


.cursor-on .cursor > span {
	 background: black;
}
 .cursor-on .cursor > span.invert {
	 transform: scale(2.5);
	 mix-blend-mode: difference;
	 filter: invert(1) grayscale(1) contrast(2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="invert">Work</h1>


<div class="cursor" >
  <span class="off"></span>
 </div>
mix-blend-mode: difference;
filter: invert(1) grayscale(1) contrast(2);

我已经设置了一个Codepen 上的游乐场折腾了一下,但还没找到解决办法。

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


这是一个使用的想法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+'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');
}
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;
}
<h1 data-text="WORK">WORK</h1>

<span class="cursor"></span>

这是另一个使用的想法radial-gradient并且无需复制可以同时处理多个元素的文本:

document.body.onmousemove = function(e) {
  document.documentElement.style.setProperty('--x',(e.clientX)+'px');
  document.documentElement.style.setProperty('--y',(e.clientY)+'px');
}
body {
  cursor:none;
}

.mask {
  background: 
    radial-gradient(circle 20px 
                    at var(--x,0) var(--y,0), 
                    #fff 99%,black 100%) 
                    fixed;
  background-clip: text;
  -webkit-background-clip: text;
  color:transparent;
  -webkit-text-fill-color: transparent;
}
html::before {
  content:"";
  position:fixed;
  width:40px;
  height:40px;
  background:#000;
  border-radius:50%;
  top:var(--y,0);
  left:var(--x,0);
  transform:translate(-50%,-50%);
  z-index:-2;
}
<h1 class="mask">WORK</h1>
<p class="mask">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis risus sapien. Maecenas dui orci, blandit et commodo eget, egestas quis odio. Donec eu tortor turpis. Aliquam convallis et nisi ut varius. Proin sapien erat, auctor in efficitur vel, efficitur sit amet justo. In pretium iaculis tempus. Vivamus congue</p>

<p class="mask">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse quis risus sapien. Maecenas dui orci, blandit et commodo eget, egestas quis odio. Donec eu tortor turpis. Aliquam convallis et nisi ut varius. Proin sapien erat, auctor in efficitur vel, efficitur sit amet justo. In pretium iaculis tempus. Vivamus congue</p>

具有类似想法的相关问题:堆叠圆圈会在边界半径上产生黑色条

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

鼠标悬停时反转文本颜色 的相关文章

随机推荐

  • data.table 连接中 i 表达式的 .SD 和 .SDcols

    我正在尝试根据联接将列的子集从 Y 复制到 X 其中列的子集是动态的 我可以很容易地识别这些列 names Y grep xxx names Y 但是当我尝试在 j 表达式中使用该代码时 它只给出列名 而不是列的值 这 SD and SDc
  • 当程序首次运行时,如何写出配置的空白版本? [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 我一直在用 C 制作一个命令行工具 最终我们使用了一些参数的配置 这些参数只需要设置一次 我没有为这些提供 UI 而是被告知只需设置一个空白配置 如果未设置值 则提供一条消息来说
  • PHP 替换特殊字符,如 à->a、è->e

    我有php文档signup php 它将表单 在form php文档中 的内容保存到MySQL库 当我想重新格式化输入内容时 问题就出现了 我想要解码 UTF 8 字符 例如 gt a first name POST first name
  • Bluez bluetoothctl 扫描与 hcitool 扫描

    我在 Raspberry Pi Buster 和 Stretch 上运行 bluez 5 50 我有一个 ble 传感器设备 仅当按下传感器设备上的按钮时才会公布数据 因此 广告是异步的 并且中间没有定期广告 并且所有数据包都是唯一的 没有
  • 如何让 python 只读取包含一首诗的文件中的每隔一行

    我知道读取每一行的代码是 f open poem txt r for line in f print line 如何让 python 只读取原始文件中的偶数行 假设行的编号从 1 开始 方法有很多种 这里简单介绍一下 with open p
  • 如何卸载jupyter

    我一直在尝试卸载jupyter 我尝试过以下命令 pip uninstall jupyter pip3 uninstall jupyter and rm rf Users user Library Jupyter 即使在运行所有这些命令后
  • C : typedef 结构名称 {...}; VS typedef struct{...} 名称;

    正如标题所说 我有这样的代码 typedef struct Book int id char title 256 char summary 2048 int numberOfAuthors struct Author authors typ
  • 删除重复项,保留最后一个条目——优化

    我正在开发一个宏 它将遍历电子表格并根据两列 Q 列和 D 列 中分别提供的两个条件删除重复的条目 行 这是我所拥有的 我在一个小数据集上测试了它 它是slow Sub RemoveDupesKeepLast dim i As Intege
  • 在编译时设置属性而不知道目标类型

    我想在编译时不知道对象类型的情况下设置对象的属性值 我希望它很快 即不是每次都使用反射 我知道属性名称和类型 最快的方法 据我所知 是使用委托 这就是我到目前为止所拥有的 class User this is an example Assu
  • 并发和多线程有什么区别?

    并发和多线程有什么区别 并发只能在多核CPU上实现吗 有人能用例子解释一下吗 并发和多线程有什么区别 并发描述了进程运行的方式 它们要么是顺序的 一个接一个 要么是并发的 能够 同时 取得进展 尽管不一定在同一时刻 要么是并行的 它们同时发
  • git rebase——我的哈希值意外不匹配

    1 为什么我没有机会暂存我的提交片段 以便我可以在合并之前将它们分成不同的提交team 2 为什么rebase之前的hash不等于rebase之后的hash 我没有看到任何消息表明有一个被压扁的空白或其他东西 GIT work git lo
  • 多个电子邮件收件人的 XML 架构

    我需要一个示例 XSD 来支持新元素中的多个电子邮件收件人 我要求每个收件人的电子邮件地址位于不同的元素中 谁能帮我解释一下吗 Example
  • 如何检查这是目录路径还是任何文件名路径?

    by this 为什么 fopen any path name r 不给出 NULL 作为返回值 我知道在linux中目录和文件被认为是文件 所以当我在 fopen 中以读取模式给出任何目录路径或文件路径时 它不会给出 NULL 文件描述符
  • 如何合并具有相同标识符 R 的行?

    I have been searching a lot but I can t seem to find an answer for what I m looking for The rows were originally melted
  • 如何在ASP.NET中实现GZip压缩?

    我正在尝试为我的 asp net 页面 包括我的 CSS 和 JS 文件 实现 GZip 压缩 我尝试了以下代码 但它只压缩我的 aspx 页面 从YSlow HttpContext context HttpContext Current
  • 适用于具有两个类的元素的 CSS 选择器

    有没有一种方法可以根据设置为两个特定类的 class 属性的值来选择带有 CSS 的元素 例如 假设我有 3 个 div div class foo Hello Foo div div class foo bar Hello World d
  • NSSortDescriptor问题

    我正在制作一个通讯录应用程序 我从 AddressBook 中获取姓名并将其存储在核心数据中 并使用以下命令将姓名显示在表格上NSFetchedResultsController However出现的第一个索引和部分是 后跟字母 但我想像在
  • 如何从 web.config 引用程序集?

    我需要将对 System XML dll 程序集的引用添加到 web config 中 为了尝试解决问题 此处提到 扩展方法存在问题 IXmlLineInfo 我尝试将以下行添加到 程序集 部分
  • Vue路由器参数对象类型在浏览器后退按钮上更改

    我正在传递一个参数论坛 它是 Vue js 中路由的对象 使用 vue router path forum forum name ForumView component ForumView props true 当我通过使用页面上的另一个链
  • 鼠标悬停时反转文本颜色

    我想在使用自定义 黑色 光标悬停黑色文本时反转黑色文本 这个GIF演示了效果 我无法用 CSS 和 JS 来实现这个工作 我猜是与混合混合模式 剪贴蒙版 伪元素和过滤器的一些组合 以下代码使光标变为白色 但不允许黑色文本变成白色 听起来很抽