CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  JavaScript

如何用鼠标左键拖动一张图片,松开左键后图片停留在鼠标位置

楼主minlove99(浪花里摇滚的狼)2006-03-12 19:55:29 在 Web 开发 / JavaScript 提问

问题,谢谢各位 问题点数:20、回复次数:5Top

1 楼kangqin(小康)回复于 2006-03-12 22:01:27 得分 2

图片放在DIV里,跟着mousemove事件走;放开的时候解除。Top

2 楼brothercat(猫猫 ^_^)回复于 2006-03-16 16:12:36 得分 3

这种代码很多了,参考  
   
  http://js.hrw.cn/picture20.htmTop

3 楼clare2003(忘情火)回复于 2006-03-16 17:11:42 得分 10

<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN">  
  <HTML>  
  <HEAD>  
  <TITLE>   Document   </TITLE>  
  <style   type="text/css">  
  body{  
  padding   :   0px   ;  
  margin   :   0px;  
  background-color   :   #999999   ;  
   
  }  
  .DivMain   {  
  position   :   absolute   ;  
  text-align   :   center   ;  
  overflow   :   hidden   ;  
  }  
  .DivMenu   {  
  position   :   absolute   ;  
  text-align   :   left   ;  
  overflow   :   hidden   ;  
  }  
  .ImgMain   {  
  position   :   absolute   ;  
  overflow   :   hidden   ;  
  border   :   0px   ;  
  }  
  .TBMain   {  
  background-color   :   #CCCCCC   ;  
  padding   :   0px   ;  
  border   :   0px   ;  
  z-index   :   99   ;  
  line-height   :   18px   ;  
   
  }  
  .TBMenu   {  
  background-color   :   #FFCCCC   ;  
  padding   :   0px   ;  
  border   :   0px   ;  
  z-index   :   99   ;  
  color:   #000000   ;  
   
  }  
  .HrScroll   {  
  margin   :   0px;  
  background-color   :   #FFFFFF   ;  
  padding   :   0px   ;  
  border   :   0px   ;  
  z-index   :   99   ;  
  color   :   #000000   ;  
  border-spacing   :   0px;  
  width   :   160px   ;  
  height   :   10px   ;  
  }  
  </style>  
   
  <SCRIPT   LANGUAGE="JavaScript">  
  <!--  
   
  var   originalWidth   ;  
  var   originalHeight   ;  
   
  var   objDivMain   ;  
  var   objTbMain   ;  
   
  var   objImg   ;  
  var   ObjDrag   ;  
   
  var   objDivMenu   ;  
  var   objTbMemu   ;  
   
  var   objbtnImgInfo   ;  
   
  var   objDivFlag;  
  var   objHrScroll;  
   
  var   oRcts   ;  
  var   oTextRange   ;      
   
  function   setInit(obj)   {  
   
  objDivMain   =   document.getElementById("divMain")   ;  
  objTbMain   =     document.getElementById("TbMain")   ;  
   
  objDivMenu   =   document.getElementById("DivMenu")   ;  
  objTbMenu   =     document.getElementById("TbMenu")   ;  
   
  objImg   =   document.getElementById("imgMain")   ;  
  objbtnImgInfo   =   document.getElementById("btnImgInfo")   ;  
   
  objDivFlag   =   document.getElementById("DivFlag");  
  objHrScroll   =   document.getElementById("HrScroll");  
   
  originalWidth   =   obj.width;  
  originalHeight   =   obj.height;  
   
  objImg.style.width   =   originalWidth;  
  objImg.style.height   =   originalHeight;  
   
  fmImgZoom.FitScreen.click();  
  }  
   
  function   setFitScreen()   {  
   
  initImg(objImg);  
  }  
   
  function   setOriginal()   {  
   
  var   intBodyWidth   ;  
  var   intBodyHeight   ;  
   
  intBodyWidth   =   document.body.clientWidth   ;  
  intBodyHeight   =   document.body.clientHeight   ;  
   
  objImg.style.left   =   0   ;  
  objImg.style.top   =   0   ;  
  objImg.style.width   =   originalWidth;  
  objImg.style.height   =   originalHeight;  
  objImg.style.zoom   =   1   ;  
   
  objDivMain.style.width   =   intBodyWidth   ;  
  objDivMain.style.height   =   intBodyHeight   -   objTbMain.clientHeight;  
   
  fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);  
  }  
   
  function   initImg(objCurrent)   {  
   
  var   intBodyWidth   ;  
  var   intBodyHeight   ;  
   
  intBodyWidth   =   document.body.clientWidth   ;  
  intBodyHeight   =   document.body.clientHeight   ;  
   
   
  objTbMain.style.top   =   0;  
  objTbMain.style.height   =   20;  
  objTbMain.style.width   =   intBodyWidth;  
   
  objDivMain.style.left   =   0   ;  
  objDivMain.style.height   =   intBodyHeight   -   objTbMain.clientHeight;  
  objDivMain.style.width   =   intBodyWidth   ;  
   
  initZoom(objCurrent);  
  }  
   
  function   initZoom(obj){  
   
  var   intObjWidth   ;  
  var   intObjHeight   ;  
  var   intDivHeight   ;  
  var   intZoomRatio   ;  
   
   
  intDivHeight   =   objDivMain.style.pixelHeight;  
  intObjHeight   =   obj.style.pixelHeight;  
  intZoomRatio   =   intDivHeight   /   intObjHeight;  
   
  obj.style.zoom   =   intZoomRatio   ;  
  obj.style.top   =   0;  
  obj.style.left   =   0;  
   
  fnWritePos   (obj.style.left,obj.style.top,obj.style.zoom)   ;  
  }  
   
  function   fnMouseDown(obj)   {  
  ObjDrag=obj;  
  ObjDrag.setCapture();  
  ObjDrag.l=event.x-ObjDrag.style.pixelLeft;  
  ObjDrag.t=event.y-ObjDrag.style.pixelTop;  
  }  
   
  function   fnMouseMove()   {  
  if(ObjDrag!=null)   {  
  ObjDrag.style.left   =   event.x-ObjDrag.l;  
  ObjDrag.style.top   =   event.y-ObjDrag.t;  
  fnWritePos(ObjDrag.style.left,ObjDrag.style.top,ObjDrag.style.zoom);  
  }  
  }  
   
  function   fnMouseUp()   {  
  if(ObjDrag!=null)   {  
  ObjDrag.releaseCapture();  
  ObjDrag=null;  
  }  
  }  
   
  function   fnZoomIn()   {  
   
  objImg.style.zoom   =   parseFloat(objImg.style.zoom)   +   0.01   ;  
  fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);  
  }  
   
  function   fnZoomOut()   {  
   
  if(objImg.style.zoom>0.01){  
  objImg.style.zoom   =   parseFloat(objImg.style.zoom)   -   0.01   ;  
  fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);  
  }  
  }  
   
  function   fnDivMouseDown()   {  
  if   (   event.button   ==   2   &&   event.srcElement.id   !=   "imgMain")   {  
  objDivMenu.style.top   =   event.y;  
  objDivMenu.style.left   =   event.x;  
  objDivMenu.style.visibility   =   "";  
   
  oTextRange   =   objHrScroll.createTextRange()   ;  
  oRcts   =   oTextRange.getClientRects();  
  oBndRct   =   objDivFlag.getBoundingClientRect();  
   
   
  objDivFlag.style.top   =   oRcts[0].top;  
  objDivFlag.style.left   =   oRcts[0].left   +   parseInt(objImg.style.zoom   *   10)   ;  
  objDivFlag.style.visibility   =   "";  
   
  }  
  if   (   event.button   ==   1   &&   event.srcElement.id   !=   "imgMain")   {  
  objDivMenu.style.visibility   =   "hidden";  
  objDivFlag.style.visibility   =   "hidden";  
  }  
  }  
   
  function   fnWritePos(intLeft,intTop,intZoom)   {  
  objbtnImgInfo.innerText   =   "   ImgLeft:"   +   intLeft   +   "   ImgTop:"   +   intTop   +   "   ImgZoom:"   +   intZoom   ;  
  }  
   
  function   fnSlipMouseDown(obj)   {  
  ObjDrag=obj;  
  ObjDrag.setCapture();  
  ObjDrag.l   =   event.x   -   ObjDrag.style.pixelLeft;  
  ObjDrag.t   =   ObjDrag.style.pixelTop;  
  }  
   
  function   fnSlipMouseMove()   {  
  if(ObjDrag!=null)   {  
   
  if(event.x   >=   oRcts[0].left   &&   event.x   <=   oRcts[0].right   ){  
  ObjDrag.style.left   =   event.x   -   objDivFlag.offsetWidth   /   2;  
  }  
   
  ObjDrag.t   =   ObjDrag.style.pixelTop;  
  var   intCurrentPos   =   ObjDrag.style.pixelLeft   -   oRcts[0].left   +   objDivFlag.offsetWidth   /   2;  
  var   intPicZoom   =   intCurrentPos   /   10;  
  objImg.style.zoom   =   intPicZoom;  
  fnWritePos(objImg.style.left,objImg.style.top,objImg.style.zoom);  
  }  
  }  
   
  function   fnSlipMouseUp()   {  
  if(ObjDrag!=null)   {  
  ObjDrag.releaseCapture();  
  ObjDrag=null;  
  }  
  }  
   
   
  function   setDisplay()   {  
   
  if(objImg.style.visibility   ==   ""){  
  objImg.style.visibility   =   "hidden";  
  }  
  else{  
  objImg.style.visibility   =   "";  
  }  
  }  
   
  function   fnContextMenu()   {  
  window.event.returnValue=false;  
  }  
  //-->  
  </SCRIPT>  
  </HEAD>  
   
  <BODY   oncontextmenu="fnContextMenu()">  
  <form   name="fmImgZoom">  
  <TABLE   id="tbMain"   class="TBMain">  
  <TR>  
  <TD>  
  <input   type="button"   name="ZoomIn"   value="ZoomIn"   onclick="fnZoomIn()">  
  <input   type="button"   name="ZoomOut"   value="ZoomOut"   onclick="fnZoomOut()">  
  <input   type="button"   name="Original"   value="Original"   onclick="setOriginal()">  
  <input   type="button"   name="FitScreen"   value="FitScreen"   onclick="setFitScreen()">  
  <input   type="button"   name="btnView"   value="hide/view"   onclick="setDisplay()">  
  <input   id="btnImgInfo"   size="50">  
  </TD>  
  </TR>  
  </TABLE>  
  <div   id="divMain"   class="DivMain"   onmousedown="fnDivMouseDown()"><img   src="http://www.800ems.com/desktop/out1/82_jpg.jpg"   id="imgMain"   class="imgMain"   onload="setInit(this)"   onmousedown="fnMouseDown(this)"   onmousemove="fnMouseMove()"   onmouseup="fnMouseUp()"   ></div>  
  <div   id="divMenu"   class="DivMenu"   style="visibility:hidden">  
  <TABLE   class="TBMenu">  
  <TR>  
  <TD>10%</TD>  
  <TD><button   id="HrScroll"   class="HrScroll"><hr   noshade></button>   </TD>  
  <TD>1600%</TD>  
  </TR>  
  </TABLE>  
  </div>  
  <DIV   ID="DivFlag"   STYLE="position:absolute;visibility:hidden;width:1px;height:10px;z-index:99"     onmousedown="fnSlipMouseDown(this)"   onmousemove="fnSlipMouseMove()"   onmouseup="fnSlipMouseUp()"   onmouseover="this.style.cursor   ='hand'"   onmouseout="this.style.cursor=''">↑</DIV>  
  </form>  
  </BODY>  
  </HTML>  
  Top

