.select-label
的 label
在 .pick- select
被选中?
不是只有一个 label
受 input
变化的影响,我希望第二个标签也对输入的变化使用react并为 运行相同的动画code>.pick-select
label
和 .select-label
label
一样。
$(document).ready(function() {
$(".select-all").on("click", function() {
$(this).is(":checked") ?
$(".select-input")
.prop("checked", true)
.change() :
$(".select-input")
.prop("checked", false)
.change();
});
$("input[name='select-check']:checkbox").change(function() {
if ($(this).is(":checked")) {
if ($("input[name='select-check']:checkbox:not(:checked)").length == 0) {
$(".select-all").prop("checked", true);
}
$(this)
.closest(".shrink")
.addClass("active");
} else {
$(".select-all").prop("checked", false);
$(this)
.closest(".shrink")
.removeClass("active");
}
});
});
.pick-select {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 999;
}
.shrink {
height: 50px;
width: 50px;
border: 3px solid;
position: relative;
-webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-webkit-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
}
.shrink.active {
-webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-webkit-transform: scale(0.8);
-ms-transform: scale(0.8);
transform: scale(0.8);
border: 2px solid red;
}
.select-label {
display: inline-block;
cursor: pointer;
position: relative;
}
.select-label span {
display: inline-block;
position: relative;
background-color: transparent;
width: 15px;
height: 15px;
transform-origin: center;
border: 2px solid silver;
border-radius: 50%;
vertical-align: -6px;
margin-right: 10px;
transition: background-color 150ms,
transform 350ms cubic-bezier(0.78, -1.22, 0.17, 1.89);
}
input[name="select-check"] {
display: none;
}
input[name="select-check"]:checked + label span {
background-color: blue;
border-color: blue;
transform: scale(1.25);
}
input[name="select-check"]:checked + label span:after {
width: 10px;
background: #fff;
transition: width 150ms ease 100ms;
}
input[name="select-check"]:checked + label span:before {
width: 5px;
background: #fff;
transition: width 150ms ease 100ms;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selectall-btn">
<input type="checkbox" id="selectall" class="select-all" name="select-check" />
<label class="select-label" for="selectall"><span></span>Select All
</label>
</div>
<div class="post-list">
<div class="item">
<div class="selectall-btn">
<label class="select-label" for="post-select1"><span></span>
</label>
</div>
<div class="shrink">
<div class="select-block">
<label class="pick-select hidden">
<input id="post-select1" type="checkbox" class="select-input" name="select-check">
</label> 1
</div>
</div>
</div>
<div class="item">
<div class="selectall-btn">
<label class="select-label" for="post-select2"><span></span>
</label>
</div>
<div class="shrink">
<div class="select-block">
<label class="pick-select hidden">
<input id="post-select2" type="checkbox" class="select-input" name="select-check">
</label> 2
</div>
</div>
</div>
</div>
最佳答案
在我的方法中,当 checkbox.select-input
被选中
时,最近的 .selectall-btn
被激活( active
添加到它的 class
)。请参阅 CSS 中的 .selectall-btn.active 标签 > span
。
$(document).ready(function() {
$(".select-all").on("click", function() {
$(this).is(":checked") ?
$(".select-input")
.prop("checked", true)
.change() :
$(".select-input")
.prop("checked", false)
.change();
});
$("input[name='select-check']:checkbox").change(function() {
if ($(this).is(":checked")) {
// This didn't work; hence I added `:not(.select-all)` to the selector. The
// `.is()` check should be kept there.
if (! $(this).is('.select-all') &&
$("input[name='select-check']:checkbox:not(:checked):not(.select-all)").length == 0) {
$(".select-all").prop("checked", true);
}
$(this)
.closest(".shrink")
.addClass("active")
// This does what you wanted. (see the CSS code)
.prev('.selectall-btn').addClass('active');
} else {
if (! $(this).is('.select-all')) {
$(".select-all").prop("checked", false);
}
$(this)
.closest(".shrink")
.removeClass("active")
// This does what you wanted. (see the CSS code)
.prev('.selectall-btn').removeClass('active');
}
});
});
.pick-select {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 999;
}
.shrink {
height: 50px;
width: 50px;
border: 3px solid;
position: relative;
-webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-webkit-transform: scale(1);
-ms-transform: scale(1);
transform: scale(1);
}
.shrink.active {
-webkit-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-o-transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
transition: all 600ms cubic-bezier(0.19, 1, 0.22, 1);
-webkit-transform: scale(0.8);
-ms-transform: scale(0.8);
transform: scale(0.8);
border: 2px solid red;
}
.select-label {
display: inline-block;
cursor: pointer;
position: relative;
}
.select-label span {
display: inline-block;
position: relative;
background-color: transparent;
width: 15px;
height: 15px;
transform-origin: center;
border: 2px solid silver;
border-radius: 50%;
vertical-align: -6px;
margin-right: 10px;
transition: background-color 150ms,
transform 350ms cubic-bezier(0.78, -1.22, 0.17, 1.89);
}
input[name="select-check"] {
display: none;
}
input[name="select-check"]:checked + label span,
.selectall-btn.active label > span {
background-color: blue;
border-color: blue;
transform: scale(1.25);
}
input[name="select-check"]:checked + label span:after,
.selectall-btn.active label > span:after {
width: 10px;
background: #fff;
transition: width 150ms ease 100ms;
}
input[name="select-check"]:checked + label span:before,
.selectall-btn.active label > span:before {
width: 5px;
background: #fff;
transition: width 150ms ease 100ms;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="selectall-btn">
<input type="checkbox" id="selectall" class="select-all" name="select-check" />
<label class="select-label" for="selectall"><span></span>Select All
</label>
</div>
<div class="post-list">
<div class="item">
<div class="selectall-btn">
<label class="select-label" for="post-select1"><span></span>
</label>
</div>
<div class="shrink">
<div class="select-block">
<label class="pick-select hidden">
<input id="post-select1" type="checkbox" class="select-input" name="select-check">
</label> 1
</div>
</div>
</div>
<div class="item">
<div class="selectall-btn">
<label class="select-label" for="post-select2"><span></span>
</label>
</div>
<div class="shrink">
<div class="select-block">
<label class="pick-select hidden">
<input id="post-select2" type="checkbox" class="select-input" name="select-check">
</label> 2
</div>
</div>
</div>
</div>
关于javascript - 一个输入的多个标签上的复选框动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50571443/