Here is my expected Result:

enter image description here

大家好,我不能再现这个图像的结果.

我想要得到图像的结果,我有一个定制的光标,当我将鼠标悬停在文本上时,我希望它的属性发生变化,但只在光标内部.

我希望黑色的文本变成白色(我看到这是可以通过css的混合-混合模式),但我也希望红色的文本变成只轮廓黄色,结合其他属性.

有没有办法创造出这样的东西?

推荐答案

是的,当然...如果您自定义光标可以是某种形状(在下面的示例中它是圆形)

我没有设计光标的样式,但元素"Circle"是随着光标移动的,所以我认为这有同样的感觉. 在这里所有魔力的背后是名为Clip-Path的css属性(更多关于它的信息是here). 如果你有任何问题,尽管问!

附注:查看整页代码截图:)

      const circle = document.getElementById("circle");
      const hoveredTextStyle =
        document.getElementById("hovered-text-style").style;

      document.addEventListener("mousemove", (e) => {
        //circle move
        circle.style.left = e.pageX - 100 + "px";
        circle.style.top = e.pageY - 100 + "px";

        //position and size of text
        const textRect = document.querySelector("span").getBoundingClientRect();

        //circle with our text collision detection
        if (
          e.clientY + 100 > textRect.top &&
          e.clientY - 100 < textRect.bottom
        ) {
          const clipPathX = e.clientX - textRect.left;
          const clipPathY = e.clientY - textRect.top;
          hoveredTextStyle.clipPath = `circle(100px at ${clipPathX}px ${clipPathY}px)`;
        } else {
          hoveredTextStyle.clipPath =
            "circle(0 at 0 0)";
        }
      });
* {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        cursor: none;
         }
      body {
        width: 100vw;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 100px;
        position: relative;
      }
      .container {
        position: relative;
        background-color: rgb(13, 215, 60);
      }
      .container span:not(.original-text) {
        position: absolute;
        left: 0;
        top: 0;
      }
      #normal-text-style {
        /* Normal text style */
        position: relative;
        z-index: 1;
      }
      #hovered-text-style {
        /* Text style when its hovered */
        color: white;
        text-decoration: underline;
        z-index: 2;
        clip-path: circle(0 at 0 0 ); 
      }
      #circle {
        top: 0;
        position: absolute;
        width: 200px;
        aspect-ratio: 1/1;
        border: 4px solid red;
        border-radius: 50%;
        z-index: 9999;
      }
    <div id="circle"></div>
    <div class="container">
      <span id="normal-text-style">Some magic text</span>
      <span id="hovered-text-style">Some magic text</span>
    </div>  

Html相关问答推荐

为什么在这种情况下是弹性基础:自动不解决内容的大小?

使用固定的HTML代码创建两个可向右滚动的行

输入表单在奇怪的地方舍入的标签

在FlexBox中将div拉伸到父div的完全高度

覆盖垫子中的边框底部 Select

Django中的图像未从静态显示

我的Django应用程序中出现模板语法错误

使用CSS Flexbox时,我的图像未对齐

来自元标记的响应文本

如何将多个类名组合到CSS中的一个关键帧

在移动屏幕上显示时分支树视图的响应能力问题

如何将 元素与常规文本垂直对齐

复制两个

会产生一个空行;复制

元素不会

文本不显示在 div 下方

在React中如何处理HTML本地日期输入?

如何避免链接在悬停 timeshift 动,同时增加导航栏中的字体大小?

如何垂直平移一个元素,使其新位置位于另外两个元素之间?

如何将两个平行 div 的页面内的表格居中?

如何更改 Quarto 中标签crossref-def-title的标题 colored颜色 ?

两部分问题 - 是什么导致了这个空白?