有趣的CSS - 多彩变化的按钮

目录

整体效果

这个按钮效果主要使用 :hover:active 伪选择器以及 animationtransition 属性来让背景色循环快速移动形成视觉效果。


核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

html 复制代码
<button>戳一下</button>

写上主体 button 标签。

css 部分代码

css 复制代码
button{
  width: 140px;
  height: 46px;
  font-size: 16px;
  font-weight: 700;
  color: black;
  border: 2px solid #ffffff;
  border-radius: 10px;
  background-color: #4158D0;
  background-image: linear-gradient(90deg, #4158D0 0%, #C850C0 17%, #e6a731 39%, #8329e2 60%, #3fb75f 80%, #4158D0 100%);
  box-shadow: 0 0 0 2px #000000;
  cursor: pointer;
  transition: all 0.5s ease;
}
button:hover{
  color: #ffffff;
  animation: quick 0.5s linear infinite;  /* 设置动画参数且循坏播放 */
}
@keyframes quick{
  to {
    background-position: 140px 0;  /*  这里的X轴的值等于button的宽度 */
  }
}
button:active{
  transform: translateY(1px);
}

css 部分主要通过 :hover 伪选择器判断鼠标悬浮时,设置 animation 参数让背景色沿 X 轴循环移动,注意这里变化的 background-positon 部分的 X 轴值等于按钮宽度,即 140px,这样动画循环播放时就不会造成视觉断层。

完整代码如下

html 页面

html 复制代码
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>多彩变化的按钮</title>
  </head>
  <body>
    <div class="app">
      <button>戳一下</button>
    </div>
  </body>
</html>

css 样式

css 复制代码
/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
button{
  width: 140px;
  height: 46px;
  font-size: 16px;
  font-weight: 700;
  color: black;
  border: 2px solid #ffffff;
  border-radius: 10px;
  background-color: #4158D0;
  background-image: linear-gradient(90deg, #4158D0 0%, #C850C0 17%, #e6a731 39%, #8329e2 60%, #3fb75f 80%, #4158D0 100%);
  box-shadow: 0 0 0 2px #000000;
  cursor: pointer;
  transition: all 0.5s ease;
}
button:hover{
  color: #ffffff;
  animation: quick 0.5s linear infinite;
}
@keyframes quick{
  to {
    background-position: 140px;
  }
}
button:active{
  transform: translateY(1px);
}

页面渲染效果

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。


1\] [原文阅读](https://mp.weixin.qq.com/s/SKInwEdF6dxpv0sZLkXTkg) 我是 Just,这里是「设计师工作日常」,求点赞求关注!skr\~ skr\~ skr\~

相关推荐
共享家952714 小时前
QT-常用控件(多元素控件)
开发语言·前端·qt
葱头的故事14 小时前
将传给后端的数据转换为以formData的类型传递
开发语言·前端·javascript
_233315 小时前
vue3二次封装element-plus表格,slot透传,动态slot。
前端·vue.js
jump68015 小时前
js中数组详解
前端·面试
崽崽长肉肉15 小时前
iOS 基于Vision.framework从图片中提取文字
前端
温宇飞15 小时前
Web Abort API - AbortSignal 与 AbortController
前端
Tomoon15 小时前
前端开发者的全栈逆袭
前端
Filotimo_15 小时前
2.CSS3.(3).html
前端·css3
whyfail15 小时前
React v19.2版本
前端·javascript·react.js