javascript实现简单拖曳功能
欢迎访问我的博客,祝码农同胞们早日走上人生巅峰,迎娶白富美~~~
场景:想必大家对这一类场景很熟悉,QQ登陆面板跟着鼠标的拖动,鼠标到哪它到哪,飞机大战小游戏里,鼠标或者手指到哪,飞机到哪,这一类场景用的很多,那么下面来简单实现一下:
思路:
- 在鼠标移动的时候,计算鼠标位置
- 给盒子一个绝对定位,将鼠标位置赋值给盒子
HTML 结构
1 2 3 4 5 6 7 8 9 10
| <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div id="drag">想看效果?狠狠的拽我!!</div> </body> </html>
|
CSS 样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #drag{ width: 200px; height: 200px; background: yellow; color: red; font-size: 20px; font-weight: bold; position: absolute; left: 40%; top: 37%; } #drag:active{ cursor: move; }
|
这时候就有了个黄底红字的大盒子,用 JS 去编写 鼠标按下,鼠标移动,鼠标抬起的事件驱动程序,实现拖曳效果
思路:将鼠标所在位置直接赋值给盒子的绝对定位的位置left
和top
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var oDrag = document.getElementById("drag") getDarg(oDrag) function getDarg(obj){ obj.onmousedown = function(e){ document.onmousemove = function(e){ var event = e || event obj.style.left = event.clientX + 'px' obj.style.top = event.clientY + 'px' } document.onmouseup = function(){ document.onmousemove = null document.onmouseup = null } } }
|
这样实现了拖曳效果,但是很显然,这样鼠标拖曳按钮一直在黄盒子的左上角,体验很不好,那么就要合理的计算鼠标在盒子中的位置,鼠标在盒子中的位置计算可以用鼠标在可视区域的位置减去盒子绝对定位的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| var oDrag = document.getElementById("drag") getDarg(oDrag) function getDarg(obj){ obj.onmousedown = function(e){
var event = e || event
var distanceX = event.clientX - this.offsetLeft var distanceY = event.clientY - this.offsetTop
document.onmousemove = function(e){ ... } document.onmouseup = function(){ ... } } }
|
这样就基本实现了需求,但是拖动的时候会不注意选中盒子上的文字,也影响用户体验,借助window
和document
下面的方法在拖曳盒子即移动鼠标的时候取消文本选中
1
| window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty()
|
这样就好多了
下面上完整代码:
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 54 55 56 57 58 59 60 61 62 63 64
| <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript" src='./js/jquery-1.8.3.min.js'></script> <style type="text/css"> #drag{ width: 200px; height: 200px; background: yellow; color: red; font-size: 20px; font-weight: bold; position: absolute; left: 40%; top:37%; } #drag:active{ cursor: move; } </style> </head> <body>
<div id="drag">想看效果?狠狠的拽我!!</div>
<script type="text/javascript"> var oDrag = document.getElementById("drag") getDarg(oDrag) function getDarg(obj){ obj.onmousedown = function(e){
var event = e || event
var distanceX = event.clientX - this.offsetLeft var distanceY = event.clientY - this.offsetTop
document.onmousemove = function(e){ var event = e || event obj.style.left = event.clientX - distanceX + 'px' obj.style.top = event.clientY - distanceY + 'px'
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty() } document.onmouseup = function(){
document.onmousemove = null document.onmouseup = null } } } </script> </body> </html>
|
至此,简单拖曳功能完成,对应的如果开发移动端,只需要改上移动端的事件即可,例如touchstart
,touchmove
,touchend
, touchcancel
等