通过鼠标单击将X Y坐标转换为PHP变量

时间:2019-04-30 04:42:42

标签: javascript php variables

以下脚本将球从盒子中的一个位置移动到另一个位置。

我想收集在此框中单击鼠标的位置的坐标,并将onclick上方的X和Y坐标转换为PHP变量,以便一些其他PHP代码可以处理此问题。

请问如何完成?

<!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>

然后我可以在其他脚本中对其进行处理。

如果有人可以解释如何实现这一目标,将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

我仔细检查了一下,找到了解决方案,如果有人在做同样的事情之后。

    <!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>