clip-path bounces at the top due to offset in Javascript?

Viewed 64

I have created a hover effect like whenever you hover on the box you will see a circle (actually it is a div with position: absolute whose clip-path position is controlled by Javascript offset) and when this circle reaches the second paragraph element it jumps to the top. So I wanna know why it's happening and how to make the circle move consistently throughout the box without giving a jerk or moving to the top.

Here is my code

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>

1 Answers

Since there are two paragraphs in place, the mouse movement triggers to choose the paragraph over the parent. You can choose to turn off the trigger using pointer-events: none on the paragraph elements.

MDN info about pointer-events: none

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>

Related