简体   繁体   English

Div1涵盖Div2:如何检查鼠标是否覆盖了Div2?

[英]Div1 covers Div2: how to check if the mouse is over the covered Div2?

I'm a bit lost. 我有点迷茫。 I try to check if my mouse is over a Div which is covered by another Div. 我试着检查我的鼠标是否在另一个Div覆盖的Div上。 I search a vanilla js solution if possible. 如果可能的话,我会搜索一个vanilla js解决方案。

I tried to use the elementFromPoint method, but it only seems to give me the top Div. 我试图使用elementFromPoint方法,但它似乎只给我顶级Div。 I also tried to mess around with the "onmouseover" Event, but I didn't found a solution either. 我也试图搞乱“onmouseover”事件,但我也没有找到解决方案。 Maybe I just overlooked something. 也许我只是忽略了一些东西。

Any ideas how to solve this? 任何想法如何解决这个问题? I want a method to check if my mouse is over the smaller Div2 or not. 我想要一种方法来检查我的鼠标是否在较小的Div2上。 I made a jsFiddle to show the situation. 我做了一个jsFiddle来展示这种情况。 I made the covering Div translucent for easier understanding from the setup. 我制作了覆盖Div半透明,以便从设置中更容易理解。

http://jsfiddle.net/y2L5C/ http://jsfiddle.net/y2L5C/

  <div id="div1"></div>
    <div id="div2"></div>



   #div1 {
    width:300px;
    height:300px;
    background:red;
    position:absolute;
    top:0px;
    z-index:1;
    opacity: 0.5;
}

#div2 {
    width:200px;
    height:200px;
    background:blue;
    position:absolute;
    top:0px;
}

Here's a quick and dirty solution. 这是一个快速而肮脏的解决方案。 I'll leave it up to you to optimize it. 我会留给你优化它。 Here's the fiddle: http://jsfiddle.net/y2L5C/1/ 这是小提琴: http//jsfiddle.net/y2L5C/1/

var div2 = $("#div2"),
    width = div2.outerWidth(true),
    height = div2.outerHeight(true),
    offset = div2.offset(),
    top = offset.top,
    left = offset.left;

$(document).mousemove(function(evt) {
    if(evt.pageX <= (left + width) && evt.pageX >= left &&
       evt.pageY <= (top + height) && evt.pageY >= top) {
        $("#indicator").text("Over the div #2");
    } else {
        $("#indicator").text("NOT over the div #2");
    }
});

if you want to check if your mouse is over a <div> that is covered by another <div> , you can achieve this by declaring this code: pointer-events: none; 如果您要检查,如果你的鼠标在<div>被另一个所覆盖<div>你可以通过声明该代码实现这一点: pointer-events: none; to the css of the covering <div> . 覆盖<div>css

For example: 例如:

<div id="under"></div>
<div id="over"></div>

Add this to your css file: 将其添加到您的css文件中:

#over{ pointer-events: none; }

In that case, all pointer events for the div having the id=over will be ignored. 在这种情况下,将忽略具有id=overdiv所有指针事件。 You can now then add this code to test if its working. 您现在可以添加此代码以测试其是否正常工作。

Add this JavaSCript code: 添加此JavaSCript代码:

$('#under').mouseover(function() {
    alert("Mouse is over the div having the id='under'");
});

Give it a try! 试试看! Good luck! 祝好运!

Interesting concept. 有趣的概念。 I do want to bring up that for plain CSS events there are plain CSS solutions such as here . 我想提一下,对于普通的CSS事件,有简单的CSS解决方案,比如这里 However, if what you are looking to do is initiate Javascript events then the trouble is that onMouseOver is not going to trigger for #div1 if #div2 is on top of it. 但是,如果你要做的是启动Javascript事件,那么麻烦的是onMouseOver不会触发#div1如果#div2在它之上。

One potential, very simple solution, is to create a script to copy the position of your #div2 element and change the style to be a higher z-index. 一个非常简单的潜在解决方案是创建一个脚本来复制#div2元素的位置,并将样式更改为更高的z-index。 While JQuery might be "easier" to create this, you could certainly create a vanilla JS solution. 虽然JQuery可能“更容易”创建它,但您当然可以创建一个vanilla JS解决方案。 This script may give you a little guidance as to how you can find positioning. 此脚本可能会为您提供有关如何找到定位的一些指导。 You can use element.style values in order to assign CSS values. 您可以使用element.style值来分配CSS值。 If your element positions are declared by CSS then you can do something like this: 如果您的元素位置由CSS声明,那么您可以执行以下操作:

var div1 = getElementById('div1');
var div2 = getElementById('div2');
var newElem = document.createElement('div');
newElem.id = 'div2makefacade';

Now you can either utilize newElem.style.top etc. and assign div2.style.top 's value, or you can even assign a custom class which has the correct position values. 现在你可以使用newElem.style.top等并分配div2.style.top的值,或者你甚至可以指定一个具有正确位置值的自定义类。 When you initiate onMouseOver, you can do so on #div2makefacade rather than #div2 , and perform actions on #div2 当您启动onMouseOver时,您可以在#div2makefacade而不是#div2执行此操作,并对#div2执行操作

well i made a function that perhaps it help you in some way, first in your view you have to load jquery librarie like this 好吧,我做了一个函数,也许它以某种方式帮助你,首先在你的视图中你必须加载像这样的jquery librarie

<script type="text/javascript" src="Scripts/jquery-1.6.2.js"></script>

and your css you have this 和你的css你有这个

.visiblepanel{
  display: none;   
}

.visiblepanela{
  display: none;   
}

your script this, you have to add the hover function 你的脚本,你必须添加悬停功能

$('#quickstart').hover(function() {
    $('#visiblepanel').toggle(); 
});

$('#quickstarta').hover(function() {
    $('#visiblepanela').toggle(); 
});

and your html body 和你的HTML身体

<div id="quickstart">Div 1</div>

<div id="quickstarta">Div 2</div>

<div id="visiblepanel" class="visiblepanel">mouse encima div 1</div>

<div id="visiblepanela" class="visiblepanel">mouse encima div 2</div>

so it consist that when the mouse is over the DIV 1 it will show an advice that your mouse is there, and so on with DIV 2...i hope i could helped you... 因此,当鼠标悬停在DIV 1上时,它会显示你的鼠标在那里的建议,依此类推DIV 2 ...我希望我能帮助你......

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

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