JavaScript拖拽图片一

本文演示如何将一张图片用鼠标拖拽到某个位置。

先看html代码:

[html]   view plain copy print ?
  1. <html>  
  2.   <head>  
  3.     <title>drag demo</title>  
  4.     <script src="js/drag.js" type="text/javascript"></script>  
  5.   </head>  
  6.   <body onload="init()">  
  7.     <div id="icon1"   
  8.      onmousedown="mouseDown(event)"  
  9.      style="left:1px; top: 1px; position: absolute;">  
  10.       <img alt="" border="0" src="/img/2.jpg"></img>  
  11.     </div>  
  12.   </body>  
  13. </html>  


注意,必须在style中指定三个属性:

left,top属性值可以为任意有效数值,position必须是absolute

为了便于扩展,将图片包装在div中,下面的JavaScript代码其实是拖动div。


然后看看drag.js文件:

[javascript]   view plain copy print ?
  1. function init() {  
  2.     window.document.onmousemove = mouseMove;  
  3.     window.document.onmouseup = mouseUp;  
  4.     window.document.ondragstart = mouseStop;  
  5. }  
  6.   
  7. function mouseDown(e) {  
  8.     window.dragObj = e.currentTarget;  
  9.     if (window.dragObj !== null) {  
  10.         window.clickLeft = window.event.x - parseInt(dragObj.style.left);  
  11.         window.clickTop = window.event.y - parseInt(dragObj.style.top);  
  12.         window.dragObj.style.zIndex += 1;  
  13.     }  
  14. }  
  15.   
  16. function mouseStop() {  
  17.     window.event.returnValue = false;  
  18. }  
  19.   
  20. function mouseMove() {  
  21.     if (window.dragObj !== null ) {  
  22.         window.dragObj.style.left = window.event.x - window.clickLeft;  
  23.         window.dragObj.style.top = window.event.y - window.clickTop;  
  24.     }  
  25. }  
  26.   
  27. function mouseUp() {  
  28.     window.dragObj = null;  
  29. }  

ondragStart事件被禁用。

主要就是mouseDown,mouseMove和mouseUp三个函数。

注意window.clickLeft和window.clickTop,它们是在点击图片的时候计算的,代表鼠标在图片里的位置(距离图片左上角的x和y值)。

它们用来保证在移动后鼠标仍然停留在图片的相对位置,通过将鼠标的位置减去这两个偏移值。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题我可以回答。以下是一个简单的HTML和JavaScript实现图片放大缩小和拖拽的示例: ```html <!DOCTYPE html> <html> <head> <title>Image Zoom and Drag</title> <style> #image-container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 1px solid black; overflow: hidden; cursor: move; } #image-container img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <div id="image-container"> <img src="https://picsum.photos/id/237/300/300" id="image"> </div> <script> var image = document.getElementById("image"); var imageContainer = document.getElementById("image-container"); var isDragging = false; var lastX, lastY; imageContainer.addEventListener("mousedown", function(e) { isDragging = true; lastX = e.clientX; lastY = e.clientY; }); imageContainer.addEventListener("mouseup", function(e) { isDragging = false; }); imageContainer.addEventListener("mousemove", function(e) { if (isDragging) { var deltaX = e.clientX - lastX; var deltaY = e.clientY - lastY; var newTop = image.offsetTop + deltaY; var newLeft = image.offsetLeft + deltaX; image.style.top = newTop + "px"; image.style.left = newLeft + "px"; lastX = e.clientX; lastY = e.clientY; } }); imageContainer.addEventListener("wheel", function(e) { e.preventDefault(); var scale = e.deltaY > 0 ? 0.9 : 1.1; var newWidth = image.clientWidth * scale; var newHeight = image.clientHeight * scale; image.style.width = newWidth + "px"; image.style.height = newHeight + "px"; }); </script> </body> </html> ``` 这个示例中,我们首先创建了一个容器 `image-container`,然后在容器中放置了一个图片 `image`。我们使用 CSS 来将容器居中并添加了一些样式,例如设置容器为相对定位、边框、隐藏溢出部分和鼠标指针为移动。 然后我们使用 JavaScript 来添加拖拽和缩放的功能。我们使用了三个变量 `isDragging`、`lastX` 和 `lastY` 来跟踪拖拽状态和鼠标位置。当用户按下鼠标按钮时,我们设置 `isDragging` 为 `true` 并记录下当前鼠标位置。当用户释放鼠标按钮时,我们设置 `isDragging` 为 `false`。当用户移动鼠标时,我们检查 `isDragging` 是否为 `true`,如果是,就计算鼠标移动的距离,并将图片的位置相应地调整。这样就实现了拖拽的功能。 最后,我们添加了一个滚轮事件监听器来实现缩放功能。当用户滚动鼠标滚轮时,我们阻止默认行为,然后根据滚轮的方向计算缩放比例,并将图片的宽度和高度相应地调整。 希望这个示例能够帮助你实现所需的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值