尝试使用过渡性事件而不是Settimeout



我有此代码:

$("#disable").click(function () {
     $("body").append("<div id='blackDisable' class='fade'> </div>");
    setTimeout(function () {
        $("#blackDisable").addClass("showBack");
    }, 150);
});
.fade {
    opacity: 0;
    transition: opacity .15s linear;
}
        
.showBack {
    opacity: .5;
}
#blackDisable {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 2222;
    background-color: black;
}
<button id="disable">Click Me</button>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

我不想使用setTimeout强制过渡工作,因此我搜索了解决方案并找到了transitionend事件,并用setTimeout替换了CC_3事件处理程序,但没有开火。

如何删除此setTimeout并使用transitionend事件?

提前感谢,对不起,我的英语!

transitionend与您的问题有任何关系 - 因为没有过渡以注册任何 end
这是将类立即分配给新创建的元素(尚未发现dom-styles-dercover(元素的问题 - without a callback - 其中setTimeout实际上像" Dom-Ready hack hack" "。

没有setTimeout?两种方式(甚至更多(:

1。animation(而不是transition(

$("#disable").click(function() {
  $("body").append("<div id='blackDisable' class='fade'></div>");
  $("#blackDisable").addClass("showBack");
  
});
.fade {
  opacity: 0;
  /* no transition */
}
.showBack {
  /*opacity: .5; not ready, instead let the browser assign/trigger keyframes */
  animation : fadeIn 0.3s linear forwards;
}
#blackDisable {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 2222;
  background-color: black;
}
@keyframes fadeIn {
  to { opacity: 0.5; }
}
<button id="disable">Click Me</button>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

2。在任何事件之前附加

并使用CSS visibility防止重叠问题

// Append on DOM ready
$("body").append("<div id='blackDisable' class='fade'></div>");
// CALLBACK
$("#disable").click(function() {
  // Since the callback, it's now discoverable,
  // DOM set, CSS styles are set and we can transition! 
  $("#blackDisable").addClass("showBack");
});
.fade {
  opacity: 0;
  visibility: hidden; /* you need this!!!!!! */
  transition: opacity .3s linear;
}
.showBack {
  opacity: .5;
  visibility: visible; /* and this!!!!!! */
}
#blackDisable {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 2222;
  background-color: black;
}
<button id="disable">Click Me</button>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

3。队列

$("#disable").click(function() {
  $("body").append("<div id='blackDisable' class='fade'></div>");
  
  // Instead of setTimeout but again mimicking a callback
  $("#blackDisable").delay().queue(function(){
    $(this).addClass("showBack");
  });
  
});
.fade {
  opacity: 0;
  transition: opacity .3s linear;
}
.showBack {
  opacity: .5;
}
#blackDisable {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 2222;
  background-color: black;
}
<button id="disable">Click Me</button>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>

相关内容

  • 没有找到相关文章

最新更新