发布于 2017-07-07 22:04:53 | 110 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Javascript教程,程序狗速度看过来!

JavaScript客户端脚本语言

Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。


这篇文章主要介绍了JS实现的添加弹出层并完成锁屏操作,涉及JS针对页面元素与样式动态操作相关技巧,需要的朋友可以参考下

本文实例讲述了JS实现的添加弹出层并完成锁屏操作。分享给大家供大家参考,具体如下:

上图:

代码:


<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">打开弹出层</span>
 </div>
 <div class="up" id="up">
   <h2>弹出层效果</h2>
   <span id="close">关闭</span>
 </div>
 <img src="a.jpg">
</body>
<script type="text/javascript">
//两种方式实现div居中:1:用css方式:top:50%,left:50%; margin-let:-divwidth/2 margin-top:divheight/2; 2:用js实现:获取窗口的高宽,top=(屏幕高-div高)/2,为了随时的监听浏览器的改变,需要用到浏览器事件
 window.onload=function(){
  function $(id){
   return document.getElementById(id);
  }
  //将div的位置封装在一个函数内部,直接调用
  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"
   }
   //创建一个DIV
   var mask=document.createElement("div");
   // //给这个DIV一个id和class属性
   // mask.id="mask";
   mask.className="mask";
   mask.style.width=document.documentElement.clientWidth;
   mask.style.height=document.documentElement.clientHeight;
   //将这个DIV放置到body里面--》一般是:父节点.appendChild(子节点)
   //这里注意的是absolute,要设置top和left;
   document.body.appendChild(mask); 
  //屏幕改变大小的时候,div不会自动的改变,需要添加窗口改变事件
  window.onresize=function(){
    myDiv();
    mask.style.width=document.documentElement.clientWidth;
    mask.style.height=document.documentElement.clientHeight;
  }
 }
</script>
</html>

希望本文所述对大家JavaScript程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务