大家好, 我有点挑战. 如果您单击图像,我有一个脚本显示鼠标的辅导. 现在,我想将这些坐标转换为PixelNumber. 让我看看是否可以使它变得更容易. 假设我们的图像高100px和100px宽. 左上角的坐标为x = 0,y = 0 右上角的坐标为x = 99,y = 0 左下角的坐标为x = 0,y = 99 左上角的坐标为x = 99,y = 99 现在,我想通过它们代表的像素以另一种方式显示这些坐标. 这意味着: 左上角的像素名字为1 右上角的像素名称为100 左下角的Pixelnumber为9901 左上角的像素名称为10000 因此,通过使用坐标,我敢肯定可以计算这些PixelNumbers. 但是,我无法自己写这篇文章. 谁能帮我这个? 这是我到目前为止的代码: [html] 功能point_it(event){ var pointer_div = document.getElementById(" pointer_div"); //IE if(window.activexobject){ pos_x = window.event.offsetx; pos_y = window.event.offsety; } // Firefox >>>仍将检查!! 别的 { var top = 0,左= 0; var elm = pointer_div; 而(榆树){ left = elm.offsetleft; top = elm.offsettop; 榆树= elm.offsetparent; } pos_x = event.pagex-左; pos_y = event.pagey-顶部; } document.pointform.form_x.value = pos_x; document.pointform.form_y.value = pos_y; document.pointform.form_pixel.value = pixel; } 坐标 x = y = 像素= [/html]
# 回答1 嗨,大家好, 又是我. 我以为我永远不会弄清楚该怎么做. 直到我意识到我从错误的角度看待它. 我只需要找到计算像素的公式即可. 因此,我从Excel开始尝试找到正确的公式. 一旦找到一个,我只需要将其转换为JavaScript即可. 您知道它实际上有效! 因此,我怀疑是否有人真的需要这样的脚本,但是无论如何,这里是:
- <html>
- <head>
- <script language="JavaScript">
-
- function point_it(event){
- var pointer_div = document.getElementById("pointer_div");
- var pixel;
- var imagewidth="516";
- var calc;
- var top = 0, left = 0;
- var elm = pointer_div;
-
- //IE
- if (window.ActiveXObject) {
- pos_x = window.event.offsetX;
- pos_y = window.event.offsetY;
- }
-
- //Firefox >>> still to be checked!!
- else {
- while (elm) {
- left = elm.offsetLeft;
- top = elm.offsetTop;
- elm = elm.offsetParent;
- }
- pos_x = event.pageX - left;
- pos_y = event.pageY - top;
- }
-
- if (pos_y <= "0"){
- calc = (pos_y)
- }
- else{
- calc = (parseInt(pos_y)*parseInt(imagewidth) - parseInt(pos_y))
- }
-
- pixel = (parseInt(pos_y) + parseInt(pos_x+1)) + calc;
-
-
-
- // Debugging
- document.pointform.form_x.value = pos_x + 1;
- document.pointform.form_y.value = pos_y + 1;
- document.pointform.form_pixel.value = pixel;
- }
-
- </script>
- </head>
- <body>
- <form name="pointform" method="post">
- <div id="pointer_div" onclick="point_it(event)" style = "background-image:url('http://www.nasa.gov/externalflash/NASA45/27/27image.jpg');width:516px;height:387px; CURSOR: crosshair"></div>
-
- <!-- Debugging -->
- Coordinates<br>
- x = <input type="text" name="form_x" size="4">
- y = <input type="text" name="form_y" size="4">
- <br>
- pixel = <input type="text" name="form_pixel" size="8">
- </form>
- </body>
- </html>
-
祝你们好运!