gpt4 book ai didi

jquery - 尝试使用transitionend事件而不是setTimeout

转载 作者:行者123 更新时间:2023-12-01 05:20:38 25 4
gpt4 key购买 nike

我有这个代码:

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

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

提前致谢,抱歉我的英语不好!

最佳答案

transitionend 与您的问题有任何关系 - 因为没有转换来注册任何end
这是立即将类分配给新创建的(尚未可发现 DOM 样式)元素的问题 - 没有回调 - 其中 setTimeout 实际上起作用就像“DOM 就绪黑客”。

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

1。 动画(而不是过渡)

$("#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 可见性来防止重叠问题

// 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>

关于jquery - 尝试使用transitionend事件而不是setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44347745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com