Does clip-path jump at the top because of JavaScript offset?

When hovering on the box, a circle appears and moves based on JavaScript offset values. However, when the circle reaches the second paragraph element, it jumps to the top. I am looking to understand why this is happening and how to ensure smooth movement of the circle throughout the box without any sudden jerks or jumping.

Below is my code snippet:

let container = document.querySelector(".container")
let secondDiv = document.querySelector(".test")

secondDiv.addEventListener("mousemove",function(e){
   secondDiv.style.setProperty("--x",e.offsetX + "px")
   secondDiv.style.setProperty("--y",e.offsetY + "px")
   //console.log(e.offsetX)
})
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: sans-serif;
}
.container{
  width:70%;
  margin: 0 auto;
  border:2px solid red;
  height:150px;
  position:relative;
  background: #9F86C0;
  color:white;
  margin-top: 10%;
}
.container p{
  font-size:2.3em;
}
.container .content{
  position: absolute;
  top:0;
  left:0;
  width: 100%;
  height:150px;
  padding:1em;
}
.container .content:nth-child(2){
  background: #E0B1CB;
  color:white;
  clip-path:circle(50px at var(--x) var(--y))
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="container">
    <div class="content check">
      <p>Hello People hover me</p>
      <p>What's going on?</p>
    </div>
    <div class="content test">
      <p>Hello People hover me</p>
      <p>What's going on?</p>
    </div>
  </div>
</body>
</html>

Answer №1

When there are two paragraphs present, the mouse movement will activate the paragraph instead of the parent element. To disable this behavior, you can utilize pointer-events: none on the paragraph elements.

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

let container = document.querySelector(".container")
let secondDiv = document.querySelector(".test")

container.addEventListener("mousemove", function(e) {
  secondDiv.style.setProperty("--x", e.offsetX + "px")
  secondDiv.style.setProperty("--y", e.offsetY + "px")
  //console.log(e.offsetX)
})
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: sans-serif;
}

.container {
  width: 70%;
  margin: 0 auto;
  border: 2px solid red;
  height: 150px;
  position: relative;
  background: #9F86C0;
  color: white;
  margin-top: 10%;
}

.container p {
  font-size: 2.3em;
  pointer-events: none; /* Added */
}

.container .content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  padding: 1em;
}

