拖动工具泡

2019-12-21  本文已影响0人  squidbrother
概述

一个拖动工具泡,拖动,点击触发事件,做了移动端与PC端兼容

展示:

展示

涉及的点:
1.事件绑定,删除事件,浏览器兼容性
2.阻止浏览器默认事件 兼容
3.拖动 (配置的 吸附与边界)
4.鼠标按下抬起(手指按下抬起)的事件处理函数 与 拖动按钮移动产生冲突 的判定

参数:
fragFn(objId,userOptions,clickEvtFn);

objId:
  DOM对象id
userOptions:
  adsorb - 拖动结束,进行吸附 / 默认值 false;
  limitRange - 限制范围 / 默认值 false; 
  spaceSide - 距离屏幕边缘的距离 / 默认值0;
clickEvtFn:
  点击触发的事件处理函数

html部分

<!DOCTYPE html>
<html style="overflow: hidden; height: 100%;">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta charset="utf-8" />
    <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <!-- <meta id="viewport" name="viewport" content="width=480,minimal-ui,maximum-scale=1.0"> -->
    <meta name="format-detection" content="telephone=no, email=no" />
    <!-- 启用360浏览器的极速模式(webkit) -->
    <meta name="renderer" content="webkit" />
    <!-- 删除默认的苹果工具栏和菜单栏 -->
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
    <meta name="HandheldFriendly" content="true" />
    <!-- 微软的老式浏览器 -->
    <meta name="MobileOptimized" content="320" />
    <!-- UC强制全屏 -->
    <meta name="full-screen" content="yes" />
    <!-- QQ强制全屏 -->
    <meta name="x5-fullscreen" content="true" />
    <!-- UC应用模式 -->
    <meta name="browsermode" content="application" />
    <!-- QQ应用模式 -->
    <meta name="x5-page-mode" content="app" />
    <!-- windows phone 点击无高光 -->
    <meta name="msapplication-tap-highlight" content="no" />
    <title>游戏登录</title>
</head>
<body>
    <!--菜单-->
    <dl id="popBox" class="popBox">
      <dt>菜单
        <b id="closeBtn" class="closeBtn">×</b></dt>
      <dd>
        <p>内容</p>
      </dd>
    </dl>
    <!--功能按钮-->
    <div id="gameOptions" class="gameOptions"></div>
    <!--游戏区域-->
    <div id="mainbox">
      <!--占位 游戏真实连接 暂用www.baidu.com-->
      <div class="gameBan gameBg">
        <!-- <iframe id="gameFrame" src="https://www.baidu.com"></iframe> --></div>
    </div>
  </body>
</html>

css部分

html{overflow:hidden;height:100%;}body{margin:0;}dl,dt,dd,p{margin:0;padding:0;}html,body{height:100%;-webkit-user-select:none;user-select:none;font-family:PingHei,"Helvetica Neue",Helvetica,"microsoft yahei";font-size:12px;background:#3a3e4a;}#mainbox{transform:scale(1);width:100%;max-width:640px;height:100%;left:0px;top:0px;margin:auto;overflow:hidden;padding:0px;font-family:Arial,宋体;z-index:1000;}#mainbox .gameBan{height:100%;background:#222;}#gameFrame{width:100%;height:100%;border:medium none;overflow-x:hidden;overflow-y:auto;}.gameOptions{position:fixed;left:-20px;top:50%;z-index:999;width:30px;height:30px;padding:10px;cursor:move;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAXR0lEQVR4nO1dz28T19ruX+AZQLfYCbgGFvRDptBegVQsCgLxCZmqoLC4BC8aqaBAqxJ6hRxKU4HUGCkREkhxSoTopYrVLBsc0dVFcgxSu4sNXRPrrm2Tb/GtciOdu8gM19jvmTkzc37N5H2kZxfH43PmOe+P877nvPMOAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUBsOJjr2N9BU/UzIbyhaw5TOIceYQ3g0VgsdtMwjIphGDXDMBqGYawYhkEobFh/Nx+Lxe6apnlU9e/YyMA55AxrQIdisdgjl0H0yvlYLHbTNM39qn9j1GFZhKuWIHAOecA0zaPWgPIaTCfWTNMcUv2bowZrDms4hxwheVB7TPmGGGSBsF0ohXMYTaEEHdRkMkmS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h0v71xdo5eV4MZgeKBz+QnzZZ6Gpt2EWFYSDRialp8wt91CAlFdGoBlJdEBrfxE9fV+zG23kMJVCwTLSqIFQ8PyE+a2W+jhVaoby0qiBx3LT5jbbiGBqGy3xbKSaAJ6z1Tvt1HeM7P7wXtcLFUWBMtKogvdyk8oLhZoQSq6ZLGwrCS6oG0cqjrBkxKk92axoBMpVO2DYFlJtKHTxiElzdu7DwIJRMXdIA795lhWEhHQrIgKj4US6/YuxlCeWoWqKcVjWFYSMUBWREWwDsW6tHJ301i/SlipqiH3yvHWH0QoAaV8ZS/IDt5KCnxoSNUyywEwON840KHKl1LGRD9bjbaRI8uKYNXuxoLKYJ0SnLuXMRnARk4ymRR+gBzU+mhZDzwMLqKgVfmKLj9ptlu0xdi9CJZWVCbyAAdKYE4Mw2hg1W60AVkR0SKhuFbsRbAGsKsu6qEdxAGfT4SIFGhWxDDENOzRPBVPi7HTQ2cyGW7uVnmhDBYkYuZqY4HmtfAM2pvtFjk3OEgTx4rnfTboILnOmCRI4N5st5yUTAzDqKFrtbFgAP1InYtykFqtSnURPIM3cJxLy2p1PrgXodjCoFmNDnGk+A4/IgwwgHrATg5fGmYWSrPdIpXqIi0Y5+epQMcBQTw3OEimikVSqS6SSnWRLNVrZKleI+WFMhkvFEgmk3ETBooD4SoSw1jfm8uP5slsqfTW+1ZeKJOpYpEMXxpmeddILBa7y8VTcXK3OLKCbhXinXfomS2OXOG+fWCd/l4T9bAoDkQnrEV5RcD7VhNa+GplHBo8hGFZjZSwh0WEGqZpmhytidyF2IpN5v08aCwWu4mtswgvsN43rwuzvQir9VBM0zxjtVLOG+tBVsN6uJrFeRQFggfMdVy1LEvnu9boeNfuKhcFAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAiv+A9JmMfryRYqiAAAAABJRU5ErkJggg==);background-repeat:no-repeat;background-color:rgba(0,0,0,0.8);background-position:center center;opacity:0.5;-webkit-background-size:60%;-moz-background-size:60%;-ms-background-size:60%;-o-background-size:60%;background-size:60%;-webkit-border-radius:50%;-moz-border-radius:50%;-ms-border-radius:50%;-o-border-radius:50%;border-radius:50%;}.popBox{position:absolute;left:50%;top:50%;z-index:999999;width:300px;height:300px;margin:-150px 0 0 -150px;background:#FFF;border:1px #ccc solid;display:none;}.popBox dt{padding:5px;overflow:hidden;text-align:center;font-size:16px;line-height:1.5em;border-bottom:1px #ccc solid;text-align:left;}.popBox dt .closeBtn{float:right;display:inline-block;width:18px;height:18px;margin:3px 3px 0 0;text-align:center;line-height:18px;background-color:#0077AA;color:#FFF;}.popBox dd{padding:10px;}.popBox dd p{font-size:14px;line-height:1.5em;}.showIt{display:block;}

js部分

;(function(){function offsetL(obj){var result={};while(obj.parentNode){result.l+=obj.offsetLeft;result.t+=obj.offsetTop;obj=obj.parentNode}return result}function myhasClass(obj,cls){cls=cls||'';if(cls.replace(/\s/g,'').length==0)return false;return new RegExp(' '+cls+' ').test(' '+obj.className+' ')}function myaddClass(obj,cls){if(!myhasClass(obj,cls)){obj.className=obj.className==''?cls:obj.className+' '+cls}}function myremoveClass(obj,cls){if(myhasClass(obj,cls)){var newClass=' '+obj.className.replace(/[\t\r\n]/g,'')+' ';while(newClass.indexOf(' '+cls+' ')>=0){newClass=newClass.replace(' '+cls+' ',' ')}obj.className=newClass.replace(/^\s+|\s+$/g,'')}}function setCss3(obj,css3name,value){var str=css3name.charAt(0).toUpperCase()+css3name.substring(1);obj.style['Webkit'+str]=value;obj.style['Moz'+str]=value;obj.style['ms'+str]=value;obj.style['O'+str]=value;obj.style[css3name]=value}function removeCss3(obj,css3name){var str=css3name.charAt(0).toUpperCase()+css3name.substring(1);obj.style['Webkit'+str]='none';obj.style['Moz'+str]='none';obj.style['ms'+str]='none';obj.style['O'+str]='none';obj.style[css3name]='none'}function myBindEvent(obj,evt,fn,captureFn){if(obj.addEventListener){obj.addEventListener(evt,fn,captureFn)}else if(obj.attachEvent){obj.attachEvent('on'+evt,fn)}}function myRemoveEvent(obj,evt,fn){if(obj.removeEventListener){obj.removeEventListener(evt,fn)}else if(obj.detachEvent){obj.detachEvent('on'+evt,fn)}}var browser={versions:function(){var u=navigator.userAgent,app=navigator.appVersion;return{trident:u.indexOf('Trident')>-1,presto:u.indexOf('Presto')>-1,webKit:u.indexOf('AppleWebKit')>-1,gecko:u.indexOf('Gecko')>-1&&u.indexOf('KHTML')==-1,mobile:!!u.match(/AppleWebKit.*Mobile.*/),ios:!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),android:u.indexOf('Android')>-1||u.indexOf('Adr')>-1,iPhone:u.indexOf('iPhone')>-1,iPad:u.indexOf('iPad')>-1,Safari:u.indexOf('Safari')>-1,weixin:u.indexOf('MicroMessenger')>-1,qq:u.match(/\sQQ/i)==" qq"}}(),language:(navigator.browserLanguage||navigator.language).toLowerCase()};function fragFn(objId,userOptions,clickEvtFn){var obj=document.getElementById(objId);var otherEventEle=null,moveToX=0,moveToY=0,objCenterX=0,objCenterY=0,passiveListener={};obj.cantouch=true;obj.screenW=function(){return document.documentElement.clientWidth};obj.screenH=function(){return document.documentElement.clientHeight};defaultOption={'limitRange':false,'adsorb':false,'spaceSide':0};userOptions=Object.assign({},defaultOption,userOptions);var myDragStart='',myDragMove='',myDragEnd='';if(browser.versions.mobile){otherEventEle=obj;myDragStart='touchstart';myDragMove='touchmove';myDragEnd='touchend';passiveListener={passive:true,capture:false}}else{otherEventEle=document;myDragStart='mousedown';myDragMove='mousemove';myDragEnd='mouseup';passiveListener=false}myBindEvent(obj,myDragStart,touchStartFn,passiveListener);function touchStartFn(ev){if(!obj.cantouch){return false}var oEv=ev||window.event;var startX=oEv.type==='touchstart'?oEv.targetTouches[0].pageX:oEv.clientX;var startY=oEv.type==='touchstart'?oEv.targetTouches[0].pageY:oEv.clientY;obj.disX=startX-obj.offsetLeft;obj.disY=startY-obj.offsetTop;myBindEvent(otherEventEle,myDragMove,MoveFn,false);myBindEvent(otherEventEle,myDragEnd,EndFn,passiveListener);obj.clockTimeStart=new Date().getTime();if(!browser.versions.mobile){if(typeof obj.setCapture!=='undefined'){obj.setCapture()}return false}}function MoveFn(ev){if(!obj.cantouch){return false}var oEv=ev||window.event;var moveX=oEv.type==='touchmove'?oEv.targetTouches[0].pageX:oEv.clientX;var moveY=oEv.type==='touchmove'?oEv.targetTouches[0].pageY:oEv.clientY;moveToX=moveX-obj.disX;moveToY=moveY-obj.disY;if(userOptions.limitRange){if(moveToX<0){moveToX=0}else if(moveToX>(obj.screenW()-obj.offsetWidth)){moveToX=obj.screenW()-obj.offsetWidth}if(moveToY<0){moveToY=0}else if(moveToY>(obj.screenH()-obj.offsetHeight)){moveToY=obj.screenH()-obj.offsetHeight}}obj.style.left=moveToX+'px';obj.style.top=moveToY+'px';if(browser.versions.mobile){oEv.preventDefault()}}function EndFn(ev){if(!obj.cantouch){return false}var oEv=ev||window.event;obj.cantouch=false;objCenterX=obj.offsetLeft+obj.offsetWidth/2;objCenterY=obj.offsetTop+obj.offsetHeight/2;if(userOptions.adsorb){if(objCenterX<obj.screenW()/2){obj.style.left=userOptions.spaceSide+'px'}else{obj.style.left=(obj.screenW()-obj.offsetWidth-userOptions.spaceSide)+'px'}setCss3(obj,'transition','all ease 0.3s');clearTimeout(obj.iTimer);obj.iTimer=setTimeout(function(){obj.cantouch=true;removeCss3(obj,'transition')},400)}if(!browser.versions.mobile){if(typeof obj.releaseCapture!='undefined'){obj.releaseCapture()}myRemoveEvent(document,myDragMove,MoveFn);myRemoveEvent(document,myDragEnd,EndFn)};if(new Date().getTime()-obj.clockTimeStart<150){clickEvtFn&&clickEvtFn()}}}fragFn('gameOptions',{adsorb:true,limitRange:true,spaceSide:10},showPopFn);var oPopBox=document.getElementById('popBox');var oCloseBtn=document.getElementById('closeBtn');function showPopFn(){myaddClass(oPopBox,'showIt')}myBindEvent(oCloseBtn,'mouseup',function(){myremoveClass(oPopBox,'showIt')})})();
上一篇下一篇

猜你喜欢

热点阅读