search

Home  >  Q&A  >  body text

Hovered text will change color at the mouse location

How to change only the text color at the mouse location instead of the color of all text? like this

I want the circle that you can see to follow the mouse movement, and on hover the text only shows a border for the portion of the letter in hover

I need advice on which library or function I need to use?

P粉697408921P粉697408921242 days ago506

reply all(1)I'll reply

  • P粉936568533

    P粉9365685332024-03-29 12:26:14

    const fx = document.querySelector(".fx");
    
    document.addEventListener("pointermove", e => {
      fx.style.top = e.clientY + "px";
      fx.style.left = e.clientX + "px";
      fx.querySelector("h1").style.transform = `translate(${(e.clientX - fx.getBoundingClientRect().width / 2) * -1}px, ${(e.clientY - fx.getBoundingClientRect().height / 2) * -1}px)`;
    })
    * {
      box-sizing: border-box;
      padding: 0;
      margin: 0;
    }
    
    .container {
      position: relative;
    }
    
    .container h1 {
      font-size: 10rem;
      color: red;
    }
    
    .container .fx {
      width: 100px;
      height: 100px;
      outline: 1px solid blue;
      border-radius: 50%;
      position: absolute;
      top: -100px;
      left: -100px;
      overflow: hidden;
      transform: translate(-50%, -50%);
      background-color: white;
    }
    
    .container .fx h1 {
      font-size: 10rem;
      color: transparent;
      -webkit-text-stroke: 2px green;
      cursor: default;
      position: relative;
    }

    abc

    abc

    reply
    0
  • Cancelreply