.container .content:nth-child(2) {
  background: #E0B1CB;
  color: white;
  clip-path: circle(50px at var(--x) var(--y))
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div class="container">
    <div class="content check">
      <p>Hello People hover me</p>
      <p>What's going on?</p>
    </div>
    <div class="content test">
      <p>Hello People hover me</p>
      <p>What's going on?</p>
    </div>
  </div>
</body>

</html>

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

Stop the child component from activating the parent's onClick event

Trying to implement Material-ui within a React project, I am facing an issue with the <IconButton> component and its onClick behavior conflicting with the parent component's behavior. Specifically, I want the <IconButton> to add an item to ...

Is there a way to verify HTML binding prior to setting up an AngularJS directive?

On a page where I utilized a custom select-box directive to display the Month, certain arguments are required by the directive: <custom-select-box id="month" model="month" model-required model-name="month" options="month.value ...

JavaScript: Repeated Depth First Exploration for hierarchical rearrangement implemented with d3's recursion()

I'm attempting to perform a depth-first search on a complex JSON object, such as the one available through this link. My goal is to generate a modified object structure that looks like this: [ { name: "Original Object", children : [ ...

JQuery Mobile Navigation with Bootstrap 3 Sidebar on the Go

While using RoR, I encountered a baffling issue. I have created a new navbar with a sidebar that only displays on mobile and not on desktop. The Turbolink is functioning properly. <%= javascript_include_tag 'application', 'data-turboli ...

Utilizing express.js alongside the native MongoDB driver: A comprehensive guide

I'm facing difficulties when attempting a simple mongoDB query from my express app: app.js var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); var http = re ...

Displaying dropdown options based on the previous selection made by the user

Can I link the data in my second dropdown to the selection made in the first dropdown? I tried a similar solution from stackoverflow without success. You can find the reference here. The code works when directly copied and pasted but not within my system. ...

Exploring the World of Angularjs 2

Currently, I am diving into learning angularjs 2. I found a helpful git repository that I am following closely, which can be found here. The repository contains some interesting codes in the index.html file. <script src="node_modules/core-js/client/shi ...

Oops! An error occurred: Unable to access the 'appendChild' property of null. Any ideas on how to modify the CSS property of an open ShadowRoot?

https://i.sstatic.net/lBZqx.png ShadowRoot(open) I am looking to adjust the z-index property within this ShadowRoot. host = document.getElementById("shadow-root"); var style = document.createElement( 'style' ) style.innerHTML = '.rss ...

New to WCF: How to Invoke Methods using JavaScript

Hello there! As someone new to WCF, I initially thought it would be similar to ASP.NET web services. However, I'm facing an issue where I am unable to call a method from client-side JavaScript. Here is a snippet of my web form: <form id="form1" r ...

Rendering the tbody data outside of the table in ReactJS

When rendering data into a table to create a dynamic table, the tbody data is appearing outside of the table. Reproduction link: http://codepen.io/sumitridhal/pen/wdoZKR?editors=1010 Expected Outcome: https://i.sstatic.net/VPRrB.png Code Snippet: class ...

Retrieve data from previous page without triggering a reload using AngularJS

Custom Routes : app.config(['$routeProvider', function ($routeProvider) { $routeProvider .when("/home/:page", {templateUrl: "templates/home.html", controller: "HomeController"}) .otherwise("/404", {templateUr ...

Including new styles in CKEDITOR.stylesSet that pertain to multiple elements

My idea involves creating a unique bullet list style with two specific features: a. A blue arrow image replacing the standard list icon b. Very subtle dotted borders above and below each list item. I am looking to incorporate this design into CKEditor t ...

Dealing with React Native text overflowing beyond the screen width when using FlexWrap

I'm currently working on implementing a component in react native that consists of a row containing and components, but I'm struggling to achieve the desired outcome. Here's my current code: <View style={{ flexDirection: ...

Creating Responsive Social Icons using HTML and CSS

Currently working on my portfolio website, but encountered an error. Can someone assist me in making my social icons responsive? I need the icons to be responsive and aligned horizontally for the mobile version. Here is a visual of the issue with the desk ...

What is the best method to style a div using CSS to replicate the functionality of this image tag?

I have a dilemma with my image tag: <img src="/images/blah.gif" alt="blah" /> Is there a way to make this work using only CSS? I attempted the following, but unfortunately, the image is not showing up. What could be the issue? .blah { backgr ...

Having difficulty operating the development environment

Currently enrolled in a Udemy course and facing an issue with running a script. Attempting to execute the command npm run dev results in the following error message: npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! <a href="/cdn-cgi/l/email-protectio ...

How to efficiently remove duplicate items from multiple select dropdowns in Angular using ng-repeat?

Need help with dynamically assigning objects to select boxes in AngularJS. I have a scenario where I add multiple select boxes to a form, but each box should only display items that haven't been selected in other boxes. How can I achieve this functio ...

Identify file size upon upload using Javascript event and allow cancellation

Currently, I am utilizing a file uploader functionality. My preference is to refrain from verifying the file size on the server side. Instead, I am in search of a method that involves monitoring the uploaded file's size periodically through a listener ...

What could be the reason behind three.js SVGLoader flipping SVGs upside down during rendering?

My solution for rendering svgs in three.js involves using the group.scale.y = -1; trick, but it has created an issue with the y coordinate system. When I apply this trick to correct the upside-down svg rendering, the positive y coordinates no longer push t ...

Encrypting and decrypting data using RSA in TypeScript

Currently, I am utilizing Angular 4 to develop the front end of my application. For authentication, I have integrated OAuth2 on the backend (which is created using Spring in Java), ensuring that only authorized individuals can access my app. However, ther ...