不过,这样只是直线是吧?
如果是按照路线呢?电子地图.
http://www./
<script>
var x1,y1,x2,y2,ju,k,top,left,dd,ee;
var x,y,yun,woyun;
function ccc(){
// if(k>1) k=0;
//if(k==0){
x1=event.clientX-left;
y1=event.clientY-top;
//}
// else
//{
//x2=event.clientX;
//y2=event.clientY;
//}
//if(k==1)
//{
yun=10000;
ss= new Array(5)
kk= new Array(5)
for(i=0;i<5;++i)
{
ss[i]=100+i*100;
kk[i]=100+i*120;
x=Math.abs(ss[i]-x1);
y=Math.abs(kk[i]-y1);
//alert(ss[i]);
//alert(kk[i]);
if(yun>Math.sqrt(x*x+y*y))
{
dd=ss[i];
ee=kk[i];
yun=Math.sqrt(x*x+y*y);
}
}
alert("您选择的点坐标是("+x1+","+y1+")");
alert("距离选择点最近的已知点坐标是("+dd+","+ee+")");
alert("两点之间距离为("+yun+")");
//alert("两点坐标:("+x1+","+y1+")("+x2+","+y2+")\n两点距离:"+ju+"像素");
//x1=0;y1=0;//x2=0;y2=0;
//}
//k++;
}
//格式化数字函数
</script>
<style type="text/css">
<!--
#d1 {
position:absolute;
width:3;
height:3;
z-index:1;
background-color: #0066ff;
overflow: hidden;
}
#d2 {
position:absolute;
width:3;
height:3;
z-index:2;
background-color: #FF0000;
overflow: hidden;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<script type="text/javascript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script><div id="d1" style="display:none"></div>
<div id="d2" style="display:none"></div>
<img src="http://www.no1edu.com/image/bj1.jpg" width="100%" height="100%" id="pic" onclick="return ccc()" />
<script>
top=document.getElementById("pic").offsetTop;
left=document.getElementById("pic").offsetLeft;
</script>
[此贴子已经被作者于2006-10-16 15:54:09编辑过]
上面是比较准确的,没有应用formatNumber函数,下面应用formatNumber的在我这里不能得到正常的结果.
<script>
var x1,y1,x2,y2,ju,k,top,left,dd,ee;
var x,y,yun,woyun;
function ccc(){
// if(k>1) k=0;
//if(k==0){
x1=event.clientX-left;
y1=event.clientY-top;
//}
// else
//{
//x2=event.clientX;
//y2=event.clientY;
//}
//if(k==1)
//{
yun=10000;
ss= new Array(5)
kk= new Array(5)
for(i=0;i<5;++i)
{
ss[i]=kk[i]=100+i*100;
x=Math.abs(ss[i]-x1);
y=Math.abs(kk[i]-y1);
//alert(ss[i]);
//alert(kk[i]);
if(yun>formatNumber(Math.sqrt(x*x+y*y),"#,###.00"))
{
dd=ss[i];
ee=kk[i];
yun=formatNumber(Math.sqrt(x*x+y*y),"#,###.00");
}
}
alert("您选择的点坐标是("+x1+","+y1+")");
alert("距离选择点最近的已知点坐标是("+dd+","+ee+")");
alert("两点之间距离为("+yun+")");
//alert("两点坐标:("+x1+","+y1+")("+x2+","+y2+")\n两点距离:"+ju+"像素");
//x1=0;y1=0;//x2=0;y2=0;
//}
//k++;
}
//格式化数字函数
function formatNumber(number,pattern){
var str = number.toString();
var strInt;
var strFloat;
var formatInt;
var formatFloat;
if(/\./g.test(pattern)){
formatInt = pattern.split('.')[0];
formatFloat = pattern.split('.')[1];
}else{
formatInt = pattern;
formatFloat = null;
} if(/\./g.test(str)){
if(formatFloat!=null){
var tempFloat = Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);
strInt = (Math.floor(number)+Math.floor(tempFloat)).toString();
strFloat = /\./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';
}else{
strInt = Math.round(number).toString();
strFloat = '0';
}
}else{
strInt = str;
strFloat = '0';
}
if(formatInt!=null){
var outputInt = '';
var zero = formatInt.match(/0*$/)[0].length;
var comma = null;
if(/,/g.test(formatInt)){
comma = formatInt.match(/,[^,]*/)[0].length-1;
}
var newReg = new RegExp('(\\d{'+comma+'})','g'); if(strInt.length<zero){
outputInt = new Array(zero+1).join('0')+strInt;
outputInt = outputInt.substr(outputInt.length-zero,zero)
}else{
outputInt = strInt;
} var
outputInt = outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1')
outputInt = outputInt.replace(/^,/,''); strInt = outputInt;
} if(formatFloat!=null){
var outputFloat = '';
var zero = formatFloat.match(/^0*/)[0].length; if(strFloat.length<zero){
outputFloat = strFloat+new Array(zero+1).join('0');
//outputFloat = outputFloat.substring(0,formatFloat.length);
var outputFloat1 = outputFloat.substring(0,zero);
var outputFloat2 = outputFloat.substring(zero,formatFloat.length);
outputFloat = outputFloat1+outputFloat2.replace(/0*$/,'');
}else{
outputFloat = strFloat.substring(0,formatFloat.length);
} strFloat = outputFloat;
}else{
if(pattern!='' || (pattern=='' && strFloat=='0')){
strFloat = '';
}
} return strInt+(strFloat==''?'':'.'+strFloat);
}
</script>
<style type="text/css">
<!--
#d1 {
position:absolute;
width:3;
height:3;
z-index:1;
background-color: #0066ff;
overflow: hidden;
}
#d2 {
position:absolute;
width:3;
height:3;
z-index:2;
background-color: #FF0000;
overflow: hidden;
}
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<script type="text/javascript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script><div id="d1" style="display:none"></div>
<div id="d2" style="display:none"></div>
<img id="pic" src="http://www.no1edu.com/image/bj1.jpg" width="100%" onclick="return ccc()" />
<script>
top=document.getElementById("pic").offsetTop;
left=document.getElementById("pic").offsetLeft;
</script>
[此贴子已经被作者于2006-10-16 15:58:28编辑过]