简体   繁体   English

JQuery将多个元素悬停在一个函数上

[英]JQuery hover multiple elements one function

I have three divs with hidden inner divs, when you rollover over each div its inner div should display and when you rollout it hides again. 我有三个div与隐藏的内部div,当你翻转每个div时,它的内部div应该显示,当你推出它时再次隐藏。

for example, I rollover div1, div1 inner appears, I rollout, div1 inner disappears. 例如,我翻转div1,div1内部出现,我推出,div1内部消失。

However when I move my mouse from div1 directly over div2, both are treated as rollout, eg div1 inner disappears (as it should), div2 inner appears(as it should) but then instantly disappears with div1 inner. 然而,当我将div1直接从div2移动到div2时,两者都被视为rollout,例如div1 inner消失(应该如此),div2 inner出现(应该如此),但随后div1内部立即消失。

Apart from writing separate functions for div1 2 and 3 I'm not sure what to do, any help much appreciated!! 除了为div1 2和3编写单独的函数之外,我不知道该怎么做,任何帮助都非常赞赏!

jsfiddle.net/user1688604/UZpEH/3 jsfiddle.net/user1688604/UZpEH/3

var box = $("#box1,#box2,#box3");
var inner = $(".item");


$(box).hover(function() {
    $(this).find(inner).stop(true,true).css({"left":"20px"}).animate({"top":"-10px", "opacity": "1"},400);

}, function() {
    $(this).find(inner).stop(true,true).animate({"top":"-20px", "opacity": "0"},400, function() {
        $(inner).css({"left":"-9999px", "top":"0"});
        });
});



<div id="box1">
<div class="item"></div>
</div>

#box1, #box2, #box3 {
            width: 300px;
            float: left;
            margin-right: 20px;
            position: relative;
            }


            .item {
                width: 151px;
                height: 49px;
                padding: 5px 10px 0;
                opacity: 0;
                position: absolute;
                    top: 0;
                    left: -9999px;
                }

Add a class to the box divs (same class for each) 在框div中添加一个类(每个类都相同)

<div id="box1" class="box">
    <div class="item"></div>
</div>

jQuery jQuery的

$(".box").hover(function(){
   $(this).find(".item").stop(true,true).css({"left":"20px"}).animate({"top":"-10px", "opacity": "1"},400);
},function(){
   $(this).find(".item").animate({"top":"-20px", "opacity": "0"},400, function() {
       $(this).css({"left":"-9999px", "top":"0"});
   });
});

在无论哪个div或任何字段,你想提供悬停函数只需调用相同的类名,然后通过引用该类名来触发你的函数。

Try like below... it will help you.. 尝试如下...它会帮助你..

Keep Same class name for all the DIV.. and try the below.. 为所有DIV保留相同的类名...并尝试以下..

Fiddle Example : http://jsfiddle.net/RYh7U/96/ 小提琴示例: http//jsfiddle.net/RYh7U/96/

HTML : HTML:

<div class="divBox">
    Show Div1
<div class="item">
Div 1    
</div>
</div>

<div class="divBox">
    Show Div 2
<div class="item">
    Div 2

</div>
</div>

<div class="divBox">
    Show Div 3
<div class="item">    
    Div 3
</div>
</div>

JQuery : JQuery:

$('.item').hide(); 
$('.divBox').hover(function() { 
    $(this).children('.item').show();     
}, function() { 
    $(this).children('.item').hide(); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM