Alter the color of the text when hovering over it, only in the area where the

Is there a way to change the color of text only where the mouse is placed, without affecting the entire text? Similar to this example

https://i.sstatic.net/vdYEn.png

I'm looking to have a circle follow the mouse, and when hovering over text, only the border of the letter in that section should be shown.

Can anyone suggest which library or function I should use for this purpose?

Answer №1

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;
}
<div class="container">
  <h1>abc</h1>
  <div class="fx">
    <h1>abc</h1>
  </div>
</div>

this is just a demonstration of an interactive effect using JavaScript and CSS. To implement this in your project, you will need additional logic and a good understanding of two-dimensional concepts. The key idea is to have two elements, one for display (like the h1) and another for the visual effect (inside the fx div). To achieve the effect shown here, ensure that the precise pixel positioning of the container and the background color of fx match the parent element's background color. By adding a mousemove event listener to the parent container (document in this demo), you can make the fx div follow the cursor using its top and left properties set to e.clientY and e.clientX. The challenge lies in making the h1 inside fx stay stationary while following the cursor movement. This is achieved by translating the fx's h1 based on the cursor position relative to the container, as demonstrated with

fx.querySelector("h1").style.transform = ...
. I recommend running through the demo step by step to fully grasp how it works.

Update

An enhanced version of the initial demo has been provided above, offering more flexibility in implementation. Simply copy/paste the code snippets and customize the css properties of the h1 element according to your needs.

const glass = document.querySelector(".magnifying-glass"),
    container = document.querySelector(".container"),
    cInfo = container.getBoundingClientRect();

document.addEventListener("pointermove", e => {
    glass.style.opacity = 1;
    glass.style.transform = `translate(
            ${e.clientX - cInfo.x - (glass.getBoundingClientRect().width / 2)}px, 
            ${e.clientY - cInfo.y - (glass.getBoundingClientRect().height / 2)}px
        )`;
    glass.querySelector("h1").style.transform = `translate(
            ${(e.clientX - (glass.getBoundingClientRect().width / 2) - cInfo.x) * -1}px, 
            ${(e.clientY - (glass.getBoundingClientRect().height / 2) - cInfo.y) * -1}px
        )`;
})
* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
}

.container {
  position: relative;
}

.container .display {
  font-size: 10rem;
  color: red;
}

.container .magnifying-glass {
  width: 100px;
  height: 100px;
  outline: 1px solid blue;
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
  transform: translate(-50%, -50%);
  background-color: white;
  opacity: 0;
}

.container .magnifying-glass .magnify-fx {
  font-size: 10rem;
  color: transparent;
  -webkit-text-stroke: 2px green;
  cursor: default;
}
<div class="container">
  <h1 class="display">abc</h1>
  <div class="magnifying-glass">
    <h1 class="magnify-fx">abc</h1>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it secure to use console.time() in a Node.js environment?

Looking at a small snippet of node.js code, here's what I have: console.time("queryTime"); doAsyncIOBoundThing(function(err, results) { console.timeEnd("queryTime"); // Process the results... }); Running this on my development system gives m ...

What is the best way to highlight a specific city on a map by placing a circle around it?

I am currently developing a project that involves creating a circle around the location of an item on a Google Map. In the code snippet below, when the show tab is clicked, it should display a circle around the item's location on the map: <div cla ...

The issue with the NextJS Layout component is that it is failing to display the page content, with an error message indicating that objects cannot be used

I am embarking on my first project using Next JS and after watching several tutorial videos, I find the Layout component to be a perfect fit for my project! However, I am encountering an issue where the page content does not display. The Menu and Footer a ...

Sending numerous arguments via HTTP DELETE request

In my code, I am trying to send the id and dept fields in a DELETE HTTP request and then retrieve them inside the deleteData() function. However, I am encountering an issue where the values for id and dept are coming up as null within the deleteData() fu ...

I am interested in gradually displaying and then hiding a collection of keywords from an array in a single instance

