有趣的css - 背景收缩动画按钮

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是借助 css transition 属性绘制出一个背景收缩交互的一个按钮。

最新文章通过公众号「设计师工作日常」发布。


整体效果

知识点:
① 关于 transition 过渡属性的运用
:before:after 伪元素以及 content 的属性使用

思路:
创建按钮标签,然后当鼠标悬浮时,利用伪元素以及内阴影属性变化实现按钮背景变化。


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

核心代码

html 代码

<div class="btn56" data-text="一个按钮"></div>

创建一个按钮标签。

css 部分代码

.btn56{
  width: 120px;
  height: 46px;
  font-size: 16px;
  font-weight: 400;
  letter-spacing: 2px;
  background: transparent;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  border: 1px solid #457356;
  border-radius: 23px;
  color: #457356;
  box-sizing: border-box;
  transition: color 0.6s cubic-bezier(0.65, 0.05, 0.36, 1);
  overflow: hidden;
}
.btn56:before{
  content: '';
  position: absolute;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: block;
  transition: box-shadow 0.6s cubic-bezier(0.65, 0.05, 0.36, 1);
}
.btn56:after{
  content: attr(data-text);
  position: absolute;
  display: block;
  z-index: 5;
}
.btn56:hover{
  color: #fff;
}
.btn56:hover:before {
  box-shadow: inset 0 0 0 120px #457356;
}
.btn56:active{
  transform: scale(0.99) translateY(1px);
}

1、 给 .btn56 标签增加样式,边框、字体颜色、以及背景设定透明,定义 overflow: hidden 属性,溢出部分隐藏。

2、利用 :before 创建伪元素,并且样式定义成一个圆形,设置 transition 属性,定义 box-shadow 过渡时间以及曲线。

3、利用 :after 创建伪元素,通过 content 属性搭配 attr 创建字体内容。

4、通过 :hover:active 判断鼠标状态,根据鼠标状态来改变按钮样式,并且启动 transition 过渡效果。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>背景收缩动画按钮</title>
  </head>
  <body>
    <div class="app">
      <div class="btn56" data-text="一个按钮"></div>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn56{
  width: 120px;
  height: 46px;
  font-size: 16px;
  font-weight: 400;
  letter-spacing: 2px;
  background: transparent;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  border: 1px solid #457356;
  border-radius: 23px;
  color: #457356;
  box-sizing: border-box;
  transition: color 0.6s cubic-bezier(0.65, 0.05, 0.36, 1);
  overflow: hidden;
}
.btn56:before{
  content: '';
  position: absolute;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  display: block;
  transition: box-shadow 0.6s cubic-bezier(0.65, 0.05, 0.36, 1);
}
.btn56:after{
  content: attr(data-text);
  position: absolute;
  display: block;
  z-index: 5;
}
.btn56:hover{
  color: #fff;
}
.btn56:hover:before {
  box-shadow: inset 0 0 0 120px #457356;
}
.btn56:active{
  transform: scale(0.99) translateY(1px);
}

页面渲染效果

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


[1] 原文阅读


CSS 是一种很酷很有趣的计算机语言,在这里跟大家分享一些 CSS 实例 Demo,为学习者获取灵感和思路提供一点帮助,希望你们喜欢。

我是 Just,这里是「设计师工作日常」,求点赞求关注!

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

设计师工作日常

请我炫个饼🫓

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值