Javascript實(shí)現(xiàn)蘋(píng)果懸浮虛擬按鈕
Javascript實(shí)現(xiàn)蘋(píng)果懸浮虛擬按鈕
直接引入代碼到頁(yè)面即可
代碼有部分冗余的地方,有興趣的小伙伴可也自己修改
如果有什么BUG 記得評(píng)論 告訴我哦
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()//釋放時(shí)自動(dòng)貼到最近位置
{
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){//超過(guò)頂部
fdiv.style.top="0px";
}else if((ev.clientY - posY) >(screenHeight-parseInt(fdiv.clientHeight))){//超過(guò)底部
fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
}else{
fdiv.style.top = (ev.clientY - posY) + "px";
}
if((ev.clientX- posX)<=0){//超過(guò)左邊
fdiv.style.left="0px";
}else if((ev.clientX - posX) >(screenWidth-parseInt(fdiv.clientWidth))){//超過(guò)右邊
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){//窗口改變適應(yīng)超出的部分
fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
}
if( (parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth))>screenWidth){//窗口改變適應(yīng)超出的部分
fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
}
document.onmouseup.apply()
};
fdiv.addEventListener('touchstart', fdiv.onmousedown, false);
fdiv.addEventListener('touchmove', function(event) {
// 如果這個(gè)元素的位置內(nèi)只有一個(gè)手指的話
if (event.targetTouches.length == 1) {
event.preventDefault();// 阻止瀏覽器默認(rèn)事件,重要
var touch = event.targetTouches[0];
if((touch.pageY)<=0){//超過(guò)頂部
fdiv.style.top="0px";
}else if(touch.pageY>(screenHeight-parseInt(fdiv.clientHeight))){//超過(guò)底部
fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
}else{
fdiv.style.top = (touch.pageY-parseInt(fdiv.clientHeight)/2) + "px";
}
if(touch.pageX<=0){//超過(guò)左邊
fdiv.style.left="0px";
}else if( touch.pageX >(screenWidth-parseInt(fdiv.clientWidth))){//超過(guò)右邊
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(){//雙擊事件可能在手機(jī)端瀏覽器會(huì)與網(wǎng)頁(yè)縮放事件沖突
alert("發(fā)揮你們的想象力吧");
}
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>
演示圖

相關(guān)文章
javascript實(shí)現(xiàn)炫酷的拖動(dòng)分頁(yè)
非??岬膉avascript拖動(dòng)分頁(yè)功能,無(wú)縫循環(huán)分頁(yè),拖動(dòng)鼠標(biāo)即可完成分頁(yè),鼠標(biāo)向左拖動(dòng)回到前一頁(yè),向右拖動(dòng)則翻開(kāi)第二頁(yè),還帶有動(dòng)畫(huà)特效,著實(shí)很不錯(cuò),界面黑色,非主流風(fēng)格,相信很多人會(huì)喜歡的。2015-05-05
JavaScript中實(shí)現(xiàn)異步編程模式的4種方法
這篇文章主要介紹了JavaScript中實(shí)現(xiàn)異步編程模式的4種方法,本文講解了回調(diào)函數(shù)、事件監(jiān)聽(tīng)、發(fā)布/訂閱、Promises對(duì)象4種方法,需要的朋友可以參考下2014-09-09
ES6新特性之?dāng)?shù)組、Math和擴(kuò)展操作符用法示例
這篇文章主要介紹了ES6新特性之?dāng)?shù)組、Math和擴(kuò)展操作符用法,結(jié)合實(shí)例形式分析了ES6中數(shù)組、Math和擴(kuò)展操作符的新特性、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04
JavaScript庫(kù) 開(kāi)發(fā)規(guī)則
1. 保持無(wú)侵入性,標(biāo)記不想知道你的JavaScript代碼;2. 嚴(yán)禁修改和擴(kuò)展Object.prototype!;3. 對(duì)JavaScript內(nèi)建對(duì)象的擴(kuò)展越少越好;4. 跟隨標(biāo)準(zhǔn);5. 或著跟隨主導(dǎo) ;6. 保持靈活;7. 管理內(nèi)存;8. 淘汰瀏覽器嗅探;9. 小巧更佳……2009-01-01
小程序開(kāi)發(fā)中如何使用async-await并封裝公共異步請(qǐng)求的方法
在平常的項(xiàng)目開(kāi)發(fā)中肯定會(huì)遇到同步異步執(zhí)行的問(wèn)題,這篇文章主要介紹了小程序開(kāi)發(fā)中如何使用async-await并封裝公共異步請(qǐng)求的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
js 綁定帶參數(shù)的事件以及手動(dòng)觸發(fā)事件
js 綁定帶參數(shù)的事件以及手動(dòng)觸發(fā)事件,需要的朋友可以參考下。2010-04-04
教你3分鐘利用原生js實(shí)現(xiàn)有進(jìn)度監(jiān)聽(tīng)的文件上傳預(yù)覽組件
這篇文章主要給大家介紹了關(guān)于如何3分鐘利用原生js實(shí)現(xiàn)有進(jìn)度監(jiān)聽(tīng)的文件上傳預(yù)覽組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用js具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

