简单实现谷歌地图的图片缩放 移动效果的JS

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

不支持矢量的缩放,只是一个大图的缩放和移动。多级地图的平滑缩放,我暂时还不会弄。

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <mce:script type="text/javascript" src="map.js" mce_src="map.js"></mce:script>  
  6. <title>老紫竹测试地图的缩放和拖拽</title>  
  7. </head>  
  8. <body>  
  9. <div id="top"><img src="images/top.jpg" mce_src="images/top.jpg"/></div>  
  10. <div id="menu">  
  11.     widthMax<div id="widthMax">1</div>  
  12.     heightMax<div id="heightMax">2</div>  
  13.     widthMap<div id="widthMap">3</div>  
  14.     heightMap<div id="heightMap">4</div>  
  15.     centerX<div id="centerX">5</div>  
  16.     centerY<div id="centerY">6</div>  
  17.     mousex<div id="mousex">7</div>  
  18.     mousey<div id="mousey">8</div>  
  19.     </div>  
  20. <div id="continer" style="position: absolute; left: 160px; top: 150px; width: 800px; height: 500px; overflow: hidden; z-index: 99; background-color: #DDDDFF" oncontextmenu="doRightClick();return false;" onmousedown="initMouseDrag()" onmouseup="stopdrag()" >  
  21. <div id="MAPLayer" style="position: absolute; left: 0px; top: 20px; z-index: 9; background-color: #FF0000"><img id="MAP" name="MAP" src="images/map.gif" mce_src="images/map.gif" oncontextmenu="doRightClick();return false;" onload="parent.init(this,document.getElementById('MAPLayer'))"  onmousewheel="scrollIt()"/></div>  
  22. </div>  
  23. </body>  
  24. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><mce:script type="text/javascript" src="map.js" mce_src="map.js"></mce:script><title>老紫竹测试地图的缩放和拖拽</title></head><body><div id="top"><img src="images/top.jpg" mce_src="images/top.jpg"/></div><div id="menu">    widthMax<div id="widthMax">1</div>    heightMax<div id="heightMax">2</div>    widthMap<div id="widthMap">3</div>    heightMap<div id="heightMap">4</div>    centerX<div id="centerX">5</div>    centerY<div id="centerY">6</div>    mousex<div id="mousex">7</div>    mousey<div id="mousey">8</div>    </div><div id="continer" style="position: absolute; left: 160px; top: 150px; width: 800px; height: 500px; overflow: hidden; z-index: 99; background-color: #DDDDFF" οncοntextmenu="doRightClick();return false;" οnmοusedοwn="initMouseDrag()" οnmοuseup="stopdrag()" ><div id="MAPLayer" style="position: absolute; left: 0px; top: 20px; z-index: 9; background-color: #FF0000"><img id="MAP" name="MAP" src="images/map.gif" mce_src="images/map.gif" οncοntextmenu="doRightClick();return false;" οnlοad="parent.init(this,document.getElementById('MAPLayer'))"  onmousewheel="scrollIt()"/></div></div></body></html>

 

唯一的js

