(Demo分享)利用JavaScript(JS)实现一个九宫格拖拽功能
<p><span style="font-family: "Microsoft YaHei"; font-size: 18pt"><strong>利用JavaScript(JS)实现一个九宫格拖拽功能</strong></span></p><p><strong> </strong></p>
<p>Demo实现了对任意方格进行拖拽,可以交换位置,其中Demo-1利用了勾股定理判断距离!</p>
<p><span style="font-size: 14pt"><strong>Demo-1整体思路:</strong></span></p>
<p><span style="font-size: 18px"><strong>1.首先div实现自由移动,一定需要脱离标准文档流,所以我们给它使用绝对定位。</strong></span></p>
<p><span style="font-size: 18px"><strong>2.利用视觉欺骗,点击鼠标悬浮的其实是利用JS生成的一个Div,交换信息的两个Div并没有位置交换,只是把双方属性进行了交换。</strong></span></p>
<p><span style="font-size: 18px"><strong>3.利用了勾股定理去判断距离。</strong></span></p>
<p><br><strong>##效果图如下:</strong></p>
<p><strong><img src="https://img2018.cnblogs.com/blog/1669498/201905/1669498-20190515193859540-141355462.png" alt=""></strong></p>
<p> </p>
<p><span style="font-size: 14pt">网页源代码:</span></p>
<div class="cnblogs_code">
<pre><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>九宫格拖拽</title>
</head>
<style type="text/css"><span style="color: rgba(0, 0, 0, 1)">
#content{
width: 300px;
height:300px;
margin: </span>0<span style="color: rgba(0, 0, 0, 1)"> auto;
background: #F47564;
position: relative;
}
#content div{
width: 100px;
height: 100px;
</span><span style="color: rgba(0, 0, 255, 1)">float</span><span style="color: rgba(0, 0, 0, 1)">: left;
line</span>-<span style="color: rgba(0, 0, 0, 1)">height: 100px;
text</span>-<span style="color: rgba(0, 0, 0, 1)">align: center;
font</span>-<span style="color: rgba(0, 0, 0, 1)">size:40px;
font</span>-weight: 900<span style="color: rgba(0, 0, 0, 1)">;
color: #fff;
cursor: pointer;
position: absolute;
}
#content div:nth</span>-child(1<span style="color: rgba(0, 0, 0, 1)">){
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #f05b72;
top:</span>0<span style="color: rgba(0, 0, 0, 1)">;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
}
#content div:nth</span>-child(2<span style="color: rgba(0, 0, 0, 1)">){
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #faa755;
top:</span>0<span style="color: rgba(0, 0, 0, 1)">;
left:100px;
}
#content div:nth</span>-child(3<span style="color: rgba(0, 0, 0, 1)">){
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #2585a6;
top:</span>0<span style="color: rgba(0, 0, 0, 1)">;
left: 200px;
}
#content div:nth</span>-child(4<span style="color: rgba(0, 0, 0, 1)">){
background:#5ce4fd;
top:100px;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
}
#content div:nth</span>-child(5<span style="color: rgba(0, 0, 0, 1)">){
background:#f61acf;
top:100px;
left: 100px;
}
#content div:nth</span>-child(6<span style="color: rgba(0, 0, 0, 1)">){
background:#3ef9bd;
top:100px;
left: 200px;
}
#content div:nth</span>-child(7<span style="color: rgba(0, 0, 0, 1)">){
background:#9af93e;
top:200px;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
}
#content div:nth</span>-child(8<span style="color: rgba(0, 0, 0, 1)">){
background:#eef93e;
top:200px;
left: 100px;
}
#content div:nth</span>-child(9<span style="color: rgba(0, 0, 0, 1)">){
background:#f9843e;
top:200px;
left:200px;
}
#content .draging{
position: absolute;
}
</span></style>
<body>
<div id="content">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
<script type="text/javascript">
<span style="color: rgba(0, 0, 255, 1)">var</span> content = document.getElementById('content'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> items = content.querySelectorAll('div'<span style="color: rgba(0, 0, 0, 1)">);
console.log(items);
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> itemBox(itemName){
itemName.onmousedown </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(evt1){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> e = evt1 ||<span style="color: rgba(0, 0, 0, 1)"> window.event;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在鼠标点下item的时候克隆一个新的一模一样的名为cloneDiv</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> cloneDiv = document.createElement('div'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把样式文字赋给克隆div</span>
cloneDiv.innerHTML =<span style="color: rgba(0, 0, 0, 1)"> itemName.innerHTML;
cloneDiv.style.backgroundColor </span>=<span style="color: rgba(0, 0, 0, 1)"> getComputedStyle(itemName).backgroundColor;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">使克隆div的class名为draging,使克隆div加绝对定位position:absolute</span>
cloneDiv.className = 'draging'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">克隆div的定位top left和原div相同</span>
cloneDiv.style.top = itemName.offsetTop +'px'<span style="color: rgba(0, 0, 0, 1)">;
cloneDiv.style.left </span>= itemName.offsetLeft+'px'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把kelongdiv加入到concent大盒子当中</span>
<span style="color: rgba(0, 0, 0, 1)"> content.appendChild(cloneDiv);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">算出鼠标点击克隆div时,鼠标在克隆div中的相对位置</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> offsetX = evt1.pageX -<span style="color: rgba(0, 0, 0, 1)"> cloneDiv.offsetLeft;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> offsetY = evt1.pageY -<span style="color: rgba(0, 0, 0, 1)"> cloneDiv.offsetTop;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> cloneDiv.style.zIndex = 10;</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">鼠标开始移动的时候</span>
document.onmousemove = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(evt){
</span><span style="color: rgba(0, 0, 255, 1)">var</span> e2 = evt ||<span style="color: rgba(0, 0, 0, 1)"> window.event;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">克隆div定位的lefttop evt.pageX鼠标点相对于整个文档中的位置, offsetX鼠标相对于克隆div中的位置 二者相减就是克隆div相对于整个文档中的位置</span>
cloneDiv.style.left = evt.pageX - offsetX +'px'<span style="color: rgba(0, 0, 0, 1)">;
cloneDiv.style.top </span>= evt.pageY -offsetY+'px'<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当鼠标松开的时候</span>
document.onmouseup = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先把移动事件停止</span>
document.onmousemove = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要判断 clonediv 的位置距 那个div 最近</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先获取到content大盒子下所有的div</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> divs = content.getElementsByTagName('div'<span style="color: rgba(0, 0, 0, 1)">);
console.log(divs.length)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">然后判断移动到位置距离哪一个最近</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> min = 800;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">考虑到被拖拽的div拖拽到大盒子之外还能进行交换,距离设为大盒子外 方圆800px</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> minDiv = <span style="color: rgba(0, 0, 255, 1)">null</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设一个空的div用来交换位置</span>
<span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i=0;i<divs.length-1;i++){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">遍历出除了鼠标移动的div外的所有的div也就是length - 1</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> div =<span style="color: rgba(0, 0, 0, 1)"> divs;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">div cloneDiv</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">distance(cloneDiv,div)返回值为c,c为两div之间的距离</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> dis = distance(cloneDiv,div);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用运用勾股定理进行比较距离的函数</span>
<span style="color: rgba(0, 0, 255, 1)">if</span>(dis <= min){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a^2 + b^2 和c^2比较</span>
min =<span style="color: rgba(0, 0, 0, 1)"> dis;
minDiv </span>=<span style="color: rgba(0, 0, 0, 1)"> div;
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把距离小的那个div的样式给拖拽走空出来的divitemName</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果这样赋值的话target的innerHTML就会丢失都变成minDiv.innerHTML,所以的设一个中转变量,让两者交换。background同理</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> minDiv.innerHTML = target.innerHTML;</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">两者样式交换,引入第三变量tmpInnerHTMl</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> tmpInnerHTMl =<span style="color: rgba(0, 0, 0, 1)"> minDiv.innerHTML;
minDiv.innerHTML </span>=<span style="color: rgba(0, 0, 0, 1)"> itemName.innerHTML;
itemName.innerHTML </span>=<span style="color: rgba(0, 0, 0, 1)"> tmpInnerHTMl;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">引入第三变量tmpColor</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> tmpColor =<span style="color: rgba(0, 0, 0, 1)"> getComputedStyle(minDiv).backgroundColor;
minDiv.style.backgroundColor </span>=<span style="color: rgba(0, 0, 0, 1)"> getComputedStyle(itemName).backgroundColor;
itemName.style.backgroundColor </span>=<span style="color: rgba(0, 0, 0, 1)"> tmpColor;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删掉克隆元素</span>
<span style="color: rgba(0, 0, 0, 1)"> content.removeChild(cloneDiv)
document.onmouseup </span>= <span style="color: rgba(0, 0, 255, 1)">null</span>;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把鼠标松开后的操作清除</span>
<span style="color: rgba(0, 0, 0, 1)">
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">for循环使每一个item都调用itemBox()这个函数</span>
<span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i < items.length; i++<span style="color: rgba(0, 0, 0, 1)">){
itemBox(items);
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> distance(div1,div2) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">勾股定理 记录 两个元素的距离</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> a = div1.offsetLeft -<span style="color: rgba(0, 0, 0, 1)"> div2.offsetLeft;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> b = div1.offsetTop -<span style="color: rgba(0, 0, 0, 1)"> div2.offsetTop;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> c = Math.sqrt(a*a+b*b);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">利用勾股定理算出两个div的距离 a^2 + b^2 再开平方得出c</span>
<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> c;
}
</span></script>
</body>
</html></pre>
</div>
<p><span style="font-size: 18pt"><strong>Demo-2整体思路(页面存在很多Bug,提供另一种思路,建议使用Demo1的方法):</strong></span></p>
<p><span style="font-size: 18px"><strong>1.首先div实现自由移动,一定需要脱离标准文档流,所以我们给它使用绝对定位。</strong></span></p>
<p><span style="font-size: 18px"><strong>2.利用每个div方块都有相对的坐标值,当鼠标按下移动元素的时候,靠近哪个坐标值,<strong>靠近坐标值对应的方块就与当前元素进行位置交换。<strong><br></strong></strong></strong></span></p>
<p><span style="font-size: 18px"><strong>3.当前demo,只能存在的bug:元素只能进行交换一次,第二次交换时会发生div被覆盖的结果,不过也提供了另一种思路。</strong></span></p>
<p><br><strong>##效果图如下:</strong></p>
<p><strong><img src="https://img2018.cnblogs.com/blog/1669498/201905/1669498-20190515194708532-1977880669.png" alt=""></strong></p>
<div class="cnblogs_code">
<pre><!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"<span style="color: rgba(0, 0, 0, 1)">
content</span>="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>九宫格</title>
<style>
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">CSS样式设置</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">为了方便查看使用通配符去除默认样式,实际开发不建议使用</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
*<span style="color: rgba(0, 0, 0, 1)"> {
margin: </span>0<span style="color: rgba(0, 0, 0, 1)">;
padding: </span>0<span style="color: rgba(0, 0, 0, 1)">;
}
#list {
width: 390px;
height: 390px;
margin: 90px auto </span>0<span style="color: rgba(0, 0, 0, 1)">;
border</span>-<span style="color: rgba(0, 0, 0, 1)">radius: 10px;
display: flex;
flex</span>-<span style="color: rgba(0, 0, 0, 1)">flow: wrap;
position: relative;
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">让div中文字无法被选取,防止因鼠标滑过选取文字而导致鼠标失去焦点</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
-webkit-touch-<span style="color: rgba(0, 0, 0, 1)">callout: none;
</span>-webkit-user-<span style="color: rgba(0, 0, 0, 1)">select: none;
</span>-moz-user-<span style="color: rgba(0, 0, 0, 1)">select: none;
</span>-ms-user-<span style="color: rgba(0, 0, 0, 1)">select: none;
user</span>-<span style="color: rgba(0, 0, 0, 1)">select: none;
}
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">需要给移动的div设置绝对定位,使其脱离标准文档流</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
div</span>><span style="color: rgba(0, 0, 0, 1)">div {
width: 130px;
height: 130px;
font</span>-<span style="color: rgba(0, 0, 0, 1)">size: 50px;
line</span>-<span style="color: rgba(0, 0, 0, 1)">height: 130px;
border</span>-<span style="color: rgba(0, 0, 0, 1)">radius: 8px;
color: #fff;
text</span>-<span style="color: rgba(0, 0, 0, 1)">align: center;
font</span>-<span style="color: rgba(0, 0, 0, 1)">weight: bold;
box</span>-sizing: border-<span style="color: rgba(0, 0, 0, 1)">box;
cursor: move;
position: absolute;
}
#d1 {
top: </span>0<span style="color: rgba(0, 0, 0, 1)">;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #f05b72;
}
#d2 {
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #faa755;
top: </span>0<span style="color: rgba(0, 0, 0, 1)">;
left: 130px;
}
#d3 {
top: </span>0<span style="color: rgba(0, 0, 0, 1)">;
left: 260px;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #2585a6;
}
#d4 {
top: 130px;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #50b7c1;
}
#d5 {
top: 130px;
left: 130px;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #69541b;
}
#d6 {
top: 130px;
left: 260px;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #8552a1;
}
#d7 {
top: 260px;
left: </span>0<span style="color: rgba(0, 0, 0, 1)">;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #274d3d;
}
#d8 {
top: 260px;
left: 130px;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #73b9a2;
}
#d9 {
top: 260px;
left: 260px;
background</span>-<span style="color: rgba(0, 0, 0, 1)">color: #7fb80e;
}
</span></style>
</head>
<body>
<div id="list">
<div id="d1">1</div>
<div id="d2">2</div>
<div id="d3">3</div>
<div id="d4">4</div>
<div id="d5">5</div>
<div id="d6">6</div>
<div id="d7">7</div>
<div id="d8">8</div>
<div id="d9">9</div>
</div>
</body>
<script>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 首先获取每个div的ID,声明变量</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> list = document.getElementById("list"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d1 = document.getElementById("d1"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d2 = document.getElementById("d2"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d3 = document.getElementById("d3"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d4 = document.getElementById("d4"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d5 = document.getElementById("d5"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d6 = document.getElementById("d6"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d7 = document.getElementById("d7"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d8 = document.getElementById("d8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> d9 = document.getElementById("d9"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 封装函数,方便后续使用</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> box (divname) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当在divname上点下鼠标发生的事件</span>
divname.onmousedown = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (env) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 在div移动前先获取每个传入的实参(divname)的位置,留作位置判断失败后返回原位置</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> a1x =<span style="color: rgba(0, 0, 0, 1)"> divname.offsetLeft;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> a1y =<span style="color: rgba(0, 0, 0, 1)"> divname.offsetTop;
console.log(a1x</span>+"这是"<span style="color: rgba(0, 0, 0, 1)">)
console.log(a1y</span>+"这是"<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 给每个传入的实参(divname)设置层级,不然会被其他div遮挡住</span>
divname.style.zIndex = "4"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> env代表事件的状态,设置兼容。</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> env = env ||<span style="color: rgba(0, 0, 0, 1)"> window.event;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取鼠标当被点击的在div上的位置:</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> env.clientX(代表当前鼠标距离屏幕左边的位置)。</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> list.offsetLeft(代表list盒子距离屏幕的左边的位置)。</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为divname设置了绝对定位,所以divname.offsetLeft的参数代表鼠标在div上距离父元素的位置</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> (env.clientX - list.offsetLeft - divname.offsetLeft)相当于把鼠标当前位置设置为原点</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> divmosue_x = (env.clientX - list.offsetLeft -<span style="color: rgba(0, 0, 0, 1)"> divname.offsetLeft);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> divmosue_y = (env.clientY - list.offsetTop -<span style="color: rgba(0, 0, 0, 1)"> divname.offsetTop);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当在divname上点下鼠标发生并且鼠标在指定的元素中移动</span>
divname.onmousemove = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (env) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> env = env ||<span style="color: rgba(0, 0, 0, 1)"> window.event;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取鼠标当被点击移动的时候在div上的位置:</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> env.clientX(代表当前鼠标距离屏幕左边的位置)。</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> divmosue_x的含义参照上条注释</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> list.offsetLeft(代表list盒子距离屏幕的左边的位置)。</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> div_x的含义相当于把鼠标当前坐标点设置为原点</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> div_x = env.clientX - divmosue_x -<span style="color: rgba(0, 0, 0, 1)"> list.offsetLeft;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> div_y = env.clientY - divmosue_y -<span style="color: rgba(0, 0, 0, 1)"> list.offsetTop;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 因为divname设置了绝对定位,下面让divname的位置随着鼠标的移动而发生变化</span>
divname.style.top =div_y+"px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= div_x+"px"
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当鼠标松开时的事件,移动的div根据if判断坐标位置,让div符合要求的div交换位置。</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 并让移动后的双方层级为1,防止元素层级遮挡</span>
divname.onmouseup = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > -65 && div_x < 0 && div_y > -65 && div_y < 0<span style="color: rgba(0, 0, 0, 1)">) {
d1.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d1.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.left </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">
d1.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 65 && div_x < 130 && div_y > -65 && div_y < 0<span style="color: rgba(0, 0, 0, 1)">) {
d2.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d2.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
d2.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 190 && div_x <255&& div_y > -65 && div_y < 0<span style="color: rgba(0, 0, 0, 1)">) {
d3.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d3.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
d3.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > -65 && div_x < 0 && div_y > 65 && div_y < 130<span style="color: rgba(0, 0, 0, 1)">) {
d4.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d4.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">
d4.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 65 && div_x < 130 && div_y > 65 && div_y < 130<span style="color: rgba(0, 0, 0, 1)">) {
d5.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d5.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
d5.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 190 && div_x <255&& div_y > 65 && div_y < 130<span style="color: rgba(0, 0, 0, 1)">) {
d6.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d6.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > -65 && div_x < 0 && div_y > 190 && div_y < 255<span style="color: rgba(0, 0, 0, 1)">) {
d7.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d7.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "0px"<span style="color: rgba(0, 0, 0, 1)">
d5.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 65 && div_x < 130 && div_y > 190 && div_y < 255<span style="color: rgba(0, 0, 0, 1)">) {
d8.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d8.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "130px"<span style="color: rgba(0, 0, 0, 1)">
d8.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (div_x > 190 && div_x <255&& div_y > 190 && div_y < 255<span style="color: rgba(0, 0, 0, 1)">) {
d9.style.top </span>= a1y + "px"<span style="color: rgba(0, 0, 0, 1)">;
d9.style.left </span>= a1x +"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.top </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
divname.style.left </span>= "260px"<span style="color: rgba(0, 0, 0, 1)">
d9.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果divname,未符合判断条件,则让它回到原点位置</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
console.log(</span>"asdsa"<span style="color: rgba(0, 0, 0, 1)">)
console.log(a1x)
console.log(a1y)
divname.style.top </span>= a1x + "px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.left </span>= a1y+"px"<span style="color: rgba(0, 0, 0, 1)">;
divname.style.zIndex </span>= "1"<span style="color: rgba(0, 0, 0, 1)">;
divname.onmousemove </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
}
divname.onmousemove </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
console.log(</span>"松开了"<span style="color: rgba(0, 0, 0, 1)">);
}
}
}
}
box(d1)
box(d2)
box(d3)
box(d4)
box(d5)
box(d6)
box(d7)
box(d8)
box(d9)
</span></script>
</html></pre>
</div>
<p><strong>本demo-1参考思路来源于网络,如有侵权,请及时联系删除。</strong></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/zhaohongcheng/p/10871726.html
頁:
[1]