欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生js實現(xiàn)可拖動的登錄框效果

 更新時間:2017年01月21日 10:32:12   作者:夏天不做夢  
本文主要介紹了原生js實現(xiàn)可拖動的登錄框效果的示例代碼。具有一定的參考價值,下面跟著小編一起來看下吧

實現(xiàn)原理

1.onmousemove事件觸發(fā)時不斷更新鼠標的pageXY改變位置,

登陸框的偏移量=鼠標當前位置-鼠標到登錄框邊框的距離

2.onmousedown鼠標摁下時觸發(fā)事件獲取鼠標到登陸框的距離,再設(shè)置true允許拖拽

3.onmouseup 鼠標彈起設(shè)置false停止拖拽

4.登錄框居中顯示公式:(可視區(qū)域?qū)捀?登錄框?qū)捀?/2

5.當瀏覽器窗口大小變化時觸發(fā)事件window.onresize 再更新登陸框居中顯示

代碼中有詳細的注釋

完整代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
body,button,input,select,textarea{font:12px/1.5 tahoma,arial,\5b8b\4f53;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
small{font-size:12px;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:underline;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
/*p{font-size: 100px;}*/
#btn{width: 80px;
 height: 40px;
 background: #3b7ae3;
 margin:0 auto;
 display: block;
 cursor: pointer;
 border-style: none;
 color: #fff;
 font-size: 16px;}
#mask{
 background: #000;
 opacity: 0.75;
 filter: alpha(opacity=75);
 height: 1000px;
 width: 100%;
 position: absolute;
 left: 0;
 top: 0;
 z-index: 1000;
}
#login{position: absolute; top: 100px; left: 100px; width: 400px; height: auto; border:1px solid #d5d5d5; z-index: 1001; }
.title{position: relative;background-color: #f7f7f7; cursor: move; height: 50px; line-height: 50px; font-size: 16px; color: #333; padding-left:30px;}
.close{position: absolute; top:0; right: 10px; color: #ccc;}
.content{background: #fff; padding: 15px 20px;}
.user{margin-bottom: 15px;}
.password{margin-bottom: 15px;}
.pt{display: block;
 height: 38px;
 padding-left: 15px;
 border: 1px solid #ddd;
 transition: .3s;
 font-size: 14px;
 color: #666;
 width: 343px;
 }
.sm{display: block;
 height: 48px;
 border: 1px solid #ddd;
 transition: .3s;
 font-size: 16px;
 color: #666;
 width: 360px;
 background: #3b7ae3;
 color: #fff;}
</style> 
</head> 
<body>
 <!-- <div id="mask"></div> -->
 <button id="btn" href="">登錄</button>
 <!-- <div class="login" id="login">
 <div class="title" id="title">登錄百度賬號<a href="#" class="close">x</a></div>
 <div class="content">
 <div class="user"><input class="pt" type="input" value="手機/郵箱/用戶名"></div>
 <div class="password"><input class="pt" type="input" value="密碼"></div>
 <div class="submit"><input class="sm" type="submit" value="登錄"></div>
 </div>
 </div> -->
<script type="text/javascript">
 function b(){ 
 //創(chuàng)建遮罩層div并插入body
 var mask=document.createElement("div");
 mask.id="mask";
 mask.style.height=cheight+"px";
 //寬度直接用100%在樣式里
 document.body.appendChild(mask);
 //創(chuàng)建登錄層div并插入body
 var login=document.createElement("div");
 login.id="login";
 login.innerHTML='<div class="title" id="title">登錄百度賬號'+'<a href="#" class="close">x</a>'+'</div>'+
 '<div class="content">'+'<div class="user">'+'<input class="pt" type="input" value="手機/郵箱/用戶名">'+'</div>'+'<div class="password">'+'<input class="pt" type="input" value="密碼">'+'</div>'+'<div class="submit">'+'<input class="sm" type="submit" value="登錄">'+'</div>'+'</div>';
 document.body.appendChild(login);
 //窗口可視區(qū)域?qū)挾?
 var cwidth= document.documentElement.clientWidth || document.body.clientWidth;
 //窗口可視區(qū)域高度
 var cheight= document.documentElement.clientHeight || document.body.clientHeight;
 //登錄框?qū)挾?
 var lwidth=login.offsetWidth;
 //登錄框高度
 var lheight=login.offsetHeight;
 //設(shè)置登錄框的居中顯示
 login.style.left=(cwidth-lwidth)/2+"px";
 login.style.top=(cheight-lheight)/2+"px";
 //設(shè)置遮罩層的高度
 mask.style.height=cheight+"px";
 //改變窗口大小后依然居中顯示
 window.onresize=function(){
 if(document.compatMode=="CSS1Compat"){  
cwidth=document.documentElement.clientWidth;
cheight=document.documentElement.clientHeight;
 }else{  
  cwidth=document.body.clientWidth;
  cheight=document.body.clientHeight;
 }
 login.style.left=(cwidth-lwidth)/2+"px";
 login.style.top=(cheight-lheight)/2+"px";
 mask.style.height=cheight+"px";
 }
 //獲取拖拽容器
 var title=document.getElementById("title");
 var isDraging=false;
 var mouseOffsetX;
 var mouseOffsetY;
 //鼠標按下事件
 title.onmousedown=function(e){
 var e=e||window.event;
 /*var el=e.srcElement;
 if(!el){
 el=e.target;//兼容火狐
 }*/
 //鼠標相對于登錄框的位置
 mouseOffsetX=e.pageX-login.offsetLeft;
 mouseOffsetY=e.pageY-login.offsetTop;
 //鼠標摁下時為true
 isDraging=true;
 /*console.log(mouseOffsetY, mouseOffsetX)*/
 }
 //鼠標移動事件
 document.onmousemove=function(e){
 var e=e||window.event;
 //鼠標移動時的坐標
 var newMX=e.pageX;
 var newMY=e.pageY;
 //判斷為true時可以拖拽
 if(isDraging===true){
 //登錄框的偏移值=當前位置-鼠標到登錄框的距離
 var loginL=newMX-mouseOffsetX;
 var loginT=newMY-mouseOffsetY;
 //如果left top值超過邊緣時就讓他等于邊緣
 if(loginL<0){
 loginL=0;
 }else if(loginL>(cwidth-lwidth)){
 loginL=cwidth-lwidth;
 }
 if(loginT<0){
 loginT=0;
 }else if(loginT>(cheight-lheight)){
 loginT=cheight-lheight;
 }
 login.style.left=loginL+"px";
 login.style.top=loginT+"px";
 } 
 }
 //鼠標彈起時設(shè)置為不可拖拽
 document.onmouseup=function(){
 isDraging=false;
 }
 //點擊X關(guān)閉登錄框和彈出層
 var close=login.getElementsByClassName("close")[0];
 close.onclick=function(){
 document.body.removeChild(mask);
 document.body.removeChild(login);
 }
 }
 //點擊登錄彈出登錄框和彈出層
 window.onload=function(){
 var btn=document.getElementById("btn");
 btn.onclick=function(){
 b();
 }
 }
</script> 
</body> 
</html> 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • 原生JS中slice()方法和splice()區(qū)別

    原生JS中slice()方法和splice()區(qū)別

    slice()方法和splice()方法都是原生js中對數(shù)組操作的方法。那么他兩種有什么區(qū)別呢?今天通過本文教程給大家簡單介紹下,需要的朋友可以參考下
    2017-03-03
  • js 函數(shù)式編程學(xué)習(xí)筆記

    js 函數(shù)式編程學(xué)習(xí)筆記

    這兩天看書看到了函數(shù)式編程那節(jié),感覺學(xué)到了挺多東西,之前看別人代碼都寫的挺漂亮,現(xiàn)在我寫的代碼是有點亂,所以還是要學(xué)習(xí)下別人的編程模式
    2017-03-03
  • JavaScript代碼異常監(jiān)控實現(xiàn)過程詳解

    JavaScript代碼異常監(jiān)控實現(xiàn)過程詳解

    這篇文章主要介紹了JavaScript代碼異常監(jiān)控實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • uniapp開發(fā)APP之強制更新和熱更新的實現(xiàn)

    uniapp開發(fā)APP之強制更新和熱更新的實現(xiàn)

    使用uni-app開發(fā),可將代碼編譯到iOS、Android、微信小程序等多個平臺,升級時也需考慮多平臺同步升級,下面這篇文章主要給大家介紹了關(guān)于uniapp開發(fā)APP之強制更新和熱更新的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • JavaScript switch case 的用法實例[范圍]

    JavaScript switch case 的用法實例[范圍]

    JavaScript switch case 的用法實例,大家可以參考下。
    2009-09-09
  • 原生JS實現(xiàn)幾個常用DOM操作API實例

    原生JS實現(xiàn)幾個常用DOM操作API實例

    下面小編就為大家?guī)硪黄鶭S實現(xiàn)幾個常用DOM操作API實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • 利用JavaScript如何查詢某個值是否數(shù)組內(nèi)

    利用JavaScript如何查詢某個值是否數(shù)組內(nèi)

    這篇文章主要給大家介紹了關(guān)于利用JavaScript如何查詢某個值是否數(shù)組內(nèi)的相關(guān)資料,文中通過示例代碼分別介紹了實現(xiàn)該問題的一些解決方法是否可行,需要的朋友可以參考借鑒,下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • js設(shè)置文字顏色的方法示例

    js設(shè)置文字顏色的方法示例

    這篇文章主要介紹了js設(shè)置文字顏色的方法,涉及JS頁面元素樣式操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-12-12
  • js中string轉(zhuǎn)int把String類型轉(zhuǎn)化成int類型

    js中string轉(zhuǎn)int把String類型轉(zhuǎn)化成int類型

    今天碰到一個問題,需要把String類型的變量轉(zhuǎn)化成int類型的,js中String轉(zhuǎn)int和Java中不一樣,不能直接把Java中的用到j(luò)s中
    2014-08-08
  • javascript搜索框效果實現(xiàn)方法

    javascript搜索框效果實現(xiàn)方法

    這篇文章主要介紹了javascript搜索框效果實現(xiàn)方法,可實現(xiàn)顯示默認提示文字的搜索框效果,非常簡單實用,需要的朋友可以參考下
    2015-05-05

最新評論