【问题标题】:Trying to use transitionend event instead of setTimeout尝试使用 transitionend 事件而不是 setTimeout
【发布时间】:2017-11-04 23:25:07
【问题描述】:

我有这个代码:

$("#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 替换为transitionend 事件处理程序,但它没有触发。

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

在此先感谢,对不起我的英语!

【问题讨论】:

  • 我认为这不是您要找的。在您想要应用$("#blackDisable").addClass("showBack") 之前,您的代码中没有transition 事件将结束。 .showBack 是具有转换的类 - 所以你不能等到它结束后再应用它 - 有意义吗? setTimeout 是这样做的方法。
  • 我知道如何使用它,但它没有用@CBroe
  • 例如,引导模式与setTimeout?@MichaelCoker 的工作方式相同
  • 我认为 transitionend 与您的问题无关。这是一个问题,立即将类分配给新创建的(尚未 DOM 样式可发现的)元素 - 无需回调 - 其中 setTimeout 实际上就像一个 DOM 就绪黑客。

标签: jquery html css css-transitions


【解决方案1】:

transitionend 与您的问题有关 - 因为没有过渡到注册任何 end
这是一个立即将类分配给新创建的(尚未 DOM-styles-discoverable)元素的问题 - without a callback - 其中setTimeout 实际上就像“DOM-ready hack em>”。

没有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>

【讨论】:

  • 我不喜欢第一种方式,因为blackDisable 一开始会在 DOM 上。第二种方法看起来不错,但我真的不明白它是如何工作的。 delayqueue 函数有什么作用?
  • @Sagie 编辑了我的答案,我会使用 animation 而不是 transition - 要回答您的评论,.queue() (jQuery) 是一种在将动画绑定到元素后推迟事件的方法. delay 是一个动画,所以实际上就像你的 setTimeout 只是以更 jQuer-ysh 的方式。
  • 我将继续使用transition 方式。在 jQuery 文档中,我看到 delay() 获得延迟的持续时间。在您的示例中,它如何知道延迟多少时间?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多