當前位置:編程學習大全網 - 源碼下載 - 版塊拖動是如何做出來的?

版塊拖動是如何做出來的?

以下代碼用文本保存為.html文件,就看到了

<html>

<head>

<title>Simulated Drag And Drop Example</title>

<script type="text/javascript">

var EventUtil = new Object;

EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {

if (oTarget.addEventListener) {

oTarget.addEventListener(sEventType, fnHandler, false);

} else if (oTarget.attachEvent) {

oTarget.attachEvent("on" + sEventType, fnHandler);

} else {

oTarget["on" + sEventType] = fnHandler;

}

};

EventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler) {

if (oTarget.removeEventListener) {

oTarget.removeEventListener(sEventType, fnHandler, false);

} else if (oTarget.detachEvent) {

oTarget.detachEvent("on" + sEventType, fnHandler);

} else {

oTarget["on" + sEventType] = null;

}

};

EventUtil.formatEvent = function (oEvent) {

if (typeof oEvent.charCode == "undefined") {

oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;

oEvent.isChar = (oEvent.charCode > 0);

}

if (oEvent.srcElement && !oEvent.target) {

oEvent.eventPhase = 2;

oEvent.pageX = oEvent.clientX + document.body.scrollLeft;

oEvent.pageY = oEvent.clientY + document.body.scrollTop;

if (!oEvent.preventDefault) {

oEvent.preventDefault = function () {

this.returnValue = false;

};

}

if (oEvent.type == "mouseout") {

oEvent.relatedTarget = oEvent.toElement;

} else if (oEvent.type == "mouseover") {

oEvent.relatedTarget = oEvent.fromElement;

}

if (!oEvent.stopPropagation) {

oEvent.stopPropagation = function () {

this.cancelBubble = true;

};

}

oEvent.target = oEvent.srcElement;

oEvent.time = (new Date).getTime();

}

return oEvent;

};

EventUtil.getEvent = function() {

if (window.event) {

return this.formatEvent(window.event);

} else {

return EventUtil.getEvent.caller.arguments[0];

}

};

var iDiffX = 0;

var iDiffY = 0;

function handleMouseMove() {

var oEvent = EventUtil.getEvent();

var oDiv = document.getElementById("div1");

oDiv.style.left = oEvent.clientX - iDiffX;

oDiv.style.top = oEvent.clientY - iDiffY;

}

function handleMouseDown() {

var oEvent = EventUtil.getEvent();

var oDiv = document.getElementById("div1");

iDiffX = oEvent.clientX - oDiv.offsetLeft;

iDiffY = oEvent.clientY - oDiv.offsetTop;

EventUtil.addEventHandler(document.body, "mousemove", handleMouseMove);

EventUtil.addEventHandler(document.body, "mouseup", handleMouseUp);

}

function handleMouseUp() {

EventUtil.removeEventHandler(document.body, "mousemove", handleMouseMove);

EventUtil.removeEventHandler(document.body, "mouseup", handleMouseUp);

}

</script>

<style type="text/css">

#div1 {

background-color: red;

height: 100px;

width: 100px;

position: absolute;

}

</style>

</head>

<body>

<p>Try dragging the red square.</p>

<p><div id="div1" onmousedown="handleMouseDown(event)"></div> </p>

</body>

</html>

  • 上一篇:上帝之數
  • 下一篇:11001000源代碼。
  • copyright 2024編程學習大全網