简体   繁体   English

在具有相同类别的一组div中将鼠标悬停在单个div上显示覆盖

[英]Show overlay on hover over single div in set of divs with same class

Let's say I have 4 dynamically generated div's, each with the same class, and unique number ids, like so. 假设我有4个动态生成的div,每个div具有相同的类,并且具有唯一的数字ID,就像这样。 Each contains an absolutely positioned div with 100% width and height so when displayed, it fills the entire div. 每个都包含一个绝对定位的div,其宽度和高度为100%,因此在显示时,它会填充整个div。

<div class="my_class" id="1">
    <div class="overlay"></div>
</div>

<div class="my_class" id="2">
    <div class="overlay"></div>
</div>

<div class="my_class" id="3">
    <div class="overlay"></div>
</div>

<div class="my_class" id="4">
    <div class="overlay"></div>
</div>



<style>
    .overlay {
        display:none;
    }
</style>

When I hover over one of these divs, I want that div, and none of the others to display the overlay, then when I stop hovering, the overlay div should disappear. 当我将鼠标悬停在这些div之一上时,我想要那个div,而其他都不显示该覆盖,那么当我停止悬停时,该覆盖div应该消失了。 Any ideas as to how to do this using jquery? 关于如何使用jquery做到这一点的任何想法? This is what I've tried. 这就是我尝试过的。

$('document').ready(function(){
  $('.my_class').mouseenter(function(){
    var id = $(this).attr("id");
    $("#"+ id +" div").fadeTo("fast", 1);
    $("#"+ id +" div").mouseleave(function(){
      $("#"+ id +" div").fadeTo("fast", 0);          
    });
  });
});

Inside the mouseenter callback just use $(this).find to find only the div within it. 里面mouseenter回调只是用$(本).find到里面找到只有股利。

Adding the mouseleave callback within the mouseenter callback is a bad idea. 添加mouseleave回调中mouseenter回调是一个坏主意。 that will add a new callback every time the mouse enters the parent div. 每次鼠标进入父div都会添加一个新的回调。

Is this what you're trying to do? 这是您要做什么?

http://jsfiddle.net/2R3Yt/ http://jsfiddle.net/2R3Yt/

$('document').ready(function () {
    $('.my_class').mouseenter(function () {
        var id = $(this).attr("id");
        $(this).find("div").fadeTo("fast", 1);
    });

    $('.my_class').mouseleave(function () {
        $(this).find("div").fadeTo("fast", 0);
    });
});

Id rewrite it to something like: id将其重写为:

$('document').ready(function(){
 $('.my_class').mouseenter(function(){$(this).fadeTo("fast", 1);});
 $('.my_class').mouseleave(function(){$(this).fadeTo("fast", 0);});
});

You are attaching the mouseleave inside the mouseenter and I guess you didn't wont that. 您要连接的mouseleave里面mouseenter ,我想你不惯于说。

The original code was working for me, however; 但是,原始代码对我有用。 the events continuously fade the overlays in and out if you move your cursor erratically between divs . 如果您在divs之间不规则地移动光标,则事件会不断淡入和淡出叠加层。

To fix that, I replaced the original mouseenter and mouseleave events with one which is similar to on but only catches on the event once. 为了解决这个问题,我取代了原来mouseentermouseleave事件有one这是类似on ,但只有一次的事件捕获。

$('.my_class').one('mouseenter', function() {...});
$('.my_class').one('mouseleave', function() {...});

I then put the mouseenter event inside the mouseleave so we can continue doing the hover effect on the after we leave the active div. 然后,我将mouseenter事件放入mouseleave以便在离开活动div之后可以继续mouseleave进行hover效果。

http://jsfiddle.net/q8qFy/4/ http://jsfiddle.net/q8qFy/4/

If you're interested CSS only solution: 如果您对仅CSS解决方案感兴趣:

http://jsfiddle.net/EsVLC/1/ http://jsfiddle.net/EsVLC/1/

I think this solution may be a bit tidier. 我认为此解决方案可能会更简洁一些。 Also have a fiddle . 也要摆弄

$('.my_class').mouseenter(function() {
    $(this).children('.overlay').fadeTo(1000, 0);
})

$('.my_class').mouseleave(function() {
    $(this).children('.overlay').fadeTo(1000, 1);
})

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

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