With the changes of the times, the importance of js is increasingly felt. js can not only make web pages (such as Ext framework), but also can make some web special effects. These special effects are not only compatible with PCs, but also compatible with mobile phones. After all, it is browser-based and has nothing to do with the platform. Now Microsoft's windows8 system apps can be developed using js. If you have time, you can try it.
Now let’s get to the point and talk about js implementation of draggable Div. To implement this function, let’s first talk about the idea:
1. Capture the mousedown event of the mouse div
2. Capture document’s mousemove event
3. Cancel event
Then let’s take a look at the code:
},
layerX: function (evt) {
var e = this.event(evt);
return e.layerX || e.offsetX;
},
layerY: function (evt) {
var e = this.event(evt);
return e.layerY || e.offsetY;
}
}
var x = page.layerX(e);
var y = page.layerY(e);
if (dv.setCapture) {
dv.setCapture();
}
else if (window.captureEvents) {
window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
d.onmousemove = function (e) {
var tx = page.pageX(e) - x;
var ty = page.pageY(e) - y;
dv.style.left = tx "px";
dv.style.top = ty "px";
}
d.onmouseup = function () {
if (dv.releaseCapture) {
dv.releaseCapture();
}
else if (window.releaseEvents) {
window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
}
d.onmousemove = null;
d.onmouseup = null;
}
}
}
Code analysis:
1.
Get the div object
Copy the code
Copy the code
The code is as follows:
The code is as follows:
I have been learning js recently, and I will share my new experiences with you in the future. I hope to learn and make progress together with everyone.