WEB 手机版拖拽
2015-05-06 21:02
190 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <title></title> </head> <style> html,body{ width:100%; height:100%; overflow: hidden; } html , body, div{ padding:0; margin:0; } #drag{ width:100px; height:100px; background:pink; -webkit-transform:translate3d(0px,0px,0px); } </style> <body> <div id="drag"></div> <script> function dragMove( obj ){ var oDrag = document.querySelector( obj ) , startX , startY , offsetX , offsetY ; //开始 var dragStart = function( event ){ //阻止冒泡行为 event.stopPropagation(); //把当前获取的位置进行处理 var nowLocation = window.getComputedStyle(oDrag , null)['-webkit-transform'].replace(/[a-z()]/g , ''); var nowLocationArr = nowLocation.split(','); //起始值 减去 位置值 等于 当前值 this.startX = event.touches[0].pageX - nowLocationArr[4]; this.startY = event.touches[0].pageY - nowLocationArr[5]; } //移动 var dragMove = function( event ){ //阻止冒泡及默认行为 event.stopPropagation(); event.preventDefault(); if ( event.targetTouches.length == 1 ) { //移动的 x 和 y 值 this.offsetX = event.targetTouches[0].pageX - this.startX; this.offsetY = event.targetTouches[0].pageY - this.startY; //DIV 块活动区域 var clientX = document.body.clientWidth - window.getComputedStyle( this , null )['width'].replace('px',''); var clientY = document.body.clientHeight - window.getComputedStyle( this , null )['height'].replace('px',''); if ( this.offsetX < 0 ) { this.offsetX = 0; }else if ( this.offsetX > clientX ) { this.offsetX = clientX; } if ( this.offsetY < 0 ) { this.offsetY = 0; }else if ( this.offsetY > clientY ) { this.offsetY = clientY; } this.style['-webkit-transform'] = 'translate3d( '+ this.offsetX +'px , '+ this.offsetY +'px , 0px)'; } } //监听事件 oDrag.addEventListener('touchstart' , dragStart ); oDrag.addEventListener('touchmove' , dragMove); } //调用 dragMove('#drag'); </script> </body> </html>
相关文章推荐
- 针对在webview模式中,小米魅族手机不支持html5原生video的control的解决办法![原创]
- 将本地web项目运行到手机上查看。
- 手机web——自适应网页设计
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
- 手机web模板 [内含199个模板][待更新...]
- Webview--如何让加载进来的页面自适应手机屏幕分辨率居中显示
- Js 禁止手机页面拖拽
- 针对手机拍出的视频不能在web网页播放器插件播放问题-->java对手机视频的处理
- 使用easydrag轻松实现web控件的拖拽效果
- 正式商业旗舰版Modoer点评系统3.4新增手机WEB端加微信公众平台接口,完整包加商业模块
- <WEB>平板_手机开发_13 个处理触摸事件和多点触摸的JS 库
- 部分华为Android7.0手机 WebView滑动卡顿
- webstorm 配置webserver选项,让其他ip设备可访问(如手机等测试效果)
- WEB 移动网站 手机点击 打电话 发短信
- 通过Chrome的inspect对手机webview进行调试
- VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
- 实现手机扫描二维码页面登录,类似web微信-第一篇,业务分析
- HTML5+JS手机web开发之jQuery Mobile初涉
- DELPHI XE5开发WEB服务器及安卓手机客户端