The div element following my cursor (JS) disappears when I hover over a CSS keyframe animation – how can I fix this?

huangapple go评论79阅读模式
英文:

The div element following my cursor (JS) disappears when I hover over a CSS keyframe animation - how can I fix this?

问题

I'm using a mousemove event in JS to get a small ball image (encased in a div) to follow my cursor.
我正在使用JS中的mousemove事件来让一个小球形图像(包裹在一个div中)跟随我的光标移动。

I also have an image in a separate div which uses keyframes animation in CSS to shake when the mouse cursor hovers over it.
我还有一个图像在一个单独的div中,当鼠标光标悬停在它上面时,它会使用CSS中的关键帧动画来摇晃。

My problem is that when I hover over the keyframe image and it shakes, the ball image disappears from the cursor.
我的问题是,当我将光标悬停在关键帧图像上并且它摇晃时,小球图像会从光标中消失。

Is there any way to keep it visible over the top of the shaking image, while the shaking image is actively being animated?
是否有办法在摇晃的图像上方保持它可见,而摇晃的图像正在进行动画?

(请注意,这只是翻译,不包括问题的回答部分。)

英文:

I'm using a mousemove event in JS to get a small ball image (encased in a div) to follow my cursor. I also have an image in a separate div which uses keyframes animation in CSS to shake when the mouse cursor hovers over it.

My problem is that when I hover over the keyframe image and it shakes, the ball image disappears from the cursor.

Is there anyway to keep it visible over the top of the shaking image, while the shaking image is actively being animated?

body {
  background-color: white;
}

h1 {
  color: teal;
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
}

div {
  background-color: teal;
  padding: 5%;
  text-align: center;
}

#circle {
  max-width: 10%;
  background-color: transparent;
  position: absolute;
}

#circle:hover {
  cursor: url('***********'), auto;
}

#shakingimage {
  max-width: 45%;
  position: relative;
}

#shakingimage:hover {
  /* Start the shake animation and make the animation last for 0.5 seconds */
  animation: shake 0.5s;

  /* When the animation is finished, start again */
  animation-iteration-count: infinite;

  cursor: url('************'), auto;
}

@keyframes shake {
  0% {
    transform: translate(1px, 1px) rotate(0deg);
  }
  10% {
    transform: translate(-1px, -2px) rotate(-1deg);
  }
  20% {
    transform: translate(-3px, 0px) rotate(1deg);
  }
  30% {
    transform: translate(3px, 2px) rotate(0deg);
  }
  40% {
    transform: translate(1px, -1px) rotate(1deg);
  }
  50% {
    transform: translate(-1px, 2px) rotate(-1deg);
  }
  60% {
    transform: translate(-3px, 1px) rotate(0deg);
  }
  70% {
    transform: translate(3px, 1px) rotate(-1deg);
  }
  80% {
    transform: translate(-1px, -1px) rotate(1deg);
  }
  90% {
    transform: translate(1px, 2px) rotate(0deg);
  }
  100% {
    transform: translate(1px, -2px) rotate(-1deg);
  }
}

.buttonbox {
  display: inline-flex;
  justify-content: center;
  flex-direction: row;
}

button {
  padding: 15px 25px;
  margin: 5px;
  font-size: 24px;
  text-align: center;
  cursor: pointer;
  outline: solid 2px white;
  color: #ffffff;
  background-color: #008080;
  border: none;
  border-radius: 15px;
  box-shadow: 0 9px #999;
}

button:hover {
  background-color: rgb(0, 172, 172);
}

button:active {
  background-color: #008080;
  box-shadow: 0 5px #666;
  transform: translateY(4px);
}

let circle = document.getElementById('circle')
const onMouseMove = (e) => {
  circle.style.left = e.pageX + 'px'
  circle.style.top = e.pageY + 'px'
}
document.addEventListener('mousemove', onMouseMove)

答案1

得分: 0

将小球图像保持在抖动图像(shakingimage)上可见,您可以为#circle元素应用一个更高的z-index值。我认为这会将其定位在堆叠顺序中高于shakingimage。

您可以尝试将您的CSS代码更新如下:

#circle {
  max-width: 10%;
  background-color: transparent;
  position: absolute;
  z-index: 2; 
}
#shakingimage {
  max-width: 45%;
  position: relative;
  z-index: 1; 
}
英文:

to make that the small ball image circle remains visible over the shaking image shakingimage) you can apply a higher z-index value to the #circle element. I think this will position it above the shakingimage in the stacking order.
how about updating your CSS code as the following

#circle {
  max-width: 10%;
  background-color: transparent;
  position: absolute;
  z-index: 2; 
}
#shakingimage {
  max-width: 45%;
  position: relative;
  z-index: 1; 
}

huangapple
  • 本文由 发表于 2023年6月8日 10:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定