Javascript實現蘋果懸浮虛擬按鈕,javascript懸浮

來源:互聯網
上載者:User

Javascript實現蘋果懸浮虛擬按鈕,javascript懸浮

Javascript實現蘋果懸浮虛擬按鈕

直接引入代碼到頁面即可
代碼有部分冗餘的地方,有興趣的小夥伴可也自己修改
如果有什麼BUG 記得評論 告訴我哦

web-touch.js

var new_element_N=document.createElement("style");   new_element_N.innerHTML = '#drager {' +    '   position: fixed;' +    '   width: 35px;' +    '   height: 35px;' +    '   background-color: rgba(0, 0, 0, 0.2);' +    '   z-index: 10000;' +    '   cursor: pointer;' +    '   top: 0px;' +    '   left: 0px;' +    '   border-radius: 30%;' +    '   padding: 6px;' +    ' }' +    ' ' +    ' #drager>div {' +    '   border-radius: 50%;' +    '   width: 100%;' +    '   height: 100%;' +    '   background-color: rgba(0, 0, 0, 0.3);' +    '   transition: all 0.2s;' +    '  -webkit-transition: all 0.2s;' +    '  -moz-transition: all 0.2s;' +    '  -o-transition: all 0.2s;' +    ' }' +    ' #drager:hover>div{' +    '   background-color: rgba(0, 0, 0, 0.6);' +    ' } ';  document.body.appendChild(new_element_N);  new_element_N=document.createElement('div');   new_element_N.setAttribute("id","drager");  new_element_N.style.top="100px";  new_element_N.style.left="100px";  new_element_N.innerHTML = ' <div></div>' ;  document.body.appendChild(new_element_N);  //   //     var posX;    var posY;       var screenWidth =document.documentElement.clientWidth;    var screenHeight = document.documentElement.clientHeight;      var fdiv = document.getElementById("drager");     fdiv.onmousedown=function(e)    {       screenWidth =document.documentElement.clientWidth;      screenHeight = document.documentElement.clientHeight;        if(!e){ e = window.event; } //IE      posX = e.clientX - parseInt(fdiv.style.left);      posY = e.clientY - parseInt(fdiv.style.top);      document.onmousemove = mousemove;          }    document.onmouseup = function()//釋放時自動貼到最近位置    {      document.onmousemove = null;      if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(screenHeight/2)){//在上半部分        if((parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)<=(screenWidth/2)){//在左半部分          if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)){//靠近上方            fdiv.style.top="0px";          }else{//靠近左邊            fdiv.style.left="0px";          }        }else{//在右半部分          if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(screenWidth-(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)) ){//靠近上方            fdiv.style.top="0px";          }else{//靠近右邊            fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";          }         }      }else{ //下半部分         if((parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)<=(screenWidth/2)){//在左半部分          if( (screenHeight-(parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2))<=(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)){//靠近下方            fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";          }else{//靠近左邊            fdiv.style.left="0px";          }        }else{//在右半部分          if( (screenHeight-(parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2))<=(screenWidth-(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)) ){//靠近上方            fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";          }else{//靠近右邊            fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";          }         }      }     }    function mousemove(ev)    {      if(ev==null){ ev = window.event;}//IE      if((ev.clientY - posY)<=0){//超過頂部         fdiv.style.top="0px";      }else if((ev.clientY - posY) >(screenHeight-parseInt(fdiv.clientHeight))){//超過底部        fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";      }else{        fdiv.style.top = (ev.clientY - posY) + "px";      }              if((ev.clientX- posX)<=0){//超過左邊         fdiv.style.left="0px";      }else if((ev.clientX - posX) >(screenWidth-parseInt(fdiv.clientWidth))){//超過右邊        fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";      }else{        fdiv.style.left = (ev.clientX - posX) + "px";      }      // console.log( posX +" "+ fdiv.style.left);           }    window.onload = window.onresize = function() { //視窗大小改變事件      screenWidth =document.documentElement.clientWidth;      screenHeight = document.documentElement.clientHeight;        if( (parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight))>screenHeight){//視窗改變適應超出的部分         fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";      }        if( (parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth))>screenWidth){//視窗改變適應超出的部分         fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";      }        document.onmouseup.apply()    };    fdiv.addEventListener('touchstart', fdiv.onmousedown, false);      fdiv.addEventListener('touchmove', function(event) {            // 如果這個元素的位置內只有一個手指的話            if (event.targetTouches.length == 1) {               event.preventDefault();// 阻止瀏覽器預設事件,重要               var touch = event.targetTouches[0];               if((touch.pageY)<=0){//超過頂部                fdiv.style.top="0px";              }else if(touch.pageY>(screenHeight-parseInt(fdiv.clientHeight))){//超過底部                fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";              }else{                fdiv.style.top = (touch.pageY-parseInt(fdiv.clientHeight)/2) + "px";              }                             if(touch.pageX<=0){//超過左邊                fdiv.style.left="0px";              }else if( touch.pageX >(screenWidth-parseInt(fdiv.clientWidth))){//超過右邊                fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";              }else{                fdiv.style.left = (touch.pageX-parseInt(fdiv.clientWidth)/2) + "px";              }            }          }, false);     fdiv.addEventListener('touchend', document.onmouseup , false);           fdiv.ondblclick=function(){//雙擊事件可能在手機端瀏覽器會與網頁縮放事件衝突      alert("發揮你們的想象力吧");    }

html

<!doctype html><html ><head>  <meta charset="UTF-8">  <title>Document</title>     </head><body>   </body><script src="web-touch.js" type="text/javascript"></script></html>

示範圖

您可能感興趣的文章:
  • JavaScript 懸浮視窗實現代碼
  • javascript 滑鼠懸浮圖片顯示原圖 移出滑鼠後原圖消失(多圖)
  • JS實現懸浮移動視窗(懸浮廣告)的特效
  • js QQ客服懸浮效果實現代碼
  • JS實現自動固定頂部的懸浮功能表列效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.