【发布时间】:2020-02-20 15:14:43
【问题描述】:
我想自定义浏览器复选框。选中状态时,应显示图标 checked.svg 而不是复选框,否则应显示图标 unchecked.svg。
这是我的代码。 HTML:
<div class="checkbox-custom">
<input type="checkbox"/>
<div class="checkmark">
<img src="@/assets/images/icons/checkbox/unchecked.svg" class="unchecked"/>
<img src="@/assets/images/icons/checkbox/checked.svg" class="checked"/>
</div>
</div>
SASS:
.checkbox-custom {
position: absolute;
width: 28px;
height: 28px;
left: 0;
top: 0;
// Hide default browser checkbox
input[type=checkbox] {
display: none;
}
input[type=checkbox] + .checkmark {
position: absolute;
left: 3.5px;
top: 3.5px;
right: 3.5px;
bottom: 3.5px;
display: inline-block;
cursor: pointer;
img {
width: 21px;
height: 21px;
}
.checked {
display: none;
}
}
input[type=checkbox]:checked + .checkmark {
.checked {
display: block;
}
.unchecked {
display: none;
}
}
}
当我点击复选框时,没有任何反应。可能是什么错误?
【问题讨论】:
-
你永远不会点击复选框,因为它是隐藏的。您需要为该复选框制作图像标签,以便它们触发选中/取消选中。