Javascript实现图片位置控制(鼠标拖拽 + 键盘方向键移动)源码分享

实现图片的拖拽和鼠标方向控制位移
 
HTML文件代码如下(img.html):
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片位置调整</title>
<script language="javascript" src="Drag.js"></script>
</head>

<body οnkeydοwn="keyAction(event);" οnlοad="Init();">
<img src="pig.GIF" name="T4" width="160" height="120" id="T4" style="position:absolute; left:10px; top:10px;" οnclick="prepareMove(this,event);" οnmοusedοwn="beginMove(this,event);"/>
</body>
</html>

 
JS文件代码如下(Drag.js):
 
>


var actionObj = new Array();
var bar;
var isIE,isNS;

function Init()
{
 //获取浏览器版本
 if(navigator.appName.toLowerCase().indexOf("microsoft") != -1 && navigator.appVersion.indexOf("6.0"))
 {
  isIE = true;
 }
 else if(navigator.appName.toLowerCase().indexOf("netscape") != -1 && navigator.appVersion.toLowerCase().indexOf("5.0") != -1)
 {
  isNS = true;
 }
 else
 {
  alert("[Global.js][Init()]请使用IE6.0或FireFox浏览器浏览本页!");
  //Glob.isIE = true;
 }

 if(isIE)
 {
  document.onmousedown = function(){clearSel();};
 }
 else if(isNS)
 {
  document.body.setAttribute("onmousedown","clearSel();");
 }

 //创建拖动点
 bar = document.createElement("div");
 bar.id = "bar";
 bar.style.width = "20px";
 bar.style.height = "20px";
 bar.style.position = "absolute";
 bar.style.zIndex = 10000;
 bar.style.display = "none";
 if(isIE)
 {
  bar.style.backgroundColor = "#ffffff";
  bar.style.filter = "alpha(opacity=0)";
 }
 else if(isNS)
 {
  bar.style.backgroundImage = "url(../images/alpha.gif)";
 }
 bar.style.cursor = "move";
 document.body.appendChild(bar);
 
 if(isIE)
 {
  bar.onmousedown = function()
  {
   beginMove(event);
  }
 }
 if(isNS)
 {
  bar.setAttribute("onmousedown","beginMove(event)");
 }
}

//为键盘移动做准备
function prepareMove(mvObj,e)
{
 //清理上一个选中
 clearSel();
 
 var obj = mvObj;
 obj.style.borderWidth = "1px";
 obj.style.borderStyle = "dashed";
 obj.style.borderColor = "#666666";
 obj.style.left = parseInt(obj.style.left) - 1 + "px";
 obj.style.top = parseInt(obj.style.top) - 1 + "px";
 
 actionObj[actionObj.length] = obj;
 
 //设置拖动点
 bar.style.display = "block";
 bar.style.width = mvObj.style.width == "" ? mvObj.width + "px" : mvObj.style.width;
 bar.style.height = mvObj.style.height == "" ? mvObj.height + "px" : mvObj.style.height;
 bar.style.left = parseInt(mvObj.style.left) + 1 + "px";
 bar.style.top = parseInt(mvObj.style.top) + 1 + "px";
 
    if (e.stopPropagation) e.stopPropagation(  );
    else e.cancelBubble = true;

    if (e.preventDefault) e.preventDefault(  );
    else e.returnValue = false;
}

//为鼠标移动做准备
function beginMove(e)
{
 var mvObj = getAc();
 if(mvObj == null)return;
 var mouseStartX = e.clientX;
 var mouseStartY = e.clientY;
 
 mvObj.sLeft = parseInt(mvObj.style.left);
 mvObj.sTop = parseInt(mvObj.style.top);
// alert(mvObj.Left);
// alert(mvObj.Top);
 
    if (e.stopPropagation) e.stopPropagation(  );
    else e.cancelBubble = true;

    if (e.preventDefault) e.preventDefault(  );
    else e.returnValue = false;
 
    if (document.addEventListener) {
        document.addEventListener("mousemove", moveHandler, true);
        document.addEventListener("mouseup", upHandler, true);
    }
    else if (document.attachEvent) {
        document.attachEvent("onmousemove", moveHandler);
        document.attachEvent("onmouseup", upHandler);
    }

 function moveHandler()
 {
  var mouseChangeX = e.clientX - mouseStartX;
  var mouseChangeY = e.clientY - mouseStartY;

  mvObj.Left = mvObj.sLeft + mouseChangeX;
   mvObj.Left = mvObj.Left > 0 ? mvObj.Left : 0;
  mvObj.Top = mvObj.sTop + mouseChangeY;
   mvObj.Top = mvObj.Top > 0 ? mvObj.Top : 0;
  
  mvObj.style.left = mvObj.Left + "px";
  mvObj.style.top = mvObj.Top + "px";
 }

 function upHandler()
 {
        if (document.removeEventListener) {
            document.removeEventListener("mouseup", upHandler, true);
            document.removeEventListener("mousemove", moveHandler, true);
        }
        else if (document.detachEvent) {
            document.detachEvent("onmouseup", upHandler);
            document.detachEvent("onmousemove", moveHandler);
        }
  
  bar.style.display = "block";
  bar.style.width = mvObj.style.width == "" ? mvObj.width + "px" : mvObj.style.width;
  bar.style.height = mvObj.style.height == "" ? mvObj.height + "px" : mvObj.style.height;
  bar.style.left = parseInt(mvObj.style.left) + 1 + "px";
  bar.style.top = parseInt(mvObj.style.top) + 1 + "px";
 }
}

function keyAction(e)
{
 var acOp = getAc();
 if(acOp == null)return;
 
 switch(e.keyCode)
 {
  case 37:
   acOp.style.left = parseInt(acOp.style.left) - 1 + "px";
   break;
  case 38:
   acOp.style.top = parseInt(acOp.style.top) - 1 + "px";
   break;
  case 39:
   acOp.style.left = parseInt(acOp.style.left) + 1 + "px";
   break;
  case 40:
   acOp.style.top = parseInt(acOp.style.top) + 1 + "px";
   break;
 }
}

function clearSel()
{
 var acOp = getAc();
 if(acOp == null)return;
 
 acOp.style.borderWidth = "0px";
 acOp.style.left = parseInt(acOp.style.left) + 1 + "px";
 acOp.style.top = parseInt(acOp.style.top) + 1 + "px";
 
 actionObj.length -= 1;
 
 //设置拖动点
 bar.style.display = "none";
}

function getAc()
{
 if(actionObj.length > 0)
  return actionObj[actionObj.length - 1];
 else
  return null;
}

 
 代码不是很工整,望谅解,有不清楚的地方,可以与我联系,Email:ctbinzi@gmail.com
 
 如果有错误,还望提出
 
 愿与大家分享技术!
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个简单的JavaScript实现图片鼠标位置的放大缩小及拖拽功能的示例: HTML代码: ```html <div id="image-container"> <img id="image" src="example.jpg" /> </div> ``` CSS代码: ```css #image-container { width: 500px; height: 500px; overflow: hidden; position: relative; } #image { position: absolute; top: 0; left: 0; width: 100%; height: 100%; cursor: move; } ``` JavaScript代码: ```javascript var container = document.getElementById("image-container"); var image = document.getElementById("image"); // 初始缩放比例为1 var scale = 1; // 鼠标按下时的位置 var mouseX = 0; var mouseY = 0; // 鼠标是否按下 var isMouseDown = false; // 鼠标按下时记录鼠标位置 container.addEventListener("mousedown", function(event) { isMouseDown = true; mouseX = event.clientX; mouseY = event.clientY; }); // 鼠标松开时重置鼠标状态 container.addEventListener("mouseup", function(event) { isMouseDown = false; }); // 鼠标移动时缩放或拖拽图片 container.addEventListener("mousemove", function(event) { if (isMouseDown) { // 鼠标按下时拖拽图片 var deltaX = event.clientX - mouseX; var deltaY = event.clientY - mouseY; image.style.left = parseInt(image.style.left) + deltaX + "px"; image.style.top = parseInt(image.style.top) + deltaY + "px"; mouseX = event.clientX; mouseY = event.clientY; } else { // 鼠标未按下时缩放图片 var deltaScale = 0; if (event.deltaY < 0) { deltaScale = 0.1; } else if (event.deltaY > 0) { deltaScale = -0.1; } scale += deltaScale; // 最小缩放比例为0.1 scale = Math.max(scale, 0.1); image.style.transform = "scale(" + scale + ")"; } }); ``` 这段代码实现了以下功能: 1. 鼠标按下并拖拽图片时,图片跟随鼠标移动。 2. 鼠标未按下时,通过滚轮上下滑动实现图片的放大缩小,最小缩放比例为0.1。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ctbinzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值