javascript 读出对象的位置并通过 onclick 显示,或保存在变量中

我正在尝试读出对象的 x 和 y 位置。理想情况下,应该有一个“onclick”按钮,可以单击该按钮,然后读出并显示位置。

这是代码和网站: https: //www.unisg.bplaced.net/JS/dragdrop3.html

我想做的事情是

  • 读出位置并将其保存在变量中

  • 甚至在警报框中显示它们也会很棒。但是,我最终需要将它们保存在我的调查程序中。


海绵宝宝撒
浏览 132回答 1
1回答

千万里不及你

只需指定 id 的属性之一dragMe,因为您已在上面的 css 中将位置指定为绝对位置,并在元素的内联 css 中将位置指定为相对位置。并获取位置px添加代码为<html><head>&nbsp; &nbsp; <style>&nbsp; &nbsp; #container {&nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; height: 400px;&nbsp; &nbsp; &nbsp; background-image: url('http://www.unisg.bplaced.net/park2.png');&nbsp; &nbsp; &nbsp; background-repeat: no-repeat;&nbsp; &nbsp; &nbsp; background-size: contain;&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; border-radius: 7px;&nbsp; &nbsp; &nbsp; touch-action: none;&nbsp; &nbsp; }&nbsp; &nbsp; #dragMe {&nbsp; &nbsp; &nbsp; width: 100px;&nbsp; &nbsp; &nbsp; height: 300px;&nbsp; &nbsp; &nbsp; background-image: url('http://www.unisg.bplaced.net/silhoutte3.png');&nbsp; &nbsp; &nbsp; background-size: 100px 300px;&nbsp; &nbsp; &nbsp; background-repeat: no-repeat;&nbsp; &nbsp; &nbsp; &nbsp; border-radius: 2em 2em 2em 2em;&nbsp; &nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; &nbsp; top: 10%;&nbsp; &nbsp; &nbsp; &nbsp; left: 20%;&nbsp; &nbsp; }&nbsp; &nbsp; #dropMe {&nbsp; &nbsp; &nbsp; &nbsp; width: 12em;&nbsp; &nbsp; &nbsp; &nbsp; height: 12em;&nbsp; &nbsp; &nbsp; &nbsp; padding: 0.5em;&nbsp; &nbsp; &nbsp; &nbsp; margin: 0 auto;&nbsp; &nbsp; }&nbsp; &nbsp; </style>&nbsp; &nbsp; <script src="/scripts/snippet-javascript-console.min.js?v=1"></script><style type="text/css">.as-console-wrapper { position: fixed; bottom: 0; left: 0; right: 0; max-height: 150px; overflow-y: scroll; overflow-x: hidden; border-top: 1px solid #000; display: none; }.as-console { background: #e9e9e9; border: 1px solid #ccc; display: table; width: 100%; border-collapse: collapse; }.as-console-row { display: table-row; font-family: monospace; font-size: 13px; }.as-console-row:after { display: table-cell; padding: 3px 6px; color: rgba(0,0,0,.35); border: 1px solid #ccc; content: attr(data-date); vertical-align: top; }.as-console-row + .as-console-row > * { border: 1px solid #ccc; }.as-console-row-code { width: 100%; white-space: pre-wrap; padding: 3px 5px; display: table-cell; font-family: monospace; font-size: 13px; vertical-align: middle; }.as-console-error:before { content: 'Error: '; color: #f00; }.as-console-assert:before { content: 'Assertion failed: '; color: #f00; }.as-console-info:before { content: 'Info: '; color: #00f; }.as-console-warning:before { content: 'Warning: '; color: #e90 }@-webkit-keyframes flash { 0% { background: rgba(255,240,0,.25); } 100% { background: none; } }@-moz-keyframes flash { 0% { background: rgba(255,240,0,.25); } 100% { background: none; } }@-ms-keyframes flash { 0% { background: rgba(255,240,0,.25); } 100% { background: none; } }@keyframes flash { 0% { background: rgba(255,240,0,.25); } 100% { background: none; } }.as-console-row-code, .as-console-row:after { -webkit-animation: flash 1s; -moz-animation: flash 1s; -ms-animation: flash 1s; animation: flash 1s; }</style></head><body>&nbsp; &nbsp; <script src="https://code.jquery.com/jquery-1.12.4.js"></script><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script><div id="container"><div id="dragMe" class="ui-draggable ui-draggable-handle" style="position: relative; "><p>You</p></div><div id="dropMe"></div>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; $('#dragMe').draggable(&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; containment: $('body'),&nbsp; &nbsp; &nbsp; &nbsp; drag: function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var position = $(this).position();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xPos = position.left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var yPos = position.top;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#positionX').text('positionX: ' + xPos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#positionY').text('positionY: ' + yPos);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accept: '#dragMe',&nbsp; &nbsp; &nbsp; &nbsp; over : function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).animate({'border-width' : '5px',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'border-color' : '#0f0'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, 500);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#dragThis').draggable('option','containment',$(this));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });$(document).ready(function(){&nbsp; $("button").click(function(){&nbsp; &nbsp; var x = $("#dragMe").position();&nbsp; &nbsp; alert("Top position: " + x.top + " Left position: " + x.left);&nbsp; });});&nbsp; &nbsp; </script><button>Click me</button><div class="as-console-wrapper"><div class="as-console"></div></div></body></html>这里的 jQuery 代码将为您提供所需的职位。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5