پیشنمایش زنده
کد HTML
<div class="amazing-checkbox">
<input id="super-check" type="checkbox" />
<label for="super-check">
<span class="checkbox-icon">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
class="checkmark"
>
<path
fill="white"
d="M6.173 11.421l-2.626-2.6 1.108-1.108 1.518 1.5 3.985-3.98 1.108 1.108z"
></path>
</svg>
</span>
</label>
</div>
کد CSS
.amazing-checkbox {
display: inline-block;
}
/* Skjul standard checkbox */
.amazing-checkbox input[type="checkbox"] {
display: none;
}
/* Label for checkbox */
.amazing-checkbox label {
cursor: pointer;
display: inline-flex;
align-items: center;
position: relative;
}
/* Checkbox-ikonet */
.amazing-checkbox .checkbox-icon {
width: 2em;
height: 2em;
border: 0.2em solid #ccc;
border-radius: 0.3em;
display: inline-block;
position: relative;
transition:
background 0.3s,
border-color 0.3s,
transform 0.3s;
box-shadow: 0 0.1em 0.3em rgba(0, 0, 0, 0.1);
overflow: hidden; /* Viktig for ripple-effekten */
}
/* Ripple-effekt med pseudo-element */
.amazing-checkbox .checkbox-icon::before {
content: "";
position: absolute;
left: 50%;
top: 50%;
width: 0;
height: 0;
background: rgba(114, 137, 218, 0.3);
border-radius: 50%;
transform: translate(-50%, -50%);
}
/* Hover-effekt */
.amazing-checkbox label:hover .checkbox-icon {
border-color: #7289da;
}
/* Fokus-effekt */
.amazing-checkbox label:focus-within .checkbox-icon {
box-shadow: 0 0 0.3em rgba(114, 137, 218, 0.5);
}
/* Standardstil for checkmark (alltid til stede, men usynlig) */
.amazing-checkbox .checkbox-icon .checkmark {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(0);
width: 1em;
height: 1em;
opacity: 0;
pointer-events: none;
}
/* Når checkboxen er krysset av */
.amazing-checkbox input[type="checkbox"]:checked + label .checkbox-icon {
background: linear-gradient(45deg, #7289da, #99aab5);
border-color: transparent;
animation: pop 0.3s ease-out;
}
/* Vis checkmark med animasjon */
.amazing-checkbox
input[type="checkbox"]:checked
+ label
.checkbox-icon
.checkmark {
animation: checkmarkIn 0.3s forwards;
}
/* Ripple-animasjon ved avkrysning */
.amazing-checkbox
input[type="checkbox"]:checked
+ label
.checkbox-icon::before {
animation: ripple 0.6s ease-out;
}
/* Når checkboxen fjernes – animer checkmark ut */
.amazing-checkbox
input[type="checkbox"]:not(:checked)
+ label
.checkbox-icon
.checkmark {
animation: checkmarkOut 0.3s forwards;
}
/* Keyframes for pop-effekt */
@keyframes pop {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1.1);
}
}
/* Keyframes for checkmark inn-animasjon */
@keyframes checkmarkIn {
0% {
transform: translate(-50%, -50%) scale(0);
opacity: 0;
}
80% {
transform: translate(-50%, -50%) scale(1.2);
opacity: 1;
}
100% {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
}
/* Keyframes for checkmark ut-animasjon */
@keyframes checkmarkOut {
0% {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
100% {
transform: translate(-50%, -50%) scale(0);
opacity: 0;
}
}
/* Keyframes for ripple-effekten */
@keyframes ripple {
0% {
width: 0;
height: 0;
opacity: 0.7;
}
100% {
width: 4em;
height: 4em;
opacity: 0;
}
}