javascript - 一个输入的多个标签上的复选框动画

标签 javascript jquery html

.select-labellabel.pick- select 被选中?

不是只有一个 labelinput 变化的影响,我希望第二个标签也对输入的变化使用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/

相关文章:

javascript - 为什么 setCustomValidity() 在 React 应用程序中不起作用?

javascript - jquery ui css 和image 文件在不同的文件夹中,如何配置?

javascript - 双击焦点输入?

javascript - 在javascript html中访问列表中的元素

javascript - TinyMCE:保存内容时我的 HTML 发生了变化。我怎样才能保留我的 HTML?

javascript - RaphaelJS Set.forEach()

javascript - 无法从 toDataURL( ) 获取图像数据,出现错误 SECURITY_ERR : DOM Exception 18

javascript - 链接可以在浏览器中使用并提供 JSON,但不能在 Angular 中使用

javascript - 在通过 cPanel 安装 Magento 期间未定义 $

javascript - 仅当 <select> 标签存在时,如何获取 <td> 内 <select> 的值?