Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在div外悬停时隐藏div_Javascript_Jquery - Fatal编程技术网

Javascript 在div外悬停时隐藏div

Javascript 在div外悬停时隐藏div,javascript,jquery,Javascript,Jquery,我有一个带id容器的大div里面有一个小div。如果有人在小div的外侧悬停,我想用id animate隐藏div。当鼠标在小div上时,它应该保持打开状态 这应该可以 <div id='container'> <div id="animate"></div> </div> 尝试: CSS: $('#small').hover(function () { $('#animate').show(); }, function () {

我有一个带id容器的大div里面有一个小div。如果有人在小div的外侧悬停,我想用id animate隐藏div。当鼠标在小div上时,它应该保持打开状态

这应该可以

<div id='container'>
    <div id="animate"></div>
</div>
尝试:

CSS:

$('#small').hover(function () {
    $('#animate').show();
}, function () {
    $('#animate').hide();
});
#container{width:100px;height:100px;background:#F00;}
#animate{width:50px;height:50px;background:#0F0;}
$(function(){
    $('#container').mouseenter(function(){
        $('#animate').fadeTo(1000,0)
                     .mouseenter(function(){$(this).fadeTo(1000,1)});
    }); // use 750 in place of 1000 to animate it fast
});
<div id='container'>
    <div id="animate">&nbsp;</div>
</div>
<div id='container'>
    <div id="animate">HI!</div>
</div>
#container{
    width: 100px;
    height: 200px;
    background-color: black; 
}
#animate{
    height: 50px;
    width: 100px;
    background-color: white;
    opacity: 0;
}
脚本:

$('#small').hover(function () {
    $('#animate').show();
}, function () {
    $('#animate').hide();
});
#container{width:100px;height:100px;background:#F00;}
#animate{width:50px;height:50px;background:#0F0;}
$(function(){
    $('#container').mouseenter(function(){
        $('#animate').fadeTo(1000,0)
                     .mouseenter(function(){$(this).fadeTo(1000,1)});
    }); // use 750 in place of 1000 to animate it fast
});
<div id='container'>
    <div id="animate">&nbsp;</div>
</div>
<div id='container'>
    <div id="animate">HI!</div>
</div>
#container{
    width: 100px;
    height: 200px;
    background-color: black; 
}
#animate{
    height: 50px;
    width: 100px;
    background-color: white;
    opacity: 0;
}
文件

HTML:

$('#small').hover(function () {
    $('#animate').show();
}, function () {
    $('#animate').hide();
});
#container{width:100px;height:100px;background:#F00;}
#animate{width:50px;height:50px;background:#0F0;}
$(function(){
    $('#container').mouseenter(function(){
        $('#animate').fadeTo(1000,0)
                     .mouseenter(function(){$(this).fadeTo(1000,1)});
    }); // use 750 in place of 1000 to animate it fast
});
<div id='container'>
    <div id="animate">&nbsp;</div>
</div>
<div id='container'>
    <div id="animate">HI!</div>
</div>
#container{
    width: 100px;
    height: 200px;
    background-color: black; 
}
#animate{
    height: 50px;
    width: 100px;
    background-color: white;
    opacity: 0;
}

提琴:

HTML:

$('#small').hover(function () {
    $('#animate').show();
}, function () {
    $('#animate').hide();
});
#container{width:100px;height:100px;background:#F00;}
#animate{width:50px;height:50px;background:#0F0;}
$(function(){
    $('#container').mouseenter(function(){
        $('#animate').fadeTo(1000,0)
                     .mouseenter(function(){$(this).fadeTo(1000,1)});
    }); // use 750 in place of 1000 to animate it fast
});
<div id='container'>
    <div id="animate">&nbsp;</div>
</div>
<div id='container'>
    <div id="animate">HI!</div>
</div>
#container{
    width: 100px;
    height: 200px;
    background-color: black; 
}
#animate{
    height: 50px;
    width: 100px;
    background-color: white;
    opacity: 0;
}
jQuery:

$('#small').hover(function () {
    $('#animate').show();
}, function () {
    $('#animate').hide();
});
#container{width:100px;height:100px;background:#F00;}
#animate{width:50px;height:50px;background:#0F0;}
$(function(){
    $('#container').mouseenter(function(){
        $('#animate').fadeTo(1000,0)
                     .mouseenter(function(){$(this).fadeTo(1000,1)});
    }); // use 750 in place of 1000 to animate it fast
});
<div id='container'>
    <div id="animate">&nbsp;</div>
</div>
<div id='container'>
    <div id="animate">HI!</div>
</div>
#container{
    width: 100px;
    height: 200px;
    background-color: black; 
}
#animate{
    height: 50px;
    width: 100px;
    background-color: white;
    opacity: 0;
}


您可能希望执行严格的/,因为当元素隐藏时,没有高度/宽度悬停在上。相反,您可能希望将不透明度设置为0(隐藏)或1(显示),并让函数在两者之间转换。您还会注意到我使用了这个函数。这是因为如果在元素上来回移动,它将继续调用排队的动画。先打电话可以避免这种情况

您可以使用纯CSS实现相同的效果:

$("#animate").hover(
    function(){
        $(this).stop().animate({
            opacity: 1
        }, 1000);
    },
    function(){
        $(this).stop().animate({
            opacity: 0
        }, 1000);
    }
);

演示:

默认情况下是否隐藏
animate
?我假设“小div”是#animate div?StackOverflow不是这个问题的合适位置。我们不会为您编写代码。你需要自己编写代码,如果你不确定为什么有些东西不能按预期工作,那么在发布代码时解释一下你期望它做什么,以及它实际在做什么,包括所有错误消息。请参阅。(引用OP:)当鼠标位于小div上时,它应该保持打开状态。