Experimenting with creating titles that fade in and out on a landing page only once without repeating. I attempted to incorporate a counter variable to ensure the titles stop looping after displaying all items in an array. This method allows for easy add ...

What is the best way to align two HTML elements in a single column?

I need to adjust the layout of an existing <td> in order to display a checkbox and one additional field on the same line. The current setup places these elements on separate lines. Below is the provided HTML code: <!DOCTYPE html> <html> ...

Decrease one of the two values in Mongoose

My User schema consists of two fields: var userSchema = mongoose.Schema({ credits : { type: Number }, energy: { type: Number }, }); I am currently working on a method to decrement one of the values based on the other. The logic is simple - if the ...

Conceal a column within a nested HTML table

I am facing a challenge with a nested table column structure: My goal is to hide specific columns based on their index within the table. Can someone explain the concept behind achieving this? I am unsure of how to get started on this task. <table clas ...

Scripts fail to load randomly due to RequireJs

I have obtained an HTML template from a platform like Themeforest, which came with numerous jQuery plugins and a main.js file where all these plugins are initialized and configured for the template. I am now in the process of developing an AngularJS applic ...

Enhance your input text form with a stylish button using Bootstrap

This is the code snippet I am currently working on: <h2>Workout Selector</h1> <div class="form-group"> <label for="workout-name">Search by Keywords (Comma Separated):</label> <input ...

What is the functionality of Mongoose for handling multiple updates?

Array; arr=[ { id: [ '5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af' ], color: [ 'a', 'b' ] } ] Models; const varyant = Models.varyant function; Promise.all( arr.map((item)=>{ return var ...

placing additional containers at the bottom rather than on the right side

Hey there! I'm currently working on a simple website and I'm having trouble getting the duplicated form rows to appear aligned below the previous one. Here's what I have right now: var i = 0; var original = document.getElementById("dupl ...

Tips for avoiding passing an empty string when using local storage

I am currently using local storage to transfer two form inputs from a form on page A to a form on page B. The process is working smoothly, but I have encountered an issue. When I navigate directly to page B or visit it without inputting any data on page A, ...

"Using jQuery to trigger a click function on elements with the same

Whenever I click the comment link all of the divs are opening but i need that particular only to be opened. HTML: <div> <a href="#" class="ck">comment</a> <div class="cmt"> <input type="text" class="txt"/> ...

Generate radio buttons in a model loop in Vue.js based on names automatically

I am attempting to generate a model for each radio button using the inputname as the identifier. My approach involves looping through the array of objects to identify instances where more than one inputname is present, and then converting this value into a ...

CSS: The addition selection operator is not functioning as expected

The span's background color is not changing correctly when the radio button is changed. Why is this happening and how can it be fixed? div { margin: 0 0 0.75em 0; } .formgroup input[type="radio"] { display: none; } input[type="radio"], label { ...

How does one make sense of comparing integers with strings and arrays?

I was experimenting with the == operator in Javascript, and the results were intriguing: 0 == "0" // true and then 0 == [0] // true BUT: "0" == [] // false It's quite perplexing for someone unfamiliar with Javascript like me. I also observed: ...

What are the implications of including jQuery on a page multiple times?

Currently, I find myself in a scenario where I am incorporating multiple ASCX files into an ASPX page. Some of these files contain the jQuery library, which means that jQuery may be included more than once depending on which ASCX files are added. Unfortun ...

The functionality of Jquery is successfully integrated into a specific function, however it seems to only work for one of the calls. To make the other call work,

After a user makes a selection, I am attempting to reset the 'selected' item. This process calls the Vue function tS() shown below. The issue lies with the first call $('#sel1').prop('selectedIndex',0);, as it only seems to wo ...

What is the best way to remove every other second and third element from an array?

I am looking to remove every second and third element of an array in Javascript. The array I am working with is as follows: var fruits = ["Banana", "yellow", "23", "Orange", "orange", "12", "Apple", "green", "10"]; My goal is to delete every second and ...