js拖拽

xiaoxiao2026-08-29  16

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <div style=" border: 1px solid ;width:300px;height:400px;position:absolute;top:10px;left:10px"> <div id="d" οnfοcus=" move()" style="background-color:red;width:100px;height:100px"></div> </div> </BODY> <script type="text/javascript"> var dragging = false; var test; var mouseY; var mouseX; window.onload = function(){ test = document.getElementById("d"); test.onmousedown = down; test.onmousemove = move; document.onmouseup = up; test.style.position = "relative"; test.style.top = "0px"; test.style.left = "0px"; } function down(event){ event = event || window.event; dragging = true; mouseX = parseInt(event.clientX); mouseY = parseInt(event.clientY); objY = parseInt(test.style.top); objX = parseInt(test.style.left); } function move(event){ event = event || window.event; if(dragging == true){ var x,y; y = event.clientY - mouseY + objY; x = event.clientX - mouseX + objX; test.style.top = y + "px"; test.style.left = x + "px"; } } function up(){ dragging = false; } //--> </SCRIPT> </HTML> 相关资源:javascript拖拽图片到指定位置
转载请注明原文地址: https://www.6miu.com/read-5052075.html

最新回复(0)