Last active
May 19, 2018 07:04
-
-
Save Jancat/aa1c1b1af29dfa6a405afa4b9254d254 to your computer and use it in GitHub Desktop.
Revisions
-
Jancat revised this gist
May 19, 2018 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ window.onload = function() { const DragDrop = (function() { let dragging = null // 记录拖拽的对象 var diffX = 0 // 记录鼠标与正方形边缘的距离 var diffY = 0 // 同上 @@ -22,7 +22,7 @@ window.onload = function() { // 实时修改正方形的position中的 top 和 left值 case 'mousemove': if (dragging !== null && event.clientX >= diffX && event.clientY >= diffY) { dragging.style.left = event.clientX - diffX + 'px' dragging.style.top = event.clientY - diffY + 'px' } @@ -38,9 +38,7 @@ window.onload = function() { // 返回对象 return { // 触发,监听以上三个事件,监听对象document fire: function() { document.addEventListener('mousedown', dragEvent, false) document.addEventListener('mousemove', dragEvent, false) -
Jancat created this gist
May 15, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ drag-drop --------- A [Pen](https://codepen.io/Jancat/pen/erLBYa) by [君临](https://codepen.io/Jancat) on [CodePen](https://codepen.io). [License](https://codepen.io/Jancat/pen/erLBYa/license). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ <div class="square"></div> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ // 实现正方形的拖拽 // 记录鼠标位置,赋予正方形position 属性 的 top 和 left // 记录事件 mousedown mousemove mouseup window.onload = function() { const DragDrop = (function() { const dragging = null // 记录拖拽的对象 var diffX = 0 // 记录鼠标与正方形边缘的距离 var diffY = 0 // 同上 function dragEvent(event) { var that = event.target // 判断事件类型 switch (event.type) { // 记录鼠标点击处的位置,与目标的位置做差值 case 'mousedown': dragging = that diffX = event.clientX - that.offsetLeft diffY = event.clientY - that.offsetTop break // 实时修改正方形的position中的 top 和 left值 case 'mousemove': if (dragging !== null) { dragging.style.left = event.clientX - diffX + 'px' dragging.style.top = event.clientY - diffY + 'px' } break // 将拖拽对象置为 null case 'mouseup': dragging = null break } // ... do something } // 返回对象 return { /* 触发,监听以上三个事件,监听对象document */ fire: function() { document.addEventListener('mousedown', dragEvent, false) document.addEventListener('mousemove', dragEvent, false) document.addEventListener('mouseup', dragEvent, false) }, } })() DragDrop.fire() } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ .square { width: 100px; height: 100px; border: 1px solid #000; background-color: #000; position: absolute; }