Heim  >  Artikel  >  Web-Frontend  >  js implementiert Drag & Drop und Adsorptionscode-Sharing

js implementiert Drag & Drop und Adsorptionscode-Sharing

小云云
小云云Original
2018-03-02 13:57:571803Durchsuche

Dieser Artikel teilt Ihnen hauptsächlich den js-Drag-and-Drop- und Adsorptionscode mit, in der Hoffnung, allen zu helfen.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
#big {
width: 500px;
height: 500px;
background-color: #ccc;
position: relative
}
#box {
width: 100px;
height: 100px;
background-color: #f00;
position: absolute
}
</style>
<script>
window.onload = function () {
var box = document.getElementById(&#39;box&#39;);
var big = document.getElementById(&#39;big&#39;);
// 鼠标在box中的位置
var disX = 0,
disY = 0;
box.onmousedown = function (e) {
var thisE = e || event;
disX = thisE.clientX - box.offsetLeft;
disY = thisE.clientY - box.offsetTop;
document.onmousemove = function (ev) {
var thisEvent = ev || event;
var l = thisEvent.clientX - disX;
var t = thisEvent.clientY - disY;
if (l < 20) {
l = 0;
} else if (l > big.offsetWidth - box.offsetWidth - 20) {
l = big.offsetWidth - box.offsetWidth;
}
if (t < 20) {
t = 0;
} else if (t > big.offsetHeight - box.offsetHeight - 20) {
t = big.offsetHeight - box.offsetHeight;
}
box.style.left = l + &#39;px&#39;;
box.style.top = t + &#39;px&#39;;
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
e.preventDefault();
}
}
</script>
</head>
<body>
<p id="big">
<p id="box"></p>
</p>
</body>
</html>

Verwandte Empfehlungen:

JS-Code zum Implementieren von Maus-Drag-and-Drop-Div-Beispiel

JS-Steuerdatei per Drag & Drop ziehen und Tropfeninhalt

Begrenztes Ziehen, magnetische Adsorption.

Das obige ist der detaillierte Inhalt vonjs implementiert Drag & Drop und Adsorptionscode-Sharing. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn