有趣的css - 圆形背景动效多选框

大家好,我是 Just,这里是「设计师工作日常」,今天分享的是用 css 实现一个圆形背景动效多选框,适用提醒用户勾选场景,突出多选框选项,可以有效增加用户识别度。

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


整体效果

💎知识点:
1️⃣ appearance 属性应用
2️⃣ ::before 以及 ::after 伪元素
3️⃣ transform 以及 transition 属性
4️⃣ :hover:active:checked 选择器

🔑思路:
自定义多选框外观,利用伪元素搭建多选框,利用变形属性和过渡属性实现动画效果。

适用提醒用户勾选场景,突出多选框选项,可以有效增加用户识别度。


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

核心代码

html 代码

<label class="label66">
  <input type="checkbox" class="check-inp66">
  <span class="span66"></span>
</label>

label 标签作为多选框主体, input 标签放到 label 中默认绑定,span 标签补充多选框外观元素。

css 部分代码

.label66{
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.check-inp66{
  width: 40px;
  height: 40px;
  border-radius: 50%;
  position: absolute;
  background-color: rgba(0, 0, 0, 0.06);
  z-index: 1;
  transform: scale(1);
  transition: transform 0.15s linear;
  appearance: none; /* none时可以自定义其外观 */
}
.span66{
  width: 20px;
  height: 20px;
  display: block;
  position: absolute;
  z-index: 2;
  cursor: pointer;
}
.span66::before{
  content: '';
  width: 20px;
  height: 20px;
  border: 2px solid rgba(0,0,0,0.6);
  border-radius: 3px;
  box-sizing: border-box;
  display: block;
  position: absolute;
  transition: all 0.3s linear;
}
.span66::after{
  content: "";
  width: 10px;
  height: 5px;
  display: block;
  position: absolute;
  top: 5px;
  left: 4px;
  border: solid 2px transparent;
  border-right: none;
  border-top: none;
  transform: rotate(-45deg);
  transition: border-color 0.3s linear;
}
.label66:hover .span66::before{
  border-color: #0093E9;
}
.label66:active .check-inp66,.label66:active .check-inp66:checked{
  transform: scale(0);
  transition: transform 0.15s linear;
}
.check-inp66:checked{
  background-color: rgba(0,147,233,0.08);
}
.check-inp66:checked ~ .span66::before{
  background-color: #0093E9;
  border-color: #0093E9;
}
.check-inp66:checked ~ .span66::after{
  border-color: #ffffff;
}

1、利用 appearance 属性,定义 appearance: none;,自定义多选框外观,定义圆形浅灰色背景,定义 transformtransition 属性参数。

2、定义 span 样式,利用伪元素 ::before 以及 ::after,分别写出多选框的矩形边框和多选框中的对勾元素;并且给它们分别增加过渡属性参数。

3、利用 :hover 选择器,当鼠标悬浮到多选框上方时,多选框边框实现颜色过渡;利用 :active 选择器,当鼠标左键点击时,多选框的圆形背景实现缩小放大过渡效果;利用 :checked 选择器,当多选框选中时,切换圆形背景颜色,多选框矩形框背景颜色过渡,对勾也过渡显示。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>圆形背景动效多选框</title>
  </head>
  <body>
    <div class="app">
      <label class="label66">
        <input type="checkbox" class="check-inp66">
        <span class="span66"></span>
      </label>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.label66{
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
.check-inp66{
  width: 40px;
  height: 40px;
  border-radius: 50%;
  position: absolute;
  background-color: rgba(0, 0, 0, 0.06);
  z-index: 1;
  transform: scale(1);
  transition: transform 0.15s linear;
  appearance: none; /* none时可以自定义其外观 */
}
.span66{
  width: 20px;
  height: 20px;
  display: block;
  position: absolute;
  z-index: 2;
  cursor: pointer;
}
.span66::before{
  content: '';
  width: 20px;
  height: 20px;
  border: 2px solid rgba(0,0,0,0.6);
  border-radius: 3px;
  box-sizing: border-box;
  display: block;
  position: absolute;
  transition: all 0.3s linear;
}
.span66::after{
  content: "";
  width: 10px;
  height: 5px;
  display: block;
  position: absolute;
  top: 5px;
  left: 4px;
  border: solid 2px transparent;
  border-right: none;
  border-top: none;
  transform: rotate(-45deg);
  transition: border-color 0.3s linear;
}
.label66:hover .span66::before{
  border-color: #0093E9;
}
.label66:active .check-inp66,.label66:active .check-inp66:checked{
  transform: scale(0);
  transition: transform 0.15s linear;
}
.check-inp66:checked{
  background-color: rgba(0,147,233,0.08);
}
.check-inp66:checked ~ .span66::before{
  background-color: #0093E9;
  border-color: #0093E9;
}
.check-inp66:checked ~ .span66::after{
  border-color: #ffffff;
}

页面渲染效果

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


[1] 原文阅读


网站《有趣的css》上线了,目前已上线 demo 实例 65 例,欢迎大家访问。

网站:Funcss

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


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

设计师工作日常

请我炫个饼🫓

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

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

打赏作者

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

抵扣说明:

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

余额充值