我试图让页面上的一个元素在鼠标离开时淡出,然后对其应用一些 css 属性。问题是 css 属性在元素完全淡出之前被添加到元素中。这是我尝试过的:
$(".tiptrigger").mouseleave(function() {
var s_id = $(this).attr('id');
$("#tiptip_holder-"+s_id).fadeOut("fast");
$("#tiptip_holder-"+s_id).css({"margin-left": "0px", "margin-top": "0px"});
});
当那行不通时,我尝试了:
$(".tiptrigger").mouseleave(function() {
var s_id = $(this).attr('id');
$("#tiptip_holder-"+s_id).fadeOut("fast").delay(500).css({"margin-left": "0px", "margin-top": "0px"});
});
当这不起作用时,我终于尝试了:
$(".tiptrigger").mouseleave(function() {
var s_id = $(this).attr('id');
$("#tiptip_holder-"+s_id).fadeOut("fast");
}, function() {
$("#tiptip_holder-"+s_id).css({"margin-left": "0px", "margin-top": "0px"});
});
有什么建议吗?
最佳答案
您可以使用 callback function
这里像:
$("#tiptip_holder-" + s_id).fadeOut("fast", function () {
$(this).css({
"margin-left": "0px",
"margin-top": "0px"
});
});
关于javascript - jQuery 过早地向元素添加 css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19811350/