本文實(shí)例為大家分享了js實(shí)現(xiàn)登錄拖拽窗口的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括遷安網(wǎng)站建設(shè)、遷安網(wǎng)站制作、遷安網(wǎng)頁(yè)制作以及遷安網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,遷安網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到遷安省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
做這個(gè)案例的兩個(gè)關(guān)鍵點(diǎn):
1、用js將盒子在可視區(qū)域居中顯示
本可以用css將盒子用定位的方式居中顯示,但是采用js的方法更好些。
方法:
盒子的left值 = (可視區(qū)域的寬度 - 盒子自身的寬度)/ 2;
盒子的top值 = (可視區(qū)域的高度 - 盒子自身的高度)/ 2;
這樣盒子就居中顯示
2、先鼠標(biāo)按下,然后鼠標(biāo)在整個(gè)文檔上移動(dòng)時(shí),盒子的位置跟著鼠標(biāo)移動(dòng)
這一點(diǎn)上要注意的點(diǎn):
1)鼠標(biāo)按下時(shí),鼠標(biāo)相對(duì)盒子X(jué)方向的位置 = event.clienX- 盒子的offsetLeft
鼠標(biāo)相對(duì)盒子Y方向的位置 = event.clientY - 盒子的offsetTop ;
2)鼠標(biāo)在移動(dòng)時(shí),盒子的left值 = event.clientX - 鼠標(biāo)相對(duì)盒子X(jué)方向的位置
盒子的top值 = event.clientY - 鼠標(biāo)相對(duì)盒子Y方向的位置
注意 1),2)中的event.clientX/clientY不是一樣的值,他們分別來(lái)自不同事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{ padding: 0; margin: 0; } button { width: 80px; height: 30px; display: block; margin: 0 auto; background-color:#3b7ae3; border-style: none; border-radius: 5px; color: #ffffff; cursor: pointer; } .mask { position: absolute; top:0; width: 100%; height: 1000px; background-color:black; opacity: 0.75; z-index: 99; } .login { width: 350px; height: auto; border: 1px solid #fff; position: absolute; top:0; left: 0; z-index: 1000; } .title { width:330px; height: 50px; padding-left: 20px; line-height: 50px; background-color: #eee; position: relative; cursor: move; } span { position: absolute; right:10px; font-size: 30px; font-weight: 300; cursor: pointer; } .current { padding: 10px 15px; background-color: #fff; } .user, .password{ margin-bottom: 10px; } .pt { width:308px; height: 40px; padding-left: 10px; } .submit { width: 320px; height: 48px; background-color:#3b7ae3; color: #fff; font-size: 16px; border-style: none; cursor: pointer; } </style> <script> window.onload = function(){ // 獲取元素 function $(id) {return document.getElementById(id);} // 獲得可視區(qū)域的大小 var clientwidth = document.documentElement.clientWidth || document.body.clientWidth; var clientHeight = document.documentElement.clientHeight || document.body.clientHeight; // 點(diǎn)擊登錄按鈕 $("btn").onclick = function(){ // 創(chuàng)建mask var mask = document.createElement("div"); mask.className = "mask"; // mask的高度等于可視化區(qū)域的高度 mask.style.height = clientHeight + "px"; document.body.appendChild(mask); // 創(chuàng)建login var login = document.createElement("div"); login.className = "login"; login.id = "_login"; login.innerHTML = '<div class="title" id="_title"> 登錄百度賬號(hào) '+' <span id="close">×</span>'+' </div>'+' <div class="current">'+ '<div class="user">'+' <input type="text" class="pt" placeholder="手機(jī)/郵箱/用戶名">'+ '</div>'+'<div class="password">'+'<input type="text" class="pt" placeholder="請(qǐng)輸入密碼">'+ '</div>'+'<div>'+' <input type="button" class="submit" value="登錄">'+'</div>'; document.body.appendChild(login); // 設(shè)置login的位置垂直居中 login.style.left= (clientwidth - login.offsetWidth)/2 + "px"; login.style.top = (clientHeight - login.offsetHeight)/2 + "px"; // 當(dāng)窗口改變大小時(shí),login仍然垂直居中顯示 window.onresize = function(){ if(window.innerWidth != null) // ie9+及新的瀏覽器 { clientwidth = window.innerWidth; clientHeight = window.innerHeight; } else if(document.compatMode == "CSS1Compat")// 標(biāo)準(zhǔn)模式 { clientwidth = document.documentElement.clientX; clientHeight = document.documentElement.clientY; } else { clientwidth = document.body.clientX; clientHeight = document.body.clientY; } login.style.left= (clientwidth - login.offsetWidth)/2 + "px"; login.style.top = (clientHeight - login.offsetHeight)/2 + "px"; mask.style.height = clientHeight + "px"; } // 鼠標(biāo)按下title實(shí)現(xiàn)拖拽 $("_title").onmousedown = function(event){ var event = event || window.event; // 鼠標(biāo)相對(duì)盒子的位置 var moveX = event.clientX - login.offsetLeft; var moveY = event.clientY - login.offsetTop; document.onmousemove = function(event){ var event = event || window.event; // 鼠標(biāo)移動(dòng)時(shí)的位置 var clientX1 = event.clientX; var clientY1 = event.clientY; // 盒子的偏移量 = 當(dāng)前鼠標(biāo)的位置 - 鼠標(biāo)按下時(shí)相對(duì)盒子的位置 var loginX = clientX1 - moveX; var loginY = clientY1 - moveY; // 判斷l(xiāng)ogin的位置是否超過(guò)可視區(qū)域 if(loginX <= 0) { loginX = 0; } else if(loginX >= clientwidth - $("_login").offsetWidth) { loginX = clientwidth - $("_login").offsetWidth; } if(loginY <= 0) { loginY = 0; } else if(loginY >= clientHeight - $("_login").offsetHeight) { loginY = clientHeight - $("_login").offsetHeight; } $("_login").style.left = loginX + "px"; $("_login").style.top = loginY + "px"; } document.onmouseup = function(){ document.onmousemove = null; } // 點(diǎn)擊叉號(hào)關(guān)閉 $("close").onclick = function(){ document.body.removeChild(mask); document.body.removeChild(login); } } } } </script> </head> <body> <button id="btn">登錄</button> <!-- <div class="mask" id="_mask"></div> <div class="login" id="_login"> <div class="title" id="_title"> 登錄百度賬號(hào) <span id="close">×</span> </div> <div class="current"> <div class="user"> <input type="text" class="pt" placeholder="手機(jī)/郵箱/用戶名"> </div> <div class="password"> <input type="text" class="pt" placeholder="請(qǐng)輸入密碼"> </div> <div > <input type="button" class="submit" value="登錄"> </div> </div> --> </div> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
名稱欄目:js實(shí)現(xiàn)登錄拖拽窗口
文章網(wǎng)址:http://chinadenli.net/article34/gphdse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、ChatGPT、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、面包屑導(dǎo)航、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)