如何在鼠标移动悬停时在文本的背景图像中设置动画?

时间:2021-02-24 05:55:28

标签: html css svg css-animations

我希望在下面的示例中使用动画 Example Demo

所以我想在我的设计中使用相同的动画。有没有办法在我的情况下添加这个动画。?

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@700;900&display=swap');
.hero-text-block h1 {
  font-family: 'Poppins', sans-serif;
  font-size: 150px;
  font-weight: 700;
  color: #121212;
  text-shadow: 6px 8px 9px rgb(22 36 65 / 20%);
  margin: 0 0 22px 0;
  line-height: 120px;
  background-image: url(https://i.stack.imgur.com/K5WX0.jpg);
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  background-position: center center;
  background-size: 100%;
  transition: all 0.3s ease;
}

.hero-text-block h1:hover {
  background-size: 104%;
}
<div class="hero-text-block">
  <h1>DESIGN</h1>
</div>

1 个答案:

答案 0 :(得分:2)

您可能需要 javascript 的帮助:

"primarycontactid@odata.bind":"/contacts(alternate_key_field='2222222')"
const h1 = document.querySelector('h1');
h1.addEventListener('mousemove', ({ target, offsetX, offsetY }) => {
  const x = offsetX / target.clientWidth;
  const y = offsetY / target.clientHeight;
  
  target.style.backgroundPosition = `${100 - x*100}% ${y*100}%`;
})
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@700;900&display=swap');

.hero-text-block h1 {
    font-family: 'Poppins', sans-serif;
    font-size: 100px;
    font-weight: 700;
    color: #121212;
    text-shadow: 6px 8px 9px rgb(22 36 65 / 20%);
    margin: 0 0 22px 0;
    line-height: 120px;
    background-image: url(https://i.stack.imgur.com/K5WX0.jpg);
    background-clip: text;
    -webkit-background-clip: text;
    color: transparent;
    background-position: center center;
    background-size: 100%;
    transition: background-size 0.3s ease;
    max-width: 800px;
    background-repeat: no-repeat;
}
.hero-text-block h1:hover {
    background-size: 104%;
}