拖拽原理
2016-12-02 20:01
483 查看
拖拽原理
1.鼠标按下+鼠标移动 = 拖拽 事件 onmousedown + onmousemove
2.鼠标松开 = 无拖拽 停止拖拽 onmouseup
3.鼠标偏移 = 拖拽距离
1.鼠标按下+鼠标移动 = 拖拽 事件 onmousedown + onmousemove
2.鼠标松开 = 无拖拽 停止拖拽 onmouseup
3.鼠标偏移 = 拖拽距离
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> .box{ width:100px; height:100px; background-color:greenyellow; } </style> </head> <body> <div class="box"></div> <script> var box=document.getElementsByTagName("div"); drag(box[0]); function drag(elem){ var oldX,oldY,newX,newY; elem.onmousedown=function(e){ //相对定位的拖拽div this.style.position="relative"; if(!this.style.left && !this.style.top){ //第一次设置left、top为0 this.style.left = 0; this.style.top = 0; } var oldX= e.clientX; var oldY= e.clientY; document.onmousemove=function(e){ newX= e.clientX; newY= e.clientY; elem.style.left=parseInt(elem.style.left)+newX-oldX+"px"; elem.style.top=parseInt(elem.style.top)+newY-oldY+"px"; oldX=newX; oldY=newY; } document.onmouseup=function(){ document.onmousemove=null; document.onmousedown=null; } } } </script> </body> </html>
相关文章推荐
- Objective-C 宏定义详细介绍
- 并行DDL浅析
- struts2 中映射文件 一个列在resultMap中引用2次必须都列出来
- AutoCompleteTextView控件的使用
- LeetCode-Q2-Add Two Numbers
- poj1338
- Spring-data-mongodb 连接 MongoDB 3.x 版本的XML文件配置
- noip2016游记
- AES加密
- poj3304——Segments(判断直线与多个线段相交)
- 那些我在jsp自定义标签中犯过的拼写错误
- SVG文字缩放(文字多的时候不兼容火狐和IE)
- js与jquery实时监听输入框值的oninput与onpropertychange方法
- 使用CSS3实现一个3D相册
- 自定义Toast实现自定义Toast布局
- [模板]哈夫曼树
- 2016 HHU新生赛业余组 题解
- jq基础笔记
- ElasticSearch.net NEST批量创建修改删除索引完整示例
- PAT(basic level) 1039 到底买不买(20)