js实现弹出层

下面是一个JavaScript代码实现的弹出层效果

 

<html>  
  <head>  
    <style type="text/css">  
      #tranDiv {   
        position:absolute;   
        left:0px;   
        top:0px;   
      }   
      #tranDivBack {   
        position:absolute;   
        left:0px;   
        top:0px;   
        width:100%;   
        height:100%;   
        background-color:#000000;   
        filter:alpha(Opacity=30);   
        -moz-opacity:0.3;   
        opacity:0.3;   
      }   
      #infoDiv {   
        position:absolute;   
        left:0px;   
        top:0px;   
        width:100%;   
        height:100%;   
        text-align:center;   
      }   
      .pop_box {   
        background:#FFF;   
        padding:10px;   
        border:#666 8px solid;   
      }   
    </style>  
    <script language="javascript">    
      
      function showWindow(width,height){    
        location.href="#";     
        var windowstr= document.getElementById("popLayer").innerHTML;   
        document.getElementById("infoDiv").innerHTML=windowstr;    
        document.getElementById("infoDiv").style.left=((document.body.clientWidth-width)>0?(document.body.clientWidth-width):0)/2+"px";    
        document.getElementById("infoDiv").style.top=100+"px";    
        document.getElementById("infoDiv").style.zIndex=10001;   
        document.getElementById("infoDiv").style.width=width+"px";    
        document.getElementById("infoDiv").style.height=height+"px";    
        document.getElementById("tranDiv").style.height=document.body.clientHeight+ "px";    
        document.getElementById("tranDiv").style.width=document.body.clientWidth+ "px";    
        document.getElementById("tranDiv").style.display="";    
        document.getElementById("tranDivBack").style.display="";    
        document.getElementById("tranDivBack").style.zIndex=10000;   
        document.getElementById("infoDiv").style.display="";    
      }    
      function closeWindow(){   
        document.getElementById("tranDiv").style.display="none";    
      }
	   function changeVal() {
		var time = document.getElementById("time");
		time.value = "";
		//alert(document.getElementById("link").innerHTML);
	   }
	   function getVal() {
	    var time = document.getElementById("time").value;
		var patrn = /[0-9]/;
		if(!patrn.exec(time)) {
			alert("内容只能为整数,请重新输入!");
			return false;
		}
		if(time <= 0) {
			alert("设置时间应该大于0,请重新输入!");
			return false;
		}
		document.getElementById("link").innerHTML = time;
		//alert(document.getElementById("link").innerHTML);
		document.getElementById("timeVal").value = document.getElementById("link").innerHTML;
		alert(document.getElementById("timeVal").value);
		closeWindow();
	   }
    </script>  
  </head>  
  
  <div id="tranDiv" style="display:none;">  
    <div id="tranDivBack"></div>  
    <div id="infoDiv"></div>  
  </div>  
   
  <div id="popLayer" style="display:none;">  
    <div class="pop_box">  
		时间:<input type="text" value="小时(整数)" id="time" name="time" onClick="return changeVal();" /><br/>
		<input type="button" value="确定" onClick="return getVal();"/>
		<input type="button" value="关闭" onClick="return closeWindow();" />
    </div>  
  </div>  
  
	测试程序:<a id="link" href="javascript:showWindow(250,60);">时间</a> 
	<input type="hidden" id="timeVal" value="0" />
    
</html>  

 注:document.getElementById("infoDiv").style.width在Firefox中必须为下面样式:

 

document.getElementById("infoDiv").style.width="180px"

 

必须加双引号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值