[javascript] view plain copy print ?
  1. /** 
  2. * 老紫竹,模仿谷歌地图制作的图片缩放,拖动的效果js 
  3. * www.laozizhu.com 
  4. * 部分功能不能再Firefox下面用,有点可惜 
  5. */  
  6. var widthFrame = 800;  
  7. var heightFrame = 500;  
  8. var widthMax;  
  9. var heightMax;  
  10. var widthMap;  
  11. var heightMap;  
  12. var map;  
  13. var mapLayer;  
  14. var centerX;  
  15. var centerY;  
  16. var mousex;  
  17. var mousey;  
  18. function init(m,ml){  
  19.   map = m;    
  20.   map.onmousemove = mouseMove;  
  21.   mapLayer = ml;  
  22.   widthMax = map.width;  
  23.   heightMax = map.height;  
  24.   widthMap = map.width;  
  25.   heightMap = map.height;  
  26.   centerX = widthMap/2;  
  27.   centerY = heightMap/2;  
  28.   toCenter();  
  29. }  
  30. function showParam(){  
  31.   document.getElementById("widthMax").innerText=widthMax;  
  32.   document.getElementById("heightMax").innerText=heightMax;  
  33.   document.getElementById("widthMap").innerText=widthMap;  
  34.   document.getElementById("heightMap").innerText=heightMap;  
  35.   document.getElementById("centerX").innerText=centerX;  
  36.   document.getElementById("centerY").innerText=centerY;  
  37.   document.getElementById("mousex").innerText=mousex;  
  38.   document.getElementById("mousey").innerText=mousey;  
  39. }  
  40. function toCenter(){  
  41.   if(centerX< widthFrame/2){  
  42.     centerX = widthFrame/2;  
  43.   }  
  44.   if(centerX>widthMap-widthFrame/2){  
  45.     centerX = widthMap-widthFrame/2;  
  46.   }  
  47.   if(centerY< heightFrame/2){  
  48.     centerY = heightFrame/2;  
  49.   }  
  50.   if(centerY> heightMap-heightFrame/2){  
  51.     centerY = heightMap-heightFrame/2;  
  52.   }  
  53.   showParam();  
  54.   mapLayer.style.left=-1*(centerX-widthFrame/2)+"px";  
  55.   mapLayer.style.top=-1*(centerY-heightFrame/2)+"px";  
  56. }  
  57. function resizeTo(w,h){  
  58.   widthMap = map.width = w;  
  59.   heightMap = map.height = h;  
  60.   toCenter();  
  61. }  
  62. function bigger(){  
  63.   var w = widthMap*1.2;  
  64.   if(w>widthMax){  
  65.     w= widthMax;  
  66.   }    
  67.     
  68.   var h = heightMap*1.2;  
  69.   if(h>heightMax){  
  70.     h = heightMax;  
  71.   }  
  72.   centerX = centerX*w/widthMap;  
  73.   centerY = centerY*h/heightMap;  
  74.   resizeTo(w,h);  
  75. }  
  76. function smaller(){  
  77.   var w = widthMap*0.8;  
  78.   var h = heightMap*0.8;  
  79.   // 保证不会缩小到屏幕放不下了  
  80.   if(w<widthFrame){  
  81.     w = widthFrame;  
  82.     h = heightMax*w/widthMax; // 保证缩放比例  
  83.   }else if(h<heightFrame){  
  84.     h = heightFrame;  
  85.     w = widthMax*h/heightMax; // 保证缩放比例  
  86.   }  
  87.   centerX = centerX*w/widthMap;  
  88.   centerY = centerY*h/heightMap;  
  89.   resizeTo(w,h);  
  90. }  
  91. function doRightClick(){  
  92.   var x = mousex-300;  
  93.   var y = mousey-100;  
  94.   if(x>widthFrame/2+50){  
  95.     centerX+=20;  
  96.   }else if(x<widthFrame/2-50){  
  97.     centerX-=20;  
  98.   }  
  99.   if(y>heightFrame/2+50){  
  100.     centerY += 20;      
  101.   }else if(y<heightFrame/2-50){  
  102.     centerY -=20;  
  103.   }  
  104.   toCenter();  
  105. }  
  106. function moveButton(x,y){  
  107.   if(x==0 && y==0){  
  108.     centerX = widthMap/2;  
  109.     centerY = heightMap/2;  
  110.   }else{  
  111.     centerX += x;  
  112.     centerY += y;  
  113.   }  
  114.   toCenter();  
  115. }  
  116. function mouseMove(ev){  
  117.   ev = ev || window.event;  
  118.   var mousePos = mouseCoords(ev);  
  119.   mousex = mousePos.x;  
  120.   mousey = mousePos.y;  
  121.     //判断鼠标已被按下且onmouseover和onmousedown事件发生在同一对象上  
  122.   if(down&&event.srcElement==obj){  
  123.     centerX -= (mousex-x)*1;  
  124.     centerY -= (mousey-y)*1;  
  125.     toCenter();  
  126.     x = mousex;  
  127.     y = mousey;  
  128.   }  
  129.   showParam();  
  130. }  
  131. function mouseCoords(ev){  
  132.   if(ev.pageX || ev.pageY){  
  133.     return {x:ev.pageX, y:ev.pageY};  
  134.   }  
  135.   return {  
  136.     x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,  
  137.     y:ev.clientY + document.body.scrollTop - document.body.clientTop  
  138.   };  
  139. }  
  140. // 鼠标拖动效果  
  141. var x,y,z,down=false,obj;   
  142. function initMouseDrag(ev){  
  143.   ev = ev || window.event;  
  144.   obj=ev.srcElement       //事件触发对象  
  145.   obj.setCapture()              //设置属于当前对象的鼠标捕捉  
  146.   z=obj.style.zIndex            //获取对象的z轴坐标值  
  147.   //设置对象的z轴坐标值为100,确保当前层显示在最前面  
  148.   obj.style.zIndex=100  
  149.   x=mousex     //获取鼠标指针位置相对于触发事件的对象的X坐标  
  150.   y=mousey     //获取鼠标指针位置相对于触发事件的对象的Y坐标  
  151.   down=true           //布尔值,判断鼠标是否已按下,true为按下,false为未按下  
  152.   obj.style.cursor='move';  
  153. }  
  154. function moveit(){  
  155. }   
  156. function stopdrag(){  
  157.   //onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false  
  158.   down=false    
  159.   obj.style.zIndex=z         //还原对象的Z轴坐标值  
  160.   obj.releaseCapture() //释放当前对象的鼠标捕捉  
  161.   obj.style.cursor='normal';  
  162. }   
  163. var scrolling = false;  
  164. function scrollIt(){  
  165.   if(scrolling){  
  166.     return true;  
  167.   }  
  168.   scrolling = true;  
  169.   if(event.wheelDelta>0){  
  170.     bigger();  
  171.   }else{  
  172.     smaller();  
  173.   }    
  174.   scrolling = false;  
  175.   event.returnValue = false;  
  176. }  