4 楼superdullwolf(超级大笨狼,每天要自强,MVP)回复于 2006-03-16 17:22:23 得分 5

<div   style='position:absolute;width:200;height:200;background:red'   onmousedown=MouseDown(this)   onmousemove=MouseMove()   onmouseup=MouseUp()><table   border=1><tr><Td>adsf</table></div>  
  <script>  
  var   Obj  
  function   MouseDown(obj){  
  Obj=obj  
  Obj.setCapture()  
  Obj.l=event.x-Obj.style.pixelLeft  
  Obj.t=event.y-Obj.style.pixelTop  
  }  
  function   MouseMove(){  
  if(Obj!=null){  
  Obj.style.left   =   event.x-Obj.l  
  Obj.style.top   =   event.y-Obj.t  
  }  
  }  
  function   MouseUp(){  
  if(Obj!=null){  
  Obj.releaseCapture()  
  Obj=null  
  }  
  }  
  </script>  
  Top

5 楼minlove99(浪花里摇滚的狼)回复于 2006-03-17 13:30:52 得分 0

谢谢各位了Top

相关问题

  • 请问:按着左键拖动鼠标就算拖动鼠标吗?
  • 在窗体中按下鼠标左键拖动窗体代码???
  • 鼠标左键按下拖动是对应的windows消息是哪个?(像WM_LBUTTONDOWE)
  • 如何禁止DBChart在鼠标左键和右键拖动的时候改变位置?
  • 在按下鼠标左键拖拉Treeview里的节点时,如何让Treeview的滚动条跟着鼠标拖动的方向一起滚动?
  • 在ClistCtrl中用鼠标框选多行的事件是什么?(就像在资源管理器中点左键或右键拖动鼠标,选中多行)
  • ListBox中的项目,如何使其在鼠标按下后,拖动,松开鼠标,将此项目的位置改变
  • 屏蔽shift+鼠标左键
  • 禁用鼠标左键,
  • 鼠标拖动的问题

关键词

得分解答快速导航

  • 帖主:minlove99
  • kangqin
  • brothercat
  • clare2003
  • superdullwolf

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo