在过去的几天里,我一直致力于创建一些使用伪元素、SVG 动画和其他一些技术的创意复选框。对于一些人来说,我使用了一点 Javascript 来提供更多选项。
对我来说一个很大的烦恼是复选框返回的值是 on 和 off,所以对于这些我创建了一个隐藏的输入,它为一些具有多个选项的复选框返回值 1、0 或介于两者之间的数字. 这意味着您可以使用 class 直接从这个隐藏的输入中访问复选框值.hidden-value
。
同样,我已将复选框的值放在属性.checkbox-container
下的元素上data-value
,为您提供了很大的灵活性。
<script src="https://kit.fontawesome.com/aecf18c460.js" crossorigin="anonymous"></script>
<a class="link" href="https://twitter.com/thisisfjolt" target="_top">Follow on Twitter</a>
<a class="link" href="https://fjolt.com/article/css-checkboxes" target="_top">Read the Article</a>
<h1>The List:</h1>
<div id="colors">
<div data-color="purple" data-main-color="#9100e8" data-box-shadow-tint="#9100e8cc" data-light-highlight="#d7c6ff"></div>
<div data-color="blue" data-main-color="#0074e8" data-box-shadow-tint="#0074e8cc" data-light-highlight="#c6e2ff"></div>
<div data-color="green" data-main-color="#20b951" data-box-shadow-tint="#20b951cc" data-light-highlight="#c6ffea"></div>
<div data-color="red" data-main-color="#de4947" data-box-shadow-tint="#de4947cc" data-light-highlight="#ffc6c6"></div>
<div data-color="orange" data-main-color="#dc7d17" data-box-shadow-tint="#dc7d17cc" data-light-highlight="#ffd6c6"></div>
</div>
<div class="list">
<div class="list-item">
<span class="checkbox-container f1 light" data-value="">
<input type="hidden" id="checkbox-f1-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f1" checked />
<label for="checkbox-f1" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
? The old fashioned
</span>
</div>
<div class="list-item">
<span class="checkbox-container f10 light" data-value="">
<input type="hidden" id="checkbox-f10-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f10" checked />
<svg id="svgElement" width="3em" height="3em" viewBox="115 140 70 70">
<path fill="none" stroke-linecap="round" class="path" stroke="#000" stroke-width="4" d="M173 196 L172 196.33333333333334 L169.8 197.2 L164.71428571428572 197.71428571428572 L161.5 197.875 L156.125 198 L150 197.625 L143.5 196.75 L136.75 195.125 L130.875 192.75 L125.75 189.875 L121.875 186.25 L119.125 181.875 L118.125 176.875 L118.625 171.875 L120.375 167 L123.25 162.5 L127.125 158.625 L131.875 155.25 L137.5 152.625 L143.5 151.125 L149.5 151.125 L155.5 152.375 L161.25 154.5 L166.5 157.5 L171 161.125 L174.75 165.375 L177.625 170.25 L180 175.125 L181.375 179.625 L181.875 183.625 L181.5 187.25 L180.75 190.375 L179.5 193 L178 195 L176.375 196.375 L174.75 197.375 L173.375 198.125 L172 198.83333333333334 L171 199 L170.5 199"></path>
</svg>
<label for="checkbox-f10" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
Circle with pencil
</span>
</div>
<div class="list-item">
<span class="checkbox-container f2 light" data-value="">
<input type="hidden" id="checkbox-f2-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f2" checked />
<label for="checkbox-f2" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
The Slider
</span>
</div>
<div class="list-item">
<span class="checkbox-container f3 light" data-value="">
<input type="hidden" id="checkbox-f3-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f3" />
<span class="halfway alt-selection" data-value="0.5"></span>
<label for="checkbox-f3" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
I'm only halfway done
</span>
</div>
<div class="list-item dark">
<span class="checkbox-container f4 light" data-value="">
<input type="hidden" id="checkbox-f4-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f4" />
<label for="checkbox-f4" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
☄️ Slide a little more
</span>
</div>
<div class="list-item">
<span class="checkbox-container f5 light" data-value="">
<input type="hidden" id="checkbox-f5-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f5" checked />
<label for="checkbox-f5" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
⭕️ The circle
</span>
</div>
<div class="list-item">
<span class="checkbox-container f6 light" data-value="">
<input type="hidden" id="checkbox-f6-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f6" checked />
<label for="checkbox-f6" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
? The target
</span>
</div>
<div class="list-item">
<span class="checkbox-container f7 light" data-value="">
<input type="hidden" id="checkbox-f7-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f7" />
<span class="noway alt-selection" data-value="0"></span>
<span class="thirdway alt-selection" data-value="0.33"></span>
<span class="threethirdway alt-selection" data-value="0.66"></span>
<span class="fullway alt-selection" data-value="1"></span>
<label for="checkbox-f7" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
4 Options, for 4 things
</span>
</div>
<div class="list-item">
<span class="checkbox-container f8 light" data-value="">
<input type="hidden" id="checkbox-f8-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f8" />
<label for="checkbox-f8" class="pseudo-checkbox">
I Agree!
</label>
</span>
<span class="list-item-text">
?? The "I Agree to Terms and Conditions"
</span>
</div>
<div class="list-item">
<span class="checkbox-container f9 light" data-value="">
<input type="hidden" id="checkbox-f9-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f9" checked />
<label for="checkbox-f9" class="pseudo-checkbox">
I Agree!
</label>
</span>
<span class="list-item-text">
? I Agree but differently
</span>
</div>
<div class="list-item">
<span class="checkbox-container f11 light" data-value="">
<input type="hidden" id="checkbox-f11-value" class="hidden-value" checked />
<input type="checkbox" id="checkbox-f11" checked />
<svg id="svgElement1" width="1em" height="1em" viewBox="440 170 70 70">
<path fill="none" stroke-linecap="round" class="path" stroke="#fff" stroke-width="7" d="M452 202 L452 202.33333333333334 L452 204.2 L452.14285714285717 206.28571428571428 L452.25 207.375 L452.625 209.375 L453 211.625 L453.5 214 L454 216 L454.625 218.25 L455.25 220.25 L455.75 222 L456.375 223.625 L456.75 224.875 L457.25 226 L457.625 227 L458 227.875 L458.375 228.5 L458.75 229.125 L459.125 229.625 L459.375 230 L459.75 230.375 L460 230.75 L460.25 231 L460.5 231.25 L460.625 231.5 L460.875 231.5 L461.125 231.375 L461.5 231 L461.875 230.125 L462.375 228.875 L463 227.125 L464.125 224.625 L465.625 221.5 L467.125 218.25 L469.125 214.375 L471.25 210.375 L473.625 206.375 L476.125 202.125 L478.75 198 L481 194.25 L483 191 L485 187.75 L486.625 185.125 L488.125 182.875 L489.25 181 L490.125 179.625 L490.875 178.625 L491.6666666666667 177.66666666666666 L492.25 177.25 L492.5 177"></path>
</svg>
<label for="checkbox-f11" class="pseudo-checkbox"></label>
</span>
<span class="list-item-text">
?♂️ The handwritten tick
</span>
</div>
</div>
:root {
--main-color: #0074e8;
--box-shadow-tint: #0074e8cc;
--light-highlight: #c6e2ff;
}
html {
font-size: 24px;
}
body {
margin: 2em;
font-family: 'Source Sans Pro', sans-serif;
}
#colors {
position: fixed;
right: 2em;
top: 7em;
width: 5em;
}
#colors > div {
width: 1em;
height: 1em;
float: right;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin: 0 0em 1em 0.5em;
cursor: pointer;
border-radius: 100px;
transition: all 0.1s ease-out;
}
#colors > div:hover {
box-shadow: inset 0 0 0 100px rgba(0,0,0,0.2);
}
[data-color="purple"] {background: #9100e8;}
[data-color="blue"] { background: #0074e8; }
[data-color="green"] {background: #20b951;}
[data-color="red"] {background: #de4947;}
[data-color="orange"] {background: #dc7d17;}
a.link {
font-size: 0.75em;
background: var(--main-color);
padding: 0.5em 1em;
border-radius: 100px;
transition: all 0.1s ease-out;
color: white;
position: fixed;
top: 2em;
right: 2.5em;
text-decoration: none;
display: block;
}
a.link + .link {
top: 4rem;
}
a.link:hover {
box-shadow: inset 0 0 0 100px rgba(0,0,0,0.3);
}
[type="checkbox"] {
position: absolute;
height: 100%;
width: 100%;
margin: 0;
top: 0;
right: 999999px;
padding: 0;
}
h1 { margin: 0; }
.checkbox-container {
display: inline-block;
position: relative;
}
.list { padding: 1em 0; }
.list-item {
height: 1.5em;
border-bottom: 1px solid rgb(52 69 69 / 8%);
padding: 0.75em 0;
white-space: nowrap;
}
.list-item-text {
height: 1.5em;
display: inline-block;
vertical-align: top;
font-size: 1.1167em;
line-height: 1.5em;
margin: 0 1em;
color: #333;
font-weight: 600;
}
.dark {
background: #262b31;
border-bottom: none;
padding: 1em;
width: calc(100% + 5em);
height: 3.5em;
box-sizing: border-box;
margin: 0 0 0 -3em;
padding: 1em 3em;
}
.dark .list-item-text {
color: white;
}
/* Some default values */
.pseudo-checkbox {
width: 1.5em;
height: 1.5em;
position: relative;
border-radius: 6px;
display: block;
background: white;
}
/* F1 */
.f1 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
border-radius: 6px;
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f1 input[type="checkbox"]:checked + *:after {
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
content: '\f00c';
text-align: center;
width: 100%;
height: 100%;
position: absolute;
font-weight: 600;
color: white;
pointer-events: none;
font-weight: 600;
top: 0;
z-index: 999;
left: 0;
}
.f1 input[type="checkbox"]:focus + *,
.f1:hover [type="checkbox"] + * { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f1 [type="checkbox"]:checked + * {
background: var(--main-color);
color: white;
box-shadow: 0 0 0 1px var(--main-color);
}
/* F2 */
.f2 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
border-radius: 100px;
cursor: pointer;
width: 3em;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f2 input + .pseudo-checkbox:after {
content: '';
width: calc(1.5em - 4px);
border-radius: 100px;
height: calc(1.5em - 4px);
position: absolute;
top: 2px;
left: 2px;
transition: all 0.15s ease-out;
background: #bbbbbb;
}
.f2 input:checked + .pseudo-checkbox:after {
left: calc(100% - 1.5em + 2px);
content: '';
background: white;
box-shadow: 0 2px 2px rgba(0,0,0,0.2);
}
.f2 input[type="checkbox"]:focus + *,
.f2:hover [type="checkbox"] + * { /* For focusing */
box-shadow: #0000003d 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f2 [type="checkbox"]:checked + *:after {
content: '\f00c';
}
.f2 [type="checkbox"]:checked + * {
background: var(--main-color);
color: white;
box-shadow: 0 0 0 1px var(--main-color);
}
/* F3 */
.f3 .pseudo-checkbox {
background: white;
border-radius: 6px;
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f3 [type="checkbox"]:focus + span + label, .f3 [type="checkbox"]:hover + span + label { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: var(--light-highlight);
}
.f3[data-value="1"] label {
background: var(--main-color);
color: white;
box-shadow: 0 0 0 1px var(--main-color);
}
.f3[data-value="1"] input[type="checkbox"] + span + .pseudo-checkbox {
background: var(--main-color);
box-shadow: 0 0 0 2px var(--main-color)
}
.f3 .halfway {
position: absolute;
top: 0;
box-shadow: rgba(52, 69, 69, 0.16) 1px 0px 0px 0px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
left: 0;
width: 50%;
height: 100%;
transition: all 0.1s ease-out;
z-index: 9;
border-radius: 6px 0 0 6px;
cursor: pointer;
}
.f3 .halfway.active {
background: var(--main-color);
box-shadow: 0 0 0 1px var(--main-color);
}
.f3 .halfway.active:hover {
background: var(--main-color);
}
.f3 .halfway:hover {
background: var(--light-highlight);
box-shadow: rgba(0,0,0,0.2) 1px 0px 0px 0px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f3 .halfway:hover + * {
background: white;
}
.f3 [type="checkbox"]:focus + .halfway.active,
.f3:hover [type="checkbox"] + .halfway.active { /* For focusing */
box-shadow: 1px 0 0 0 var(--main-color);
}
.f3[data-value="1"]:after {
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
content: '\f00c';
text-align: center;
width: 100%;
height: 100%;
position: absolute;
background: var(--main-color);
color: white;
border-radius: 6px;
font-weight: 600;
pointer-events: none;
top: 0;
z-index: 999;
left: 0;
}
/* F4 */
.f4 {
width: 3em;
}
.f4 .pseudo-checkbox {
cursor: pointer;
transition: all 0.1s ease-out;
}
.f4 input[type="checkbox"] + *:after {
transition: all 0.1s ease-out;
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
content: '';
text-align: center;
width: 1em;
height: 1em;
position: absolute;
color: white;
pointer-events: none;
top: 0.25em;
font-weight: 600;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
z-index: -1;
border-radius: 100px;
left: -1px;
background: white;
}
.f4 [type="checkbox"] + * {
z-index: 99999;
border-radius: 100px;
background: transparent;
color: white;
width: 100%;
}
.f4 input:checked + .pseudo-checkbox:after {
left: calc(100% - 1em + 2px);
box-shadow: #0074e600 0px 2px 2px 0px, var(--main-color) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: var(--main-color);
}
.f4 input + .pseudo-checkbox:before, .f4:after {
content: '';
width: 100%;
border-radius: 100px;
z-index: -1;
height: 6px;
position: absolute;
top: calc(50% - 3px);
border-radius: 100px;
left: 0;
transition: all 0.15s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: white;
}
.f4:after {
background: var(--main-color);
width: 0px;
z-index: 99999;
box-shadow: none;
pointer-events: none;
display: block;
}
.f4[data-value="1"]:after {
width: 100%;
}
.f4 input[type="checkbox"]:focus + .pseudo-checkbox:after, .f4:hover input[type="checkbox"] + *:after { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: var(--main-color);
}
/* F5 */
.f5 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
border-radius: 100px;
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f5 input[type="checkbox"] + *:after {
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
content: '\f00c';
text-align: center;
width: 100%;
height: 100%;
position: absolute;
color: white;
pointer-events: none;
top: 0;
border-radius: 100px;
font-weight: 600;
background: var(--main-color);
color: white;
box-shadow: 0 0 0 1px var(--main-color);
clip-path: circle(0% at 0.25em 0.25em);
z-index: 999;
transform: scale(1);
transition: all 0.08s cubic-bezier(0, 1.13, 0.93, 0.82);
left: 0;
}
.f5 [type="checkbox"]:focus + *,
.f5:hover [type="checkbox"] + * { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: linear-gradient(0deg, #f3f9ff, #f4faff);
}
.f5 [type="checkbox"]:checked + *:after {
clip-path: circle(100% at 0.25em 0.25em);
}
/* F6 */
.f6 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
border-radius: 100px;
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f6 [type="checkbox"]:checked + label {
background: var(--main-color);
box-shadow: none;
}
.f6 input[type="checkbox"] + *:after, .f6 input[type="checkbox"] + *:before {
line-height: 1.5em;
content: '';
text-align: center;
width: 70%;
pointer-events: none;
height: 70%;
position: absolute;
pointer-events: none;
box-shadow: 0 2px 2px rgba(0,0,0,0.2);
top: 15%;
transform: scale(0.001);
border-radius: 100px;
background: white;
z-index: 999;
transition: all 0.08s cubic-bezier(0, 1.13, 0.93, 0.82);
left: 15%;
}
.f6 input[type="checkbox"] + *:before {
width: 40%;
height: 40%;
top: 30%;
left: 30%;
z-index: 9999;
background: var(--main-color);
box-shadow: none;
}
.f6 input[type="checkbox"]:checked + *:after, .f6 input[type="checkbox"]:checked + *:before {
transform: scale(1);
}
.f6 > input[type="checkbox"]:focus + *, .f6:hover [type="checkbox"] + * { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
/* F7 */
.f7 {
width: 4em;
cursor: pointer;
}
.f7 .noway, .f7 .fullway, .f7 .thirdway, .f7 .threethirdway {
content: '';
width: 0.75em;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
position: absolute;
top: calc(50% - 0.3525em);
left: -3px;
z-index: 99;
border-radius: 100px;
height: 0.75em;
background: white;
}
.f7 .fullway, .f7 .thirdway, .f7 .threethirdway {
right: -3px;
left: auto;
z-index: 999;
cursor: pointer;
}
.f7 .thirdway {left: calc(33.3% - 0.2em);}
.f7 .threethirdway {left: calc(66.6% - 0.4em);}
.f7 .pseudo-checkbox {
width: inherit;
cursor: pointer;
pointer-events: none;
}
.f7[data-value="0"] .noway,
.f7[data-value="1"] .noway, .f7[data-value="1"] .fullway,
.f7[data-value="1"] .thirdway, .f7[data-value="1"] .threethirdway,
.f7[data-value="0.33"] .noway, .f7[data-value="0.33"] .thirdway,
.f7[data-value="0.66"] .noway, .f7[data-value="0.66"] .thirdway,
.f7[data-value="0.66"] .threethirdway {
background: var(--main-color);
box-shadow: var(--main-color) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px, 0 3px 2px rgba(0,0,0,0.15);
}
.f7 input[type="checkbox"] ~ label:after {
width: inherit;
content: '';
height: 6px;
border-radius: 100px;
top: calc(50% - 3px);
background: white;
position: absolute;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f7 input[type="checkbox"]:focus ~ label:after { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: linear-gradient(0deg, #f3f9ff, #f4faff);
}
/* F8 */
.f8 label {
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
float: left;
width: auto;
border-radius: 6px;
}
.f8 label {
float: left;
/* font-weight: 600; */
cursor: pointer;
padding: 0 0.75em;
color: #848484;
user-select: none;
font-size: 0.75em;
line-height: 1.5em;
width: auto;
height: auto;
}
.f8 input[type="checkbox"] + *:after {
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
transition: height 0.08s cubic-bezier(0, 1.13, 0.93, 0.82), top 0.08s cubic-bezier(0, 1.13, 0.93, 0.82);
content: '\f00c';
text-align: center;
width: 100%;
height: 100%;
height: 0;
position: absolute;
overflow: hidden;
font-weight: 600;
opacity: 0;
color: white;
pointer-events: none;
z-index: 999;
border-radius: 6px;
left: 0;
background: var(--main-color);
top: 100%;
color: white;
box-shadow: 0 0 0 1px var(--main-color);
}
.f8 input[type="checkbox"]:checked + *:after {
top: 0;
opacity: 1;
height: 100%;
}
.f8 [type="checkbox"]:focus + *, .f8:hover [type="checkbox"] + * { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
/* F9 */
.f9 label {
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
float: left;
width: auto;
border-radius: 6px;
}
.f9 label {
float: left;
cursor: pointer;
padding: 0 0.75em 0 2.25em;
color: #848484;
user-select: none;
font-size: 0.75em;
line-height: 1.5em;
width: auto;
height: auto;
}
.f9 input[type="checkbox"] + *:after {
font-family: 'Font Awesome 5 Free';
line-height: 1.5em;
transition: height 0.08s cubic-bezier(0, 1.13, 0.93, 0.82), top 0.08s cubic-bezier(0, 1.13, 0.93, 0.82);
content: '\f00d';
text-align: center;
width: 100%;
height: 100%;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
position: absolute;
overflow: hidden;
pointer-events: none;
z-index: 999;
border-radius: 6px 0 0 6px;
font-weight: 600;
left: 0;
width: 1.5em;
top: 0;
}
.f9 input[type="checkbox"]:checked + *:after {
background: var(--main-color);
box-shadow: 0 0 0 1px var(--main-color);
color: white;
content: '\f00c';
}
.f9 [type="checkbox"]:focus + *, .f9:hover [type="checkbox"] + * { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
/* F10 */
.f10 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
border-radius: 100px;
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f10 input[type="checkbox"]:checked ~ label:after {
font-family: 'Font Awesome 5 Free';
line-height: 2.5em;
content: '\f00c';
text-align: center;
width: 100%;
height: 100%;
font-size: 0.633em;
background: var(--main-color);
position: absolute;
clip-path: circle(40%);
border-radius: 100px;
font-weight: 600;
color: white;
pointer-events: none;
top: 0;
z-index: 999;
left: 0;
}
.f10 input[type="checkbox"]:focus ~ label, .f10:hover [type="checkbox"] ~ label { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: linear-gradient(0deg, #f3f9ff, #f4faff);
}
.f10 [type="checkbox"]:checked ~ label {
color: white;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f10 svg {
position: absolute;
top: -0.65em;
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
left: -0.5em;
z-index: 9999;
pointer-events: none;
}
.f10 [type="checkbox"]:checked ~ svg {
animation: dash 2s linear forwards;
}
/* F11 */
.f11 .pseudo-checkbox {
background: linear-gradient(0deg, #f6fbff, white);
cursor: pointer;
transition: all 0.1s ease-out;
box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 1px 0px, rgba(52, 69, 69, 0.16) 0px 0px 0px 1px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
}
.f11 input[type="checkbox"]:checked ~ label:after {
font-family: 'Font Awesome 5 Free';
line-height: 2.5em;
content: '';
text-align: center;
width: 100%;
height: 100%;
font-size: 0.633em;
background: var(--main-color);
position: absolute;
border-radius: 6px;
box-shadow: none;
font-weight: 600;
color: white;
pointer-events: none;
top: 0;
z-index: 999;
left: 0;
}
.f11 input[type="checkbox"]:focus ~ label, .f11:hover [type="checkbox"] ~ label { /* For focusing */
box-shadow: rgba(0,0,0,0.1) 0px 2px 2px 0px, var(--box-shadow-tint) 0px 0px 0px 2px, rgba(60, 66, 87, 0.08) 0px 2px 5px 0px;
background: linear-gradient(0deg, #f3f9ff, #f4faff);
}
.f11 [type="checkbox"]:checked ~ label {
color: white;
box-shadow: none;
}
.f11 svg {
position: absolute;
top: 0.25em;
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
transform: scale3d(1.3, 1, 1);
left: 0.25em;
z-index: 9999;
pointer-events: none;
}
.f11 [type="checkbox"]:checked ~ svg {
animation: dash 2s linear forwards;
}
@keyframes dash {
to {
stroke-dashoffset: 0;
}
}
const parent = function(el, match, last) {
var result = [];
for (var p = el && el.parentElement; p; p = p.parentElement) {
result.push(p);
if(p.matches(match)) {
break;
}
}
if(last == 1) {
return result[result.length - 1];
} else {
return result;
}
};
const alternativeSelections = function(item) {
let parentEl = parent(item, '.checkbox-container', true);
let value = item.getAttribute('data-value');
if(item.classList.contains('active')) {
item.classList.remove('active');
parentEl.querySelector('input[type="checkbox"]').value = 'off';
} else {
item.classList.add('active');
parentEl.querySelector('input[type="checkbox"]').value = 'on';
}
parentEl.setAttribute('data-value', value);
parentEl.querySelector('input[type="hidden"]').value = value;
}
document.addEventListener("DOMContentLoaded", function(e) {
document.querySelectorAll('.alt-selection').forEach(function(item) {
item.addEventListener('click', function(e) {
alternativeSelections(this);
});
});
document.querySelectorAll('input[type="checkbox"]').forEach(function(item) {
let parentEl = parent(item, '.checkbox-container', true);
if(item.checked) {
parentEl.setAttribute('data-value', 1);
parentEl.querySelector('input[type="hidden"]').value = 1;
} else {
parentEl.setAttribute('data-value', 0);
parentEl.querySelector('input[type="hidden"]').value = 0;
}
item.addEventListener('change', function(e) {
let value = 0;
let getValue = parseFloat(parentEl.getAttribute('data-value'));
if(this.checked) {
value = 1;
if(parentEl.querySelector('.alt-selection') !== null) {
parentEl.querySelector('.alt-selection').classList.remove('active');
}
}
parentEl.setAttribute('data-value', value);
parentEl.querySelector('input[type="hidden"]').value = value;
});
});
document.querySelectorAll('#colors > div').forEach(function(item) {
item.addEventListener('click', function(e) {
let mainColor = item.getAttribute('data-main-color');
let boxShadow = item.getAttribute('data-box-shadow-tint');
let highlight = item.getAttribute('data-light-highlight');
console.log(mainColor);
document.documentElement.style.setProperty("--main-color", mainColor)
document.documentElement.style.setProperty("--box-shadow-tint", boxShadow)
document.documentElement.style.setProperty("--light-highlight", highlight)
});
})
});
CSS 中的自定义复选框#
复选框和输入表单是我们用来从用户那里收集信息的各种屏幕之一。与这些的用户交互通常很无聊,但它们并非必须如此。对于依赖复选框的列表应用和项目,复选框的显示方式非常重要。这里的复选框涵盖了涉及复选框的主要用例。
适应性
对于您在 Web 上找到的更复杂的复选框,它们通常带有 CSS 类,例如.big
或.medium
。随着当今网站提供的各种屏幕,这实际上可能是一个障碍,而不是特别有用。
为了避免这种情况,我用em
单位制作了所有这些复选框。这意味着您的复选框可以是您喜欢的任何大小,您只需更改父元素上的字体大小。在这些复选框的文件中,这可能意味着更改.list-item
. 我已经在 HTML 标记上完成了它,但可以随意更改它。这意味着您可以轻松地为移动设备调整它们。举个简单的例子:
@media screen and (max-width: 900px) {
html {
font-size: 30px; /* Very big buttons */
}
}
这样就不必依赖特定的 CSS 类而大惊小怪,并使一切都相对简单。
SVG
我为其中一些复选框所做的事情是使用 SVG 路径并为其设置动画。令我惊讶的是我不需要任何 Javascript,所以我将在另一个教程中介绍如何创建我在这里得到的手绘 SVG 效果。敬请关注。
Javascript
尽管这些复选框中的大多数在没有 Javascript 的情况下都可以正常工作,但我添加了一些以获得一些额外的功能。例如,某些复选框有多个选项。例如,一个人可能有 4 个设置。在这些情况下,我想设置自定义值——而不是 1 或 0,1/3 可能会产生 0.33。当然,这对于纯 CSS 是不可能的,但它只适用于少数复选框。
其次,我想更新隐藏输入的值,正如我在第一段中解释的那样。用 Javascript 做这件事是一件相对轻而易举的事。
可用性和可访问性
任何设计的关键部分是可用性和可访问性。浏览页面的能力很有用,不仅对某些无障碍程序,而且对我来说,有一次我把咖啡洒在我的键盘上,而 Tab 键是唯一有效的。
所以对于这些复选框,我添加了这个,不是完全隐藏输入,而是将其移出屏幕。这意味着输入仍然是可聚焦的,当聚焦时我们会更新它旁边的标签或元素。这允许我们通过标签,并为我们的自定义复选框提供焦点事件:
input[type="checkbox"] {
position: absolute;
right: 999999px;
}
input[type="checkbox"]:focus + label {
/* Our focus CSS goes here */
}
CSS 变量
我在这里使用的一件大事也是 CSS 变量。所有与复选框相关的颜色都由 CSS 变量定义。这意味着您可以更新 CSS 变量颜色,也可以更新每个复选框的颜色。这非常有用 – 无需单独更改复选框的外观,我们可以简单地更新一些关键变量。我在这里介绍了如何使用 Javascript 更新 CSS 变量。
结论
总之,我希望这些效果在您未来的一个项目中证明是有用的。我已将演示和代码放在 CodePen 上,代码可从下面的 Git Repo 获得。享受!
- CodePen 演示
- GitHub Repo