gpt4 book ai didi

javascript - 如果鼠标悬停在 DIV 上,则保持显示

转载 作者:行者123 更新时间:2023-11-29 18:25:32 28 4
gpt4 key购买 nike

我有以下 jQuery 代码(取自此处某处的另一个问题):

$(document).ready(function(){
$("#profile_dropdown").mouseenter(function(){
clearTimeout($(this).data('timeoutId'));
$("#profile_dropdown_content").fadeIn("slow");
}).mouseleave(function(){
var someElement = $("#profile_dropdown");
var timeoutId = setTimeout(function(){
$("#profile_dropdown_content").fadeOut("slow");
}, 650);
//set the timeoutId, allowing us to clear this trigger if the mouse comes back over
someElement.data('timeoutId', timeoutId);
});
});

它按预期工作,但是 - 我无法弄清楚如何在鼠标移到它上面时保持 #profile_dropdown_content 显示。如果鼠标移出,如何让它淡出……

有什么想法吗?

最佳答案

#profile_dropdown_content 嵌套在 #profile_dropdown 容器中并使用悬停事件。

jsfiddle:http://jsfiddle.net/UsWYq/1/

JS

$(document).ready(function(){
$("#profile_dropdown").hover(function(){
clearTimeout($(this).data('timeoutId'));
$("#profile_dropdown_content").fadeIn("slow");
}, function(){
var someElement = $("#profile_dropdown");
var timeoutId = setTimeout(function(){
$("#profile_dropdown_content").fadeOut("slow");
}, 650);
//set the timeoutId, allowing us to clear this trigger if the mouse comes back over
someElement.data('timeoutId', timeoutId);
});
});

HTML

<div id="profile_dropdown">
<div class="inner">Hover Me</div>
<div id="profile_dropdown_content">Display Me</div>
</div>
<div id="profile_dropdown"></div>

CSS

#profile_dropdown {
background:whitesmoke;
float:left;
}
#profile_dropdown .inner {
height:100px;
width:100px;
}
#profile_dropdown_content {
display:none;
background:red;
height:100px;
width:100px;
}

选项 2

您可以做的另一件事是 CSS3 转换:http://jsfiddle.net/Ezxm5/

#profile_dropdown {
background:whitesmoke;
float:left;
}
#profile_dropdown:hover #profile_dropdown_content {
display:block;
opacity:1;
height:100px;
}
#profile_dropdown .inner {
height:100px;
width:100px;
}
#profile_dropdown_content {
opacity:0;
background:red;
height:0;
width:100px;
overflow:hidden;
-webkit-transition: opacity 0.4s ease-in, height 0.4s ease-out;
-moz-transition: opacity 0.4s ease-in, height 0.4s ease-out;
-ms-transition: opacity 0.4s ease-in, height 0.4s ease-out;
-o-transition: opacity 0.4s ease-in, height 0.4s ease-out;
transition: opacity 0.4s ease-in, height 0.4s ease-out;
}​

关于javascript - 如果鼠标悬停在 DIV 上,则保持显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13883629/

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