简体   繁体   English

在鼠标单击时将 XY 坐标转换为 PHP 变量

[英]Getting X Y Coordinates into PHP variables on Mouse Click

The following script moves a ball from one location to another inside a box.以下脚本将一个球从一个位置移动到一个盒子内的另一个位置。

I would like to gather the coordinates of where the mouse is clicked inside this box and convert the X and Y coordinates onclick over to PHP variables so some additional PHP code can process this.我想收集鼠标在此框中单击的位置的坐标,并将 onclick 的 X 和 Y 坐标转换为 PHP 变量,以便一些额外的 PHP 代码可以处理此问题。

How can this be done please?请问这怎么办?

<!DOCTYPE html>
<html>

<head>
<title>Move to Click Position</title>
<style type="text/css">
body {
    background-color: #FFF;
    margin: 30px;
    margin-top: 10px;
}
#contentContainer {
    width: 550px;
    height: 350px;
    border: 5px black solid;
    overflow: hidden;
    background-color: #F2F2F2;
    cursor: pointer;
}
#thing {
    position: relative;
    left: 50px;
    top: 50px;
    transition: left .5s ease-in, top .5s ease-in;
}
</style>
</head>

<body>
<div id="contentContainer">
    <img id="thing" src="//www.kirupa.com/images/smiley_red.png">
</div>

<script src="//www.kirupa.com/prefixfree.min.js"></script>
<script>
var theThing = document.querySelector("#thing");
var container = document.querySelector("#contentContainer");

container.addEventListener("click", getClickPosition, false);

function getClickPosition(e) {
    var parentPosition = getPosition(e.currentTarget);
    var xPosition = e.clientX - parentPosition.x - (theThing.clientWidth / 2);
    var yPosition = e.clientY - parentPosition.y - (theThing.clientHeight / 2);

    theThing.style.left = xPosition + "px";
    theThing.style.top = yPosition + "px";
}

// Helper function to get an element's exact position
function getPosition(el) {
  var xPos = 0;
  var yPos = 0;

  while (el) {
    if (el.tagName == "BODY") {
      // deal with browser quirks with body/window/document and page scroll
      var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
      var yScroll = el.scrollTop || document.documentElement.scrollTop;

      xPos += (el.offsetLeft - xScroll + el.clientLeft);
      yPos += (el.offsetTop - yScroll + el.clientTop);
    } else {
      // for all other non-BODY elements
      xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
      yPos += (el.offsetTop - el.scrollTop + el.clientTop);
    }

    el = el.offsetParent;
  }
  return {
    x: xPos,
    y: yPos
  };
}
</script>
</body>
</html>

I can then process these in the other script I have.然后我可以在我拥有的其他脚本中处理这些。

If someone can explain how this can be achieved it would be greatly appreciated.如果有人可以解释如何实现这一点,将不胜感激。

Thanks谢谢

I have looked through and found a solution if anyone is after doing the same thing.如果有人在做同样的事情后,我已经查看并找到了解决方案。

    <!DOCTYPE html>
    <html>

    <head>
    <title>Move to Click Position</title>
    <style type="text/css">
    body {
        background-color: #FFF;
        margin: 30px;
        margin-top: 10px;
    }
    #contentContainer {
        width: 614px;
        height: 864px;
        border: 5px black solid;
        overflow: hidden;
        background: url(Sample-Contract-Agreement-Letter.jpg) top left no-repeat;
        background-color: #F2F2F2;
        cursor: pointer;
    }
    #thing {
        position: relative;
        left: 50px;
        top: 50px;
        transition: left .5s ease-in, top .5s ease-in;
    }
    </style>
    </head>

    <body>
    <div id="contentContainer">
    <img id="thing" src="//www.kirupa.com/images/smiley_red.png">
</div>

<script src="//www.kirupa.com/prefixfree.min.js"></script>
<script>
    var theThing = document.querySelector("#thing");
    var container = document.querySelector("#contentContainer");

    container.addEventListener("click", getClickPosition, false);

    function getClickPosition(e) {
        var parentPosition = getPosition(e.currentTarget);
        var xPosition = e.clientX - parentPosition.x - (theThing.clientWidth / 2);
        var yPosition = e.clientY - parentPosition.y - (theThing.clientHeight / 2);
        document.getElementById('myField1').value = xPosition;
        document.getElementById('myField2').value = yPosition;
        theThing.style.left = xPosition + "px";
        theThing.style.top = yPosition + "px";
    }

    // Helper function to get an element's exact position
    function getPosition(el) {
      var xPos = 0;
      var yPos = 0;

      while (el) {
        if (el.tagName == "BODY") {
          // deal with browser quirks with body/window/document and page scroll
          var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
          var yScroll = el.scrollTop || document.documentElement.scrollTop;

          xPos += (el.offsetLeft - xScroll + el.clientLeft);
          yPos += (el.offsetTop - yScroll + el.clientTop);
        } else {
          // for all other non-BODY elements
          xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
          yPos += (el.offsetTop - el.scrollTop + el.clientTop);
        }

        el = el.offsetParent;
      }
      return {
        x: xPos,
        y: yPos
      };
    }
    </script>

    <form action="test.php" method="post">


    <input type=”hidden” value="" id="myField1" name="myField1">
    <input type=”hidden” value="" id="myField2" name="myField2">

    <input type="submit" value="Submit">

    </form>
    </body>
    </html>

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

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