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

JS實(shí)現(xiàn)的添加彈出層并完成鎖屏操作示例

 更新時(shí)間:2017年04月07日 08:58:52   作者:謝玉勝  
這篇文章主要介紹了JS實(shí)現(xiàn)的添加彈出層并完成鎖屏操作,涉及JS針對(duì)頁(yè)面元素與樣式動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了JS實(shí)現(xiàn)的添加彈出層并完成鎖屏操作。分享給大家供大家參考,具體如下:

上圖:

代碼:

<html>
<head>
 <title>彈出層</title>
 <style type="text/css">
  *{
   padding:0px;
   margin:0px;
  }
  .up{
   width:500px;
   height: 400px;
   border:1px solid silver;
   position: absolute;
   display: none;
   z-index: 9999;
   background:#fff;
/*   top:50%;
   left: 50%;*/
/*   margin-left: -250px;
   margin-top: -200px;*/
  }
  .up h2{
   background-color: #f2f2f2;
   text-align: center;
  }
  .con span{
   width:20px;
   height:30px;
   text-align: center;
   line-height: 30px;
   background-color:red;
   cursor: pointer;
  }
  .mask{
   width:3000px;
   height: 3000px;
   background:#000;
   opacity: 0.3;
   position: absolute;
   top:0;
   left: 0;
   z-index: 9998;
   display:none;
  }
 </style>
</head>
<body>
 <div class="con">
  <span id="open">打開(kāi)彈出層</span>
 </div>
 <div class="up" id="up">
   <h2>彈出層效果</h2>
   <span id="close">關(guān)閉</span>
 </div>
 <img src="a.jpg">
</body>
<script type="text/javascript">
//兩種方式實(shí)現(xiàn)div居中:1:用css方式:top:50%,left:50%; margin-let:-divwidth/2 margin-top:divheight/2; 2:用js實(shí)現(xiàn):獲取窗口的高寬,top=(屏幕高-div高)/2,為了隨時(shí)的監(jiān)聽(tīng)瀏覽器的改變,需要用到瀏覽器事件
 window.onload=function(){
  function $(id){
   return document.getElementById(id);
  }
  //將div的位置封裝在一個(gè)函數(shù)內(nèi)部,直接調(diào)用
  function myDiv(){
   var mTop=(document.documentElement.clientHeight-500)/2;
   var mleft=(document.documentElement.clientWidth-400)/2;
   $("up").style.top=mTop+"px";
   $("up").style.left=mleft+"px";
  }
   myDiv();
   $("open").onclick=function(){
    $("up").style.display="block";
    mask.style.display="block";
   }
   $("close").onclick=function(){
    $("up").style.display="none";
    mask.style.display="none"
   }
   //創(chuàng)建一個(gè)DIV
   var mask=document.createElement("div");
   // //給這個(gè)DIV一個(gè)id和class屬性
   // mask.id="mask";
   mask.className="mask";
   mask.style.width=document.documentElement.clientWidth;
   mask.style.height=document.documentElement.clientHeight;
   //將這個(gè)DIV放置到body里面--》一般是:父節(jié)點(diǎn).appendChild(子節(jié)點(diǎn))
   //這里注意的是absolute,要設(shè)置top和left;
   document.body.appendChild(mask); 
  //屏幕改變大小的時(shí)候,div不會(huì)自動(dòng)的改變,需要添加窗口改變事件
  window.onresize=function(){
    myDiv();
    mask.style.width=document.documentElement.clientWidth;
    mask.style.height=document.documentElement.clientHeight;
  }
 }
</script>
</html>

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript切換特效與技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論