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

发布于 2024-08-17 05:03:12 字数 583 浏览 6 评论 0原文

我正在尝试检测圆圈上的鼠标悬停事件。我这样定义圆形 div:

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

然后我使用 jQuery 检测鼠标悬停,如下所示:

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

这效果很好,只是整个 80px x 80px 区域会触发鼠标悬停事件。换句话说,即使鼠标不在可见圆圈上方,只要触摸 div 的右下角就会触发 mouseover 事件。

有没有一种简单且jquery友好的方法来仅在圆形区域中触发鼠标悬停事件?

更新:为了解决这个问题,我们假设浏览器支持 CSS3 并且正确渲染边框半径。有没有人拥有疯狂的数学/几何技能来想出一个简单的方程来检测鼠标是否已进入圆圈?为了更简单,我们假设它是一个圆,而不是任意的边界半径。

I am trying to detect a mouseover event on a circle. I define the circle div like this:

.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:

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

This works well, except that the entire 80px by 80px area triggers the mouseover event. 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.

Is there a simple and jquery friendly way to trigger the mouseover event in the circular area only?

Update: For the sake of this question, let's assume that the browser is CSS3 capable and renders the border-radius correctly. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光礼记 2024-08-24 05:03:12

如果鼠标的位置太远,则忽略鼠标悬停事件。这真的很简单。取div的中心点,计算到鼠标指针的距离(距离公式 = sqrt((x2 - x1)^2 + (y2 - y1)^2))以及是否更大比圆的半径(div 宽度的一半)大,它还没有在圆中。

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.

岁月苍老的讽刺 2024-08-24 05:03:12

不,没有。用几何术语思考。您仍在使用 div,它是一个盒子元素。该框元素具有触发鼠标悬停事件的特定矩形边界。使用 CSS 提供圆形边框只是装饰性的,不会更改该元素的矩形边界。

No, there is not. Think in geometrical terms. You're still using a div, which is a box element. That box element has a specific rectangular boundary that triggers the mouse over event. The use of CSS to supply a rounded border is cosmetic only, and does not change the rectangular boundary of that element.

野稚 2024-08-24 05:03:12

您可能可以使用老式图像地图做类似的事情 - 有一个圆形区域。
事实上,这个插件可以帮助你: jQuery MapHilight 插件< /a>

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

忘你却要生生世世 2024-08-24 05:03:12

如果你想让它像你想要的那样工作,则需要大量的计算。每当鼠标进入一个对象时,您必须首先找出它是否有圆角(考虑到不同的浏览器),然后计算光标是否确实在这些角内,如果是,则应用悬停类。

不过,似乎不值得这么麻烦。

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.

陪你搞怪i 2024-08-24 05:03:12

这是一个使用 Javascript 计算两点(圆心和 mouseX/mouseY)之间距离的片段: http://snipplr.com/view/47207/

Here is an snnipet to calculate the distance betewwn two points (the center of the circle, and the mouseX/mouseY) using Javascript: http://snipplr.com/view/47207/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文