<tfoot id='mkdy4'></tfoot>
    <legend id='mkdy4'><style id='mkdy4'><dir id='mkdy4'><q id='mkdy4'></q></dir></style></legend>

    <i id='mkdy4'><tr id='mkdy4'><dt id='mkdy4'><q id='mkdy4'><span id='mkdy4'><b id='mkdy4'><form id='mkdy4'><ins id='mkdy4'></ins><ul id='mkdy4'></ul><sub id='mkdy4'></sub></form><legend id='mkdy4'></legend><bdo id='mkdy4'><pre id='mkdy4'><center id='mkdy4'></center></pre></bdo></b><th id='mkdy4'></th></span></q></dt></tr></i><div id='mkdy4'><tfoot id='mkdy4'></tfoot><dl id='mkdy4'><fieldset id='mkdy4'></fieldset></dl></div>

    <small id='mkdy4'></small><noframes id='mkdy4'>

      <bdo id='mkdy4'></bdo><ul id='mkdy4'></ul>

    1. 使用 javascript/jQuery 查找最接近鼠标位置的网格坐标

      Finding the closest grid coordinate to the mouse position with javascript/jQuery(使用 javascript/jQuery 查找最接近鼠标位置的网格坐标)
      <i id='xzJ3H'><tr id='xzJ3H'><dt id='xzJ3H'><q id='xzJ3H'><span id='xzJ3H'><b id='xzJ3H'><form id='xzJ3H'><ins id='xzJ3H'></ins><ul id='xzJ3H'></ul><sub id='xzJ3H'></sub></form><legend id='xzJ3H'></legend><bdo id='xzJ3H'><pre id='xzJ3H'><center id='xzJ3H'></center></pre></bdo></b><th id='xzJ3H'></th></span></q></dt></tr></i><div id='xzJ3H'><tfoot id='xzJ3H'></tfoot><dl id='xzJ3H'><fieldset id='xzJ3H'></fieldset></dl></div>
    2. <tfoot id='xzJ3H'></tfoot>
    3. <legend id='xzJ3H'><style id='xzJ3H'><dir id='xzJ3H'><q id='xzJ3H'></q></dir></style></legend>
        <bdo id='xzJ3H'></bdo><ul id='xzJ3H'></ul>

        1. <small id='xzJ3H'></small><noframes id='xzJ3H'>

                <tbody id='xzJ3H'></tbody>

                本文介绍了使用 javascript/jQuery 查找最接近鼠标位置的网格坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我要做的是在页面上创建一个等距的不可见坐标网格.然后,我希望在触发 onclick 时将 <div> 放置在最接近指针的任何网格坐标处.这是粗略的想法:

                What I'm trying to do is make a grid of invisible coordinates on the page equally spaced. I then want a <div> to be placed at whatever grid coordinate is closest to the pointer when onclick is triggered. Here's the rough idea:

                我已经跟踪了鼠标坐标并且 <div> 的放置效果很好.我坚持的是如何解决坐标网格的问题.

                I have the tracking of the mouse coordinates and the placing of the <div> worked out fine. What I'm stuck with is how to approach the problem of the grid of coordinates.

                首先,我是否应该将所有坐标放在一个数组中,然后将我的 onclick 坐标与之进行比较?

                First of all, should I have all my coordinates in an array which I then compare my onclick coordinate to?

                或者看到我的网格坐标遵循一个规则,我是否可以做一些事情,比如找出哪个坐标是 无论我的间距是多少的倍数最接近 onclick 坐标?

                Or seeing as my grid coordinates follow a rule, could I do something like finding out which coordinate that is a multiple of whatever my spacing is is closest to the onclick coordinate?

                然后,我从哪里开始计算最接近的网格点坐标?最好的方法是什么?

                And then, where do I start with working out which grid point coordinate is closest? What's the best way of going about it?

                谢谢!

                推荐答案

                我最初是在写一个类似于 bobince 的答案,但他比我先到了那里.我喜欢这样做的方式,但他的版本有一些楼层(尽管它仍然是一个很好的答案).

                I was initially writing an answer similar to bobince's, but he got there before me. I like that way of doing it, but his version has got some floors (though it's still a very good answer).

                我认为你想要的是一个没有 HTML 的网格(即没有像表格那样的标记),bobince 提供了一个解决方案.在这种情况下,代码可能会针对跨浏览器兼容性、可读性、错误和速度进行显着优化.

                I presume that what you want is a HTML-less grid (that is, without markup like a table), which bobince supplies a solution for. In that case, the code may be optimised significantly for cross browser compatibility, readability, errors and speed.

                所以,我建议代码应该更像这样:

                So, I suggest the code should be more like this:

                #canvas { position: relative; width: 100px; height: 100px; border: solid red 1px; }
                #nearest { position: absolute; width: 10px; height: 10px; background: yellow; }
                
                <div id="canvas"><div id="nearest"></div></div>
                
                var
                    canvasOffset = $("div#canvas").offset(),
                    // Assuming that the space between the points is 10 pixels. Correct this if necessary.
                    cellSpacing = 10;
                
                $("div#canvas").mousemove(function(event) {
                    event = event || window.event;
                    $("div#nearest").css({
                        top: Math.round((mouseCoordinate(event, "X") - canvasOffset.left) / cellSpacing) * cellSpacing + "px",
                        left: Math.round((mouseCoordinate(event, "Y") - canvasOffset.top) / cellSpacing) * cellSpacing + "px"
                    });
                });
                
                // Returns the one half of the current mouse coordinates relative to the browser window.
                // Assumes the axis parameter to be uppercase: Either "X" or "Y".
                function mouseCoordinate(event, axis) {
                    var property = (axis == "X") ? "scrollLeft" : "scrollTop";
                    if (event.pageX) {
                        return event["page"+axis];
                    } else {
                        return event["client"+axis] + (document.documentElement[property] ? document.documentElement[property] : document.body[property]);;
                    }
                };
                

                mouseCoordinate() 函数是这两个函数的简化版本:

                The mouseCoordinate() function is a boiled down version of these two functions:

                function mouseAxisX(event) {
                    if (event.pageX) {
                        return event.pageX;
                    } else if (event.clientX) {
                        return event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                    }
                };
                
                function mouseAxisY(event) {
                    if (event.pageY) {
                        return event.pageY;
                    } else if (event.clientY) {
                        return event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                    }
                };
                

                我真的很喜欢你的项目的想法,也许我会自己做一些类似的东西:D

                I really like the idea of your project, perhaps I'll make something similar myself :D

                这篇关于使用 javascript/jQuery 查找最接近鼠标位置的网格坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                quot;Status Code:200 OK (from ServiceWorker)quot; in Chrome Network DevTools?(“状态码:200 OK(来自 ServiceWorker)在 Chrome 网络开发工具中?)
                How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
                Adding custom HTTP headers using JavaScript(使用 JavaScript 添加自定义 HTTP 标头)
                SQL Query DocumentDB in Azure Functions by an integer not working(通过整数在 Azure Functions 中 SQL 查询 DocumentDB 不起作用)
                Azure Functions [JavaScript / Node.js] - HTTP call, good practices(Azure Functions [JavaScript/Node.js] - HTTP 调用,良好实践)
                Azure Functions - Import Custom Node Module(Azure Functions - 导入自定义节点模块)
                  <bdo id='llgm3'></bdo><ul id='llgm3'></ul>

                    1. <tfoot id='llgm3'></tfoot>
                      • <legend id='llgm3'><style id='llgm3'><dir id='llgm3'><q id='llgm3'></q></dir></style></legend>

                        <small id='llgm3'></small><noframes id='llgm3'>

                          <tbody id='llgm3'></tbody>
                          <i id='llgm3'><tr id='llgm3'><dt id='llgm3'><q id='llgm3'><span id='llgm3'><b id='llgm3'><form id='llgm3'><ins id='llgm3'></ins><ul id='llgm3'></ul><sub id='llgm3'></sub></form><legend id='llgm3'></legend><bdo id='llgm3'><pre id='llgm3'><center id='llgm3'></center></pre></bdo></b><th id='llgm3'></th></span></q></dt></tr></i><div id='llgm3'><tfoot id='llgm3'></tfoot><dl id='llgm3'><fieldset id='llgm3'></fieldset></dl></div>