检测鼠标位置是否在一组位置规则内?

发布于 2024-08-11 17:43:15 字数 585 浏览 4 评论 0原文

我有一些像这样的 JavaScript 对象(它是 psuedo,所以我知道语法是否错误):

[{ "divid":"1","x1":"35","y1":"100","height":"150","width":"150" },
{ "divid":"2","x1":"45","y1":"110","height":"150","width":"150" },
{ "divid":"3","x1":"55","y1":"120","height":"150","width":"150" },
{ "divid":"4","x1":"65","y1":"130","height":"150","width":"150" }]

我目前正在使用 jQuery 检测鼠标位置,这很好,但是......

我想检测我何时超过这些位置之一自动返回该 JavaScript 集的第一列或多列,就像我查询它一样,如果位置位于其中一个返回列的集合列表之间。

我无法想象每次鼠标位置发生变化时我都必须对所有这些进行 .each() ?也许我会...

如果有人以前做过类似的事情,请指出我正确的方向。

谢谢 -乔什

I have some JavaScript objects such as this (it's psuedo so I know if syntax is wrong):

[{ "divid":"1","x1":"35","y1":"100","height":"150","width":"150" },
{ "divid":"2","x1":"45","y1":"110","height":"150","width":"150" },
{ "divid":"3","x1":"55","y1":"120","height":"150","width":"150" },
{ "divid":"4","x1":"65","y1":"130","height":"150","width":"150" }]

And I'm curruently detecting mouse position with jQuery which is fine but...

I want to detect when I'm over one of those positions automatically return back the first or multiple columns of that JavaScript set almost like I'm querying it if the position is between one of those return the set list of columns.

I can't imagine I would have to do an .each() over all of them everytime the mouse position changes? Maybe I would...

If anyone has done something like this before please point me in the right direction.

Thanks
-Josh

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

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

发布评论

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

评论(3

听你说爱我 2024-08-18 17:43:15

执行此操作的一个简单方法可能是绝对定位某些动态创建的 div 并为其分配 mouseOver 事件。它们必须具有高 z-index、不可见并且漂浮在其他内容之上。

An easy way to do this might be to absolute position some dynamically created divs and assign a mouseOver event to them. They'd have to have high z-index, be invisible, and floated above other content.

走野 2024-08-18 17:43:15

你可以得到你的慕斯的offSet,这取决于所选的范例,比如你的父div,
然后将返回值与从 json 中获得的值进行比较!

getposOffset:function(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;

如果匹配,则触发显示您需要的事件!

You could get the offSet of you mousse, depending of the chosen paradigm, say your parent div,
then compare the return values to the one you get from your json!

getposOffset:function(what, offsettype){
    var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
    var parentEl=what.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;

if it matches then fire the event that shows what you need!

生生漫 2024-08-18 17:43:15

我不知道这个想法是否适合你想要的,但我做了一些跳出框框的思考......

使用图像映射怎么样? :P

它需要最少的编码和处理,并且可以工作!我覆盖了映射图像并在这些区域上添加了悬停事件。我在这里发布了一个演示 - 它并不完美,但更多的是概念证明。

I don't know if this idea will work for what you want, but I did a little thinking outside the box...

How about using an image map? :P

It requires minimal coding and processing and it works! I overlaid a mapped image and added hover events on the areas. I posted a demo here - it's not perfect, but more of a proof of concept.

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