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

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


目录

整体效果

有趣的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>
  • 1.
  • 2.
  • 3.
  • 4.

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.

页面渲染效果

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

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


网站《有趣的css》上线了,欢迎大家访问~ 点击访问: Funcss

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


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