当前位置:Gxlcms > css > 如何使用纯CSS实现带有金属光泽的立体按钮的动画效果(附源码)

如何使用纯CSS实现带有金属光泽的立体按钮的动画效果(附源码)

时间:2021-07-01 10:21:17 帮助过:5人阅读

本篇文章给大家带来的内容是关于如何使用纯CSS实现带有金属光泽的立体按钮的动画效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

2021531372-5b738bc6d477f_articlex.gif

源代码下载

https://github.com/comehope/front-end-daily-challenges/tree/master/004-metallic-glossy-3d-button-effects

代码解读

在 dom 中定义一个容器:

  1. <div class="box">BUTTON</div>

容器居中显示:

  1. html, body {
  2. height: 100%;
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. background-color: skyblue;
  7. }

设置按钮的 2d 样式,为了便于调整按钮尺寸,使用了变量:

  1. .box {
  2. background: linear-gradient(to right, gold, darkorange);
  3. color: white;
  4. --width: 250px;
  5. --height: calc(var(--width) / 3);
  6. width: var(--width);
  7. height: var(--height);
  8. text-align: center;
  9. line-height: var(--height);
  10. font-size: calc(var(--height) / 2.5);
  11. font-family: sans-serif;
  12. letter-spacing: 0.2em;
  13. border: 1px solid darkgoldenrod;
  14. border-radius: 2em;
  15. }

设置按钮的 3d 样式:

  1. .box {
  2. transform: perspective(500px) rotateY(-15deg);
  3. text-shadow: 6px 3px 2px rgba(0, 0, 0, 0.2);
  4. box-shadow: 2px 0 0 5px rgba(0, 0, 0, 0.2);
  5. }

定义按钮的鼠标划过动画效果:

  1. .box:hover {
  2. transform: perspective(500px) rotateY(15deg);
  3. text-shadow: -6px 3px 2px rgba(0, 0, 0, 0.2);
  4. box-shadow: -2px 0 0 5px rgba(0, 0, 0, 0.2);
  5. }
  6. .box {
  7. transition: 0.5s;
  8. }

用伪元素增加光泽:

  1. .box {
  2. position: relative;
  3. }
  4. .box::before {
  5. content: '';
  6. position: absolute;
  7. width: 100%;
  8. height: 100%;
  9. background: linear-gradient(to right, transparent, white, transparent);
  10. left: 0;
  11. }

定义光泽动画效果:

  1. .box::before {
  2. left: -100%;
  3. transition: 0.5s;
  4. }
  5. .box:hover::before {
  6. left: 100%;
  7. }

最后,隐藏容器之外的内容:

  1. .box {
  2. overflow: hidden;
  3. }

大功告成!

相关推荐:

如何使用纯CSS实现文字断开的动画效果(附源码)

如何使用CSS实现渐变色动画边框的效果(附代码)

如何使用CSS和混色模式实现loader动画效果(附代码)

以上就是如何使用纯CSS实现带有金属光泽的立体按钮的动画效果(附源码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行