admin管理员组文章数量:1332708
Well, i am trying to create a box should pop out when a link is hovered,
well i got the basic covered, but for some reason i couldn't fully acplish what i am trying to do.
as you can see on here, / (sorry its a poor recreation of the problem i am having)
What i am trying to do is to make the box (TEST
) visible if HOVER HERE
is hovered and box should visible if mouse is hovered inside.
when the mouse entered TEST
, it works correctly, however, when it is hovered to other OTHER
and a empty space, it doesn't work correctly as the TEST
box is still visible.
How do i make the TEST
box hide if mouse hovered on OTHER & empty space
Thanks and i am newbie to jQuery.
Well, i am trying to create a box should pop out when a link is hovered,
well i got the basic covered, but for some reason i couldn't fully acplish what i am trying to do.
as you can see on here, http://jsfiddle/EpV87/1/ (sorry its a poor recreation of the problem i am having)
What i am trying to do is to make the box (TEST
) visible if HOVER HERE
is hovered and box should visible if mouse is hovered inside.
when the mouse entered TEST
, it works correctly, however, when it is hovered to other OTHER
and a empty space, it doesn't work correctly as the TEST
box is still visible.
How do i make the TEST
box hide if mouse hovered on OTHER & empty space
Thanks and i am newbie to jQuery.
3 Answers
Reset to default 5Inspired by this old answer:
var $link = $(".link");
var $box = $("#box");
$link.mouseenter(function() {
clearTimeout($box.data('timeoutId'));
$box.show(200);
}).mouseleave(function() {
var timeoutId = setTimeout(function() {
$box.hide(200);
}, 650);
$box.data('timeoutId', timeoutId);
});
$box.mouseenter(function() {
clearTimeout($box.data('timeoutId'));
}).mouseleave(function() {
var timeoutId = setTimeout(function() {
$box.hide(200);
}, 650);
$box.data('timeoutId', timeoutId);
});
.link {
border: 1px solid red;
padding: 10px;
}
#box {
display: none;
border: 1px solid red;
margin-top: 10px;
padding: 10px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="link">Hover me</a>
<div id="box">Surprise!</div>
You were handling an mouseleave inside another mouseleave handler in the version I checked and had a typo in your selector
$('#abc').mouseleave(function(){...
should be
$('.abc').mouseleave(function(){...
Version you posted originally
http://jsfiddle/EpV87/1/
HTML
<a class="abc">ABC</a>
<div id="def">TEST</div>
Javascript
$('.abc').mouseenter(function(e) {
$('#def').show(200);
}).mouseleave(function(e){
$('#abc').mouseleave(function(){
$('#def').hide(200);
});
});
Solution
http://jsfiddle/EpV87/6/
HTML
<a class="abc">ABC</a>
<div id="def" style="display: none;">TEST</div>
Javascript
$('.abc')
.on("mouseenter", function () {
$("#def").show();
})
.on("mouseleave", function () {
$("#def").hide();
});
You need another div to contain the two elements:
<div id="container">
<a class="abc">ABC</a>
<div id="def">TEST</div>
</div>
This way you can make the TEST div disappear when the mouse leaves the container div.
$('.abc').mouseenter(function(e) {
$('#def').show(200);
});
$('#container').mouseleave(function(e){
$('#def').hide(200);
});
You can check it here.
本文标签: javascriptjQuery display box on hover and hide on mouseleaveStack Overflow
版权声明:本文标题:javascript - jQuery display box on hover and hide on mouseleave - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235689a2438036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论