有趣的css - 引导式详情按钮

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是一个带有图标动效的引导式按钮,适用于阅读详情的引导点击场景。

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


整体效果

知识点:
:hover 获取鼠标状态来切换样式
② 伪元素 :before:after 的使用
animation 动画的使用

思路:分别创建图标内容以及文字内容,然后根据鼠标状态来让图标背景变宽,然后让图标滑动起来。


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

核心代码

html 代码

<button class="btn58">
  <span class="icon58"></span>
  <span class="btn-text58">查看详情</span>
</button>

button 标签作为按钮主体,然后在 button 标签内创建两个 span 标签,分别是图标内容 class="icon58" 以及文字内容 class="btn-text58"

css 部分代码

.btn58{
  width: 120px;
  height: 32px;
  position: relative;
  padding: 0;
  border: none;
  background-color: transparent;
  cursor: pointer;
  display: flex;
  justify-content: left;
  align-items: center;
}
.icon58{
  width: 32px;
  height: 32px;
  position: relative;
  box-sizing: border-box;
  background-color: #ffd845;
  display: block;
  border-radius: 16px;
  transition: 0.3s;
}
.icon58:before,.icon58:after{
  content: '';
  width: 3px;
  height: 10px;
  background-color: #000000;
  display: block;
  position: absolute;
  top: 8px;
  left: 16px;
  transform: rotate(-45deg);
}
.icon58:after{
  top: 14px;
  left: 16px;
  transform: rotate(45deg);
}
.btn-text58{
  font-size: 16px;
  color: #000000;
  font-weight: bold;
  display: block;
  position: absolute;
  left: 40px;
}
.btn58:hover .icon58{
  width: 120px;
}
.btn58:hover .icon58:before,.btn58:hover .icon58:after{
  animation: move58eff 1.2s linear infinite;
}
@keyframes move58eff{
  0% {
    left: 16px;
  }
  50% {
    left: 26px;
  }
  100% {
    left: 16px;
  }
}

1、先定义图标 .icon58 整体的样式,然后利用伪元素 .icon58:before.icon58:after 绘制两个矩形,并 transform: rotate(...) 旋转两个矩形的角度,形成箭头图标。

2、定义文字内容 .btn-text58 样式,设置 position: absolute;,调整合适的位置,让其与图标内容对齐,间距合适就好 。

3、根据 button 标签主体的 :hover 状态,让图标内容 .icon58 的背景宽度增加,包裹住旁边的文字内容,且有一定的间距即可;然后给图标内容 .icon58 增加过渡属性 transition: 0.3s; ,这样图标背景变宽时,就会有过渡变化效果。

4、根据图标内容最后变宽的宽度值,给 button 主体定义同样宽度即可,还有让其背景透明 background-color: transparent;

完整代码如下

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 class="btn58">
        <span class="icon58"></span>
        <span class="btn-text58">查看详情</span>
      </button>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.abtn57{
  width: 140px;
  height: 46px;
  list-style: none;
  text-decoration: none;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #457356;
  position: relative;
  cursor: pointer;
  border-radius: 23px;
  overflow: hidden;
}
.btn57,.bg57{
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
  color: #ffffff;
}
.bg57{
  position: absolute;
  text-shadow: 90px 0 #fff, -90px 0 #fff,-180px 0 #fff;
  animation: effx57 1.2s linear infinite;
  display: none;
}
@keyframes effx57{
  to{
    transform: translateX(90px);
  }
}
.abtn57:hover .bg57{
  display: block;
}
.abtn57:hover .btn57{
  display: none;
}

页面渲染效果

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


[1] 原文阅读


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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

设计师工作日常

请我炫个饼🫓

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

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

打赏作者

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

抵扣说明:

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

余额充值