jQuery 多选按钮美化特效可以通过自定义样式和添加交互效果来实现。以下是一个简单的示例,展示了如何使用 jQuery 和 CSS 来美化多选按钮,并添加一些基本的交互特效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 多选按钮美化</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="checkbox-container">
<label class="checkbox-label">
<input type="checkbox" class="checkbox-input">
<span class="checkbox-custom"></span>
Option 1
</label>
<label class="checkbox-label">
<input type="checkbox" class="checkbox-input">
<span class="checkbox-custom"></span>
Option 2
</label>
<label class="checkbox-label">
<input type="checkbox" class="checkbox-input">
<span class="checkbox-custom"></span>
Option 3
</label>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</body>
</html>
/* styles.css */
.checkbox-container {
display: flex;
flex-direction: column;
}
.checkbox-label {
position: relative;
padding-left: 30px;
cursor: pointer;
margin-bottom: 10px;
}
.checkbox-custom {
position: absolute;
left: 0;
top: 0;
width: 20px;
height: 20px;
border: 2px solid #ccc;
background-color: #fff;
transition: all 0.3s ease;
}
.checkbox-input:checked + .checkbox-custom {
background-color: #007bff;
border-color: #007bff;
}
.checkbox-input:checked + .checkbox-custom::after {
content: '';
position: absolute;
left: 6px;
top: 2px;
width: 6px;
height: 12px;
border: solid #fff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
// script.js
$(document).ready(function() {
$('.checkbox-input').on('change', function() {
if ($(this).is(':checked')) {
$(this).next('.checkbox-custom').addClass('checked');
} else {
$(this).next('.checkbox-custom').removeClass('checked');
}
});
});
label
标签中,这样可以方便点击文本时也能选中复选框。span
元素用于自定义复选框的外观。::after
来创建选中后的勾选标记。change
事件,当复选框被选中或取消选中时,动态添加或移除 checked
类,从而触发 CSS 过渡效果。通过这种方式,你可以轻松地为网页上的多选按钮添加美观的外观和交互效果。
领取专属 10元无门槛券
手把手带您无忧上云