切换函数在transitionend

时间:2018-02-01 07:09:34

标签: javascript jquery html css transitionend

我有一个切换,其checked状态我用jQuery调用。每次单击未选中的切换到选中时,控制台都会记录"do this!"两次。为什么这个函数会触发两次?解决这个问题的方法是什么?

$("#my-toggle").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function() {
  if (document.getElementById('my-checkbox').checked) {
    console.log("do this!")
  }
});
.switch {
  position: absolute;
  top: 15%;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  display: none;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before,
.slider .number {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider .number {
  background: none;
  font-size: 14px;
  left: 9px;
  top: 9px;
}

input:checked+.slider {
  background-color: #2196F3;
}

input:focus+.slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+.slider:before,
input:checked+.slider .number {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<label class="switch">
  <input type="checkbox" id="my-checkbox">
  <span class="slider round" id="my-toggle"></span>
</label>

我的最终HTML看起来像这样,事件会触发三次:

<label class="switch">
  <input type="checkbox" id="my-checkbox">
  <span class="slider round" id="my-toggle">
    <span class="number">00</span>
  </span>
</label>

3 个答案:

答案 0 :(得分:2)

有两个#my-toggle个事件被触发:一个来自<span> ::before,一个来自e伪元素。您需要区分这两个事件。你需要事件参数e.originalEvent.propertyName

然后在e.originalEvent.pseudoElement(对应相应的CSS属性)和if中找到唯一的区别。

因此,让我们检查&& e.target == this条件中的最后一个属性。

同时在transitionend event is also firing for children中加入另一个<span>,因为事件会冒泡。由于您只是在父e.stopPropagation上收听该事件,因此您不能简单地调用$("#my-toggle").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(e) { if (document.getElementById("my-checkbox").checked && !e.originalEvent.pseudoElement && e.target == this) { console.log("do this!"); } });,这只会阻止的触发事件。

&#13;
&#13;
.switch {
  position: absolute;
  top: 15%;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before,
.slider .number {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}
.slider .number {
  background: none;
  font-size: 14px;
  left: 9px;
    top: 9px;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before,
input:checked + .slider .number {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
  <input type="checkbox" id = "my-checkbox">
  <span class="slider round" id = "my-toggle">
     <span class="number">00</span>
  </span>
</label>
&#13;
var doc = new jsPDF();
var html2canvasOpts =  { 
        background: '#ffffff'
    }
doc.addHTML($('#mytargetelement')[0], html2canvasOpts, function () {
    doc.save('Test.pdf');
});
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这来自过渡所关注的要素。 如果您放置console.log(event)(将其作为事件处理程序的参数),您将看到相关属性为background-colortrandform。 这两个属性与转换有关,因此每个属性都会调用一次事件hendler。

这是使用&#34;过滤&#34;的片段。在transform财产:

&#13;
&#13;
$("#my-toggle").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(event){
  if (event.originalEvent.propertyName === "transform" && document.getElementById('my-checkbox').checked){
    console.log("do this!");
  }
 });
&#13;
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {display:none;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="switch">
  <input type="checkbox" id="my-checkbox">
  <span class="slider round" id="my-toggle"></span>
</label>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

寻找点击功能时,使用.slider而不是.switch,这对我有用,我不知道为什么。

$('.slider').on('click',function(){


alert("Test");
});