3d悬停效果反向动画

发布于 2025-01-21 03:57:11 字数 797 浏览 1 评论 0原文

我发现了这个很酷的 3D 倾斜悬停图像效果,归功于此 codepen: https://codepen.io/technokami/pen/abojmZa

我发现的唯一问题是,当您开始将鼠标悬停在其上时,动画就会开始,但是一旦您将鼠标移开,它就会立即重置到起点。这可能是一个小细节,但如果动画能够顺利恢复,那就更好了。

(例如:http://jsfiddle.net/Ugc5g/892/ 它是不一样,但描述了我的意思,通过反向动画,我想用第一个3d悬停动画codepen实现类似的反向动画,缩放并旋转回原点位置,而不仅仅是跳跃back)

        /* Add listener for mouseout event, remove the rotation 
TODO: add reverse animation scale and rotate back to original smoothly
*/
    el.addEventListener('mouseout', function() {
      el.style.transform = 'perspective(500px) scale(1) rotateX(0) rotateY(0)'
    })

我尝试修复它,但找不到合适的解决方案,希望这里的CSS动画大师可以帮助我。

I found this cool 3d tilt hover image effect, credit to this codepen:
https://codepen.io/technokami/pen/abojmZa

The only problem I found, when you start to hover over it, the animation starts, but as soon as you move the mouse away, it immediately resets to its starting point. It might be a small detail, but would so much better if the animation would smoothly revert back.

(For example: http://jsfiddle.net/Ugc5g/892/ it is not the same, but describes what I mean, with reverse back the animation, I would like to achieve the similarly reverse back animation with the first 3d hover animation codepen, scale and rotate back to the origin position, not just jumping back)

        /* Add listener for mouseout event, remove the rotation 
TODO: add reverse animation scale and rotate back to original smoothly
*/
    el.addEventListener('mouseout', function() {
      el.style.transform = 'perspective(500px) scale(1) rotateX(0) rotateY(0)'
    })

I tried to fix it, but could not find the proper solution for it, hope a css animation master here can help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2025-01-28 03:57:11

像这样还是通过360来旋转?这是通过延迟它来使其平滑。

/* Store the element in el */
let el = document.getElementById('tilt')

/* Get the height and width of the element */
const height = el.clientHeight
const width = el.clientWidth

/*
  * Add a listener for mousemove event
  * Which will trigger function 'handleMove'
  * On mousemove
  */
el.addEventListener('mousemove', handleMove)

/* Define function a */
function handleMove(e) {
  /*
    * Get position of mouse cursor
    * With respect to the element
    * On mouseover
    */
  /* Store the x position */
  const xVal = e.layerX
  /* Store the y position */
  const yVal = e.layerY
  
  /*
    * Calculate rotation valuee along the Y-axis
    * Here the multiplier 20 is to
    * Control the rotation
    * You can change the value and see the results
    */
  const yRotation = 20 * ((xVal - width / 2) / width)
  
  /* Calculate the rotation along the X-axis */
  const xRotation = -20 * ((yVal - height / 2) / height)
  
  /* Generate string for CSS transform property */
  const string = 'perspective(500px) scale(1.1) rotateX(' + xRotation + 'deg) rotateY(' + yRotation + 'deg)'
  
  /* Apply the calculated transformation */
  el.style.transform = string
}

/* Add listener for mouseout event, remove the rotation */
el.addEventListener('mouseout', function() {
  el.style.transform = ' perspective(500px) scale(1) rotateX(0) rotateY(0) ';
  el.style.transition = 'all 3s ease-out';
})

/* Add listener for mouseover event, to simulate click */
el.addEventListener('mouseover', function() {
  el.style.transform = 'perspective(500px) scale(0.9) rotateX(0) rotateY(0)';
   el.style.transition = 'all 0s ease-out'  
})

/* Add listener for hover, simulate release of mouse click */
el.addEventListener('hover', function() {
  el.style.transform = 'perspective(500px) scale(1.1) rotateX(0) rotateY(0)';
  el.style.transition = 'all 0s ease-out';
    
});
html {
 display: flex;
  justify-content: center;
  align-items: center;
  align-contents: center;
  height: 100%;
}

/* Styles for the tilt block */
#tilt {
  display: block;
  height: 200px;
  width: 300px;
  background-color: grey;
  margin: 0 auto;
  transition: box-shadow 0.1s, transform 0.1s;
  
  /*
    * Adding image to the background
    * No relation to the hover effect.
    */
  background-image: url(http://unsplash.it/300/200);
  background-size: 100%;
  background-repeat: no-repeat;
}

#tilt:hover {
  box-shadow: 0px 0px 30px rgba(0,0,0, 0.6);
  cursor: pointer;
}
<div id="tilt">
  <!--  Container for our block  -->
</div>

Like this or rotate back by going to 360?? this one is to smooth it back by delaying it.

/* Store the element in el */
let el = document.getElementById('tilt')

/* Get the height and width of the element */
const height = el.clientHeight
const width = el.clientWidth

/*
  * Add a listener for mousemove event
  * Which will trigger function 'handleMove'
  * On mousemove
  */
el.addEventListener('mousemove', handleMove)

/* Define function a */
function handleMove(e) {
  /*
    * Get position of mouse cursor
    * With respect to the element
    * On mouseover
    */
  /* Store the x position */
  const xVal = e.layerX
  /* Store the y position */
  const yVal = e.layerY
  
  /*
    * Calculate rotation valuee along the Y-axis
    * Here the multiplier 20 is to
    * Control the rotation
    * You can change the value and see the results
    */
  const yRotation = 20 * ((xVal - width / 2) / width)
  
  /* Calculate the rotation along the X-axis */
  const xRotation = -20 * ((yVal - height / 2) / height)
  
  /* Generate string for CSS transform property */
  const string = 'perspective(500px) scale(1.1) rotateX(' + xRotation + 'deg) rotateY(' + yRotation + 'deg)'
  
  /* Apply the calculated transformation */
  el.style.transform = string
}

/* Add listener for mouseout event, remove the rotation */
el.addEventListener('mouseout', function() {
  el.style.transform = ' perspective(500px) scale(1) rotateX(0) rotateY(0) ';
  el.style.transition = 'all 3s ease-out';
})

/* Add listener for mouseover event, to simulate click */
el.addEventListener('mouseover', function() {
  el.style.transform = 'perspective(500px) scale(0.9) rotateX(0) rotateY(0)';
   el.style.transition = 'all 0s ease-out'  
})

/* Add listener for hover, simulate release of mouse click */
el.addEventListener('hover', function() {
  el.style.transform = 'perspective(500px) scale(1.1) rotateX(0) rotateY(0)';
  el.style.transition = 'all 0s ease-out';
    
});
html {
 display: flex;
  justify-content: center;
  align-items: center;
  align-contents: center;
  height: 100%;
}

/* Styles for the tilt block */
#tilt {
  display: block;
  height: 200px;
  width: 300px;
  background-color: grey;
  margin: 0 auto;
  transition: box-shadow 0.1s, transform 0.1s;
  
  /*
    * Adding image to the background
    * No relation to the hover effect.
    */
  background-image: url(http://unsplash.it/300/200);
  background-size: 100%;
  background-repeat: no-repeat;
}

#tilt:hover {
  box-shadow: 0px 0px 30px rgba(0,0,0, 0.6);
  cursor: pointer;
}
<div id="tilt">
  <!--  Container for our block  -->
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文