拖拽
最近整理了一下以前了解过的一些小demo,就从拖拽
说起吧,用原生js实现拖拽效果,大致可分为两种,一是使用鼠标事件,(mousedown、mousemove和mouseup),二是使用H5拖拽drag与拖放drop属性,下面逐一进行代码演示:
使用鼠标事件实现拖拽效果 html
1 2 3 <div id ="container" > <div id ="demo" > </div > </div >
css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 *{ margin : 0 ; padding : 0 ; border : 0 ; } #container { width : 500px ; height : 500px ; border : 1px solid #000 ; position : relative; } #demo { width : 100px ; height : 100px ; background-color : #f40 ; position : absolute; cursor : pointer; }
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 var dragFun = function (child,parent ) { var drag = document .getElementById(child); var dragContainer = document .getElementById(parent) || document ; drag.onmousedown = function (event ) { var event = event || window .event; var diffX = event.clientX - drag.offsetLeft; var diffY = event.clientY - drag.offsetTop; if (typeof drag.setCapture !== 'undefined' ){ drag.setCapture(); } document .onmousemove = function (event ) { var event = event || window .event; var moveX = event.clientX - diffX; var moveY = event.clientY - diffY; if (moveX < 0 ){ moveX = 0 }else if (moveX > window .innerWidth - drag.offsetWidth){ moveX = window .innerWidth - drag.offsetWidth } if (moveY < 0 ){ moveY = 0 }else if (moveY > window .innerHeight - drag.offsetHeight){ moveY = window .innerHeight - drag.offsetHeight } if (moveX < 0 ){ moveX = 0 }else if (moveX > dragContainer.offsetWidth - drag.offsetWidth){ moveX = dragContainer.offsetWidth - drag.offsetWidth } if (moveY < 0 ){ moveY = 0 }else if (moveY > dragContainer.offsetHeight - drag.offsetHeight){ moveY = dragContainer.offsetHeight - drag.offsetHeight } drag.style.left = moveX + 'px' ; drag.style.top = moveY + 'px' } document .onmouseup = function (event ) { this .onmousemove = null ; this .onmouseup = null ; if (typeof drag.releaseCapture!='undefined' ){ drag.releaseCapture(); } } } } dragFun('demo' ,'container' )
使用H5拖拽属性实现拖拽效果 html
1 2 3 <div class ="box1" draggable ="true" id ="source" > </div > <br > <div class ="box2" id ="target" > </div >
css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 *{ margin : 0 ; padding : 0 ; border : 0 ; } .box1 { width : 100px ; height : 100px ; background : salmon } .box2 { width : 300px ; height : 300px ; border : 1px solid black }
js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 window .onload = function ( ) { var source = document .getElementById('source' ); var target = document .getElementById('target' ); source.ondragstart = function (event ) { var e = event || window .event console .log('开始拖拽' ); e.dataTransfer.setData('text' , e.target.id); } target.ondragenter = function ( ) { console .log('进入目标元素' ) } target.ondragover = function (event ) { var event = event || window .event; console .log('在目标元素中拖拽' ); event.preventDefault() } target.ondragleave = function ( ) { console .log('拖放离开目标元素' ) } target.ondrop = function (event ) { console .log('拖放' ); var e = event || window .event var data = e.dataTransfer.getData('text' ); e.target.appendChild(document .getElementById(data)); } }