sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-01 05:20:38 27 4
mua khóa gpt4 Nike

我有这个代码:

$("#disable").click(function () {
$("body").append("
");
setTimeout(function () {
$("#blackDisable").addClass("showBack");
}, 150);
});
.fade {
opacity: 0;
transition: opacity .15s linear;
}

.showBack {
opacity: .5;
}

#blackDisable {
position: fixed;
top: 0;
dưới cùng: 0;
left: 0;
phải: 0;
z-index: 2222;
background-color: black;
}

我不想使用setTimeout来强制转换工作,所以我搜索了解决方案并找到了transitionend事件并替换了setTimeout code> 与 transitionend 事件处理程序,但它没有触发。

如何删除此 setTimeout và sử dụng transitionend 事件?

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

1 Câu trả lời

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

KHÔNGsetTimeout?两种方式(甚至更多):

1。 Hoạt hình(còn hơn là过渡)

$("#disable").click(function() {

$("body").append("
");
$("#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;
dưới cùng: 0;
left: 0;
phải: 0;
z-index: 2222;
background-color: black;
}

@keyframes fadeIn {
to { opacity: 0.5; }
}

2。在任何事件之前附加

并使用 CSS 可见性来防止重叠问题

// Append on DOM ready
$("body").append("
");

// 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;
dưới cùng: 0;
left: 0;
phải: 0;
z-index: 2222;
background-color: black;
}

3。队列

$("#disable").click(function() {

$("body").append("
");

// 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;
dưới cùng: 0;
left: 0;
phải: 0;
z-index: 2222;
background-color: black;
}

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

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com