简体   繁体   English

准确检测具有圆角的div的鼠标悬停事件

[英]Accurately detect mouseover event for a div with rounded corners

I am trying to detect a mouseover event on a circle. 我试图在圆圈上检测鼠标悬停事件。 I define the circle div like this: 我像这样定义圆形div:

.circle {
  width: 80px;
  height: 80px;
  -moz-border-radius: 40px;
  -webkit-border-radius: 40px;
  background-color: #33f;
}

Then I detect the mousover using jQuery like this: 然后我使用jQuery检测mousover,如下所示:

$('.circle').mouseover(function() {
  $(this).css({backgroundColor:'#f33'});
});

This works well, except that the entire 80px by 80px area triggers the mouseover event. 除了整个80px乘80px区域触发鼠标悬停事件外,这种方法效果很好。 In other words, just touching the bottom right corner of the div triggers the mouseover event, even though the mouse is not over the visible circle. 换句话说,只需触摸div的右下角即可触发鼠标悬停事件,即使鼠标未在可见圆圈上方也是如此。

Is there a simple and jquery friendly way to trigger the mouseover event in the circular area only? 是否有简单且jquery友好的方式来触发圆形区域中的鼠标悬停事件?

Update: For the sake of this question, let's assume that the browser is CSS3 capable and renders the border-radius correctly. 更新:为了这个问题,让我们假设浏览器支持CSS3并正确渲染border-radius。 Does anyone have the mad math/geometry skills to come up with a simple equation to detect whether the mouse has entered the circle? 有没有人有疯狂的数学/几何技能来提出一个简单的方程来检测鼠标是否进入了圆圈? To make it even simpler, let's assume that it is a circle and not an arbitrary border radius. 为了使它更简单,我们假设它是一个圆而不是任意的边界半径。

Just ignore the mouseover event if the mouse's position is too far away. 如果鼠标的位置太远,请忽略鼠标悬停事件。 It's really simple. 这很简单。 Take the center point of the div, and calculate the distance to the mouse pointer (distance formula = sqrt((x2 - x1)^2 + (y2 - y1)^2) ) and if it's larger than the radius of the circle (half the width of the div), it's not in the circle yet. 取div的中心点,并计算到鼠标指针的距离(距离公式= sqrt((x2 - x1)^2 + (y2 - y1)^2) ),如果它大于圆的半径( div的宽度的一半),它还不在圆圈中。

No, there is not. 不,那里没有。 Think in geometrical terms. 用几何术语来思考。 You're still using a div, which is a box element. 你还在使用div,这是一个盒子元素。 That box element has a specific rectangular boundary that triggers the mouse over event. 该box元素具有特定的矩形边界,可触发鼠标悬停事件。 The use of CSS to supply a rounded border is cosmetic only, and does not change the rectangular boundary of that element. 使用CSS来提供圆形边框仅仅是装饰性的,并不会改变该元素的矩形边界。

You can probably do something like that with an old-fashioned image map - there's a circular area. 你可以用老式的图像映射做类似的事情 - 这里有一个圆形区域。
In fact, this plugin can help you: jQuery MapHilight Plugin 实际上,这个插件可以帮到你: jQuery MapHilight插件

这是一个使用Javascript计算两个点(圆心和鼠标X /鼠标Y)的距离的snnipet: http ://snipplr.com/view/47207/

If you want to get that to work like you intend it to do, it would require considerable amount of calculations. 如果你想让它像你想要的那样工作,那就需要大量的计算。 Whenever a mouse enters an object, you would have to first find out whether it has rounded corners (taking in account different browsers), then calculate if the cursor really is inside those corners, and if it is, apply an hover class. 每当鼠标进入一个对象时,你必须首先找出它是否有圆角(考虑到不同的浏览器),然后计算光标是否真的在这些角落内,如果是,则应用一个悬停类。

Doesn't seem as being worth all the trouble, though. 但是,似乎并不值得所有麻烦。

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

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