javascript實現一個簡單的彈出窗,javascript實現

來源:互聯網
上載者:User

javascript實現一個簡單的彈出窗,javascript實現

功能介紹:點擊一個按鈕,然後頁面會彈出一個視窗,而頁面原來的內容會保持不變,只是在其頁面上加了一個遮罩層,設定了不透明度,彈出的視窗可設定在固定位置,也可以自由設定,常見於網站的登入按鈕。

html頁面:

<!DOCTYPE html><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>js實現一個彈出框</title><style type="text/css">/*預先寫好彈出窗的樣式*/#menu{height: 900px;}#close{   width:30px;   height:30px;   cursor:pointer;   position:absolute;   right:5px;   top:5px;   text-indent:-999em;  background-color:blue;  }#mask{   background-color:pink;  opacity:0.5;  filter: alpha(opacity=50);   position:absolute;   left:0;  top:0;  z-index:1;  }#login{   position:fixed;  z-index:2;  }.loginCon{   position:relative;   width:670px;  height:380px;  /*background:url(img/loginBg.png) #2A2C2E center center no-repeat;*/  background-color: #ccc;  }</style></head><body><div id="menu">  <div id="login-area">   <button id="btnLogin">登入</button>  </div></div></body></html>

js代碼:

<script>function openNew(){  //擷取頁面的高度和寬度  var sWidth=document.body.scrollWidth;  var sHeight=document.body.scrollHeight;    //擷取頁面的可視地區高度和寬度  var wHeight=document.documentElement.clientHeight;    var oMask=document.createElement("div");    oMask.id="mask";    oMask.style.height=sHeight+"px";    oMask.style.width=sWidth+"px";    document.body.appendChild(oMask);  var oLogin=document.createElement("div");    oLogin.id="login";    oLogin.innerHTML="<div class='loginCon'><div id='close'>關閉</div></div>";    document.body.appendChild(oLogin);    //擷取登陸框的寬和高  var dHeight=oLogin.offsetHeight;  var dWidth=oLogin.offsetWidth;    //設定登陸框的left和top    oLogin.style.left=sWidth/2-dWidth/2+"px";    oLogin.style.top=wHeight/2-dHeight/2+"px";  //點擊關閉按鈕  var oClose=document.getElementById("close");      //點擊登陸框以外的地區也可以關閉登陸框    oClose.onclick=oMask.onclick=function(){          document.body.removeChild(oLogin);          document.body.removeChild(oMask);          };          };            window.onload=function(){      var oBtn=document.getElementById("btnLogin");        //點擊登入按鈕        oBtn.onclick=function(){          openNew();          return false;          }            }</script>

您可能感興趣的文章:
  • javascript 控制快顯視窗
  • js右下角快顯視窗,點擊可關閉效果
  • JS快顯視窗代碼大全(詳細整理)
  • 讓js快顯視窗居前顯示的實現方法
  • js實現快顯視窗、頁面變成灰色並不可操作的例子分享
  • JavaScript快顯視窗方法匯總
  • 禁止iframe頁面的所有js指令碼如alert及快顯視窗等
  • JS+CSS實現帶關閉按鈕DIV快顯視窗的方法
  • JS+CSS實現Div快顯視窗同時背景變暗的方法
  • JavaScript動態修改快顯視窗大小的方法
  • js實現仿qq訊息的彈出窗效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.