/*** 老紫竹,模仿谷歌地图制作的图片缩放,拖动的效果js* www.laozizhu.com* 部分功能不能再Firefox下面用,有点可惜*/var widthFrame = 800;var heightFrame = 500;var widthMax;var heightMax;var widthMap;var heightMap;var map;var mapLayer;var centerX;var centerY;var mousex;var mousey;function init(m,ml){  map = m;    map.onmousemove = mouseMove;  mapLayer = ml;  widthMax = map.width;  heightMax = map.height;  widthMap = map.width;  heightMap = map.height;  centerX = widthMap/2;  centerY = heightMap/2;  toCenter();}function showParam(){  document.getElementById("widthMax").innerText=widthMax;  document.getElementById("heightMax").innerText=heightMax;  document.getElementById("widthMap").innerText=widthMap;  document.getElementById("heightMap").innerText=heightMap;  document.getElementById("centerX").innerText=centerX;  document.getElementById("centerY").innerText=centerY;  document.getElementById("mousex").innerText=mousex;  document.getElementById("mousey").innerText=mousey;}function toCenter(){  if(centerX< widthFrame/2){    centerX = widthFrame/2;  }  if(centerX>widthMap-widthFrame/2){    centerX = widthMap-widthFrame/2;  }  if(centerY< heightFrame/2){    centerY = heightFrame/2;  }  if(centerY> heightMap-heightFrame/2){    centerY = heightMap-heightFrame/2;  }  showParam();  mapLayer.style.left=-1*(centerX-widthFrame/2)+"px";  mapLayer.style.top=-1*(centerY-heightFrame/2)+"px";}function resizeTo(w,h){  widthMap = map.width = w;  heightMap = map.height = h;  toCenter();}function bigger(){  var w = widthMap*1.2;  if(w>widthMax){    w= widthMax;  }      var h = heightMap*1.2;  if(h>heightMax){    h = heightMax;  }  centerX = centerX*w/widthMap;  centerY = centerY*h/heightMap;  resizeTo(w,h);}function smaller(){  var w = widthMap*0.8;  var h = heightMap*0.8;  // 保证不会缩小到屏幕放不下了  if(w<widthFrame){    w = widthFrame;    h = heightMax*w/widthMax; // 保证缩放比例  }else if(h<heightFrame){    h = heightFrame;    w = widthMax*h/heightMax; // 保证缩放比例  }  centerX = centerX*w/widthMap;  centerY = centerY*h/heightMap;  resizeTo(w,h);}function doRightClick(){  var x = mousex-300;  var y = mousey-100;  if(x>widthFrame/2+50){    centerX+=20;  }else if(x<widthFrame/2-50){    centerX-=20;  }  if(y>heightFrame/2+50){    centerY += 20;      }else if(y<heightFrame/2-50){    centerY -=20;  }  toCenter();}function moveButton(x,y){  if(x==0 && y==0){    centerX = widthMap/2;    centerY = heightMap/2;  }else{    centerX += x;    centerY += y;  }  toCenter();}function mouseMove(ev){  ev = ev || window.event;  var mousePos = mouseCoords(ev);  mousex = mousePos.x;  mousey = mousePos.y;    //判断鼠标已被按下且onmouseover和onmousedown事件发生在同一对象上  if(down&&event.srcElement==obj){    centerX -= (mousex-x)*1;    centerY -= (mousey-y)*1;    toCenter();    x = mousex;    y = mousey;  }  showParam();}function mouseCoords(ev){  if(ev.pageX || ev.pageY){    return {x:ev.pageX, y:ev.pageY};  }  return {    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,    y:ev.clientY + document.body.scrollTop - document.body.clientTop  };}// 鼠标拖动效果var x,y,z,down=false,obj; function initMouseDrag(ev){  ev = ev || window.event;  obj=ev.srcElement       //事件触发对象  obj.setCapture()              //设置属于当前对象的鼠标捕捉  z=obj.style.zIndex            //获取对象的z轴坐标值  //设置对象的z轴坐标值为100,确保当前层显示在最前面  obj.style.zIndex=100  x=mousex     //获取鼠标指针位置相对于触发事件的对象的X坐标  y=mousey     //获取鼠标指针位置相对于触发事件的对象的Y坐标  down=true           //布尔值,判断鼠标是否已按下,true为按下,false为未按下  obj.style.cursor='move';}function moveit(){} function stopdrag(){  //onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false  down=false    obj.style.zIndex=z         //还原对象的Z轴坐标值  obj.releaseCapture() //释放当前对象的鼠标捕捉  obj.style.cursor='normal';} var scrolling = false;function scrollIt(){  if(scrolling){    return true;  }  scrolling = true;  if(event.wheelDelta>0){    bigger();  }else{    smaller();  }    scrolling = false;  event.returnValue = false;}

 

用到的地图,比较大,大家自己随便找一个大图,测试看看好了!

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值