代码如下:
切记改一下jq的引入路径
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="js/jquery-1.8.3.min.js"></script>
</head>
<style type="text/css">
*{margin:0; padding:0;}
.ws-mask{width:100%; height:100%; background:rgba(0,0,0,0.2); position: fixed; top: 0; z-index: 9999;}
.ws-popup{width:400px; height:auto; border:5px solid #b9b9b9; background:#fff; z-index:10000; padding-bottom: 15px; position: absolute; animation: fadeInDown .8s 0s ease both;}
.popup-top{width:100%; height:50px; font:18px/50px 'microsoft yahei'; color:#4b4b4b; text-align: center; background:#f2f2f2; position: relative;}
.popup-list{width:370px; height:45px; margin:20px 15px 0 15px; text-align: center;}
.popup-list span{font:14px/45px 'microsoft yahei'; color:#4b4b4b; padding:0 10px;}
.popup-list-inp{width:280px; height:43px; border:1px solid #ccc; padding:0 5px;}
.popup-list-btn{width:160px; height:40px; border:0; background:#f21c26; font: 14px/40px 'microsoft yahei'; color:#fff; text-align: center; margin:0 auto;}
.popup-list-btn:hover{background:#e4271b; transition-delay:0.2s; transition:all 0.4s; -moz-transition:all 0.4s; -webkit-transition:all 0.4s; -o-transition:all 0.4s; cursor:pointer;}
.popup-close{width:30px; height:30px; font:26px/30px 'microsoft yahei'; color:#4b4b4b; background:#f2f2f2; position: absolute; top: 10px; right:10px; cursor:pointer;}
.popup-close:hover{background:#00b19d; transition-delay:0.2s; transition:all 0.4s; -moz-transition:all 0.4s; -webkit-transition:all 0.4s; -o-transition:all 0.4s; color:#fff;}
</style>
<body style="height:1500px;">
<input type="button" style="margin-top:500px;" onclick="wspopup();" name="" value="点我弹出">
<!-- 遮罩层 -->
<div class="ws-mask" style="display:none;"></div>
<!-- 弹出层 -->
<div class="ws-popup" style="display:none;">
<form action="javascript:;" method="post" accept-charset="utf-8">
<div class="popup-top">
<span>添加项目</span><span class="popup-close" onclick="wsclose();">×</span>
</div>
<div class="popup-list">
<span>项目名称</span><input class="popup-list-inp" type="text" name="" value="" placeholder="">
</div>
<div class="popup-list">
<span>项目价格</span><input class="popup-list-inp" type="text" name="" value="" placeholder="">
</div>
<div class="popup-list">
<span>项目单位</span><input class="popup-list-inp" type="text" name="" value="" placeholder="如:天、次...">
</div>
<div class="popup-list">
<input class="popup-list-btn" type="submit" name="" value="提交">
</div>
</form>
</div>
<script type="text/javascript">
//显示弹出层
function wspopup(){
document.documentElement.style.overflow='hidden'; //禁止浏览器滚动
var bodyHeight = document.documentElement.clientHeight + document.body.scrollTop; //获取浏览器可视区域高度 实际高度+下拉的高度
var bodyWidth = document.documentElement.clientWidth; //获取浏览器可视区域宽度
var popupHeight = $('.ws-popup').height(); //获取弹出层高度
var popupWidth = $('.ws-popup').width(); //获取弹出层宽度
var tolHeight = (bodyHeight + document.documentElement.scrollTop - popupHeight) / 2; //计算弹出层上下偏移量
var tolWidth = (bodyWidth - popupWidth) / 2; //计算弹出层左右偏移量
$('.ws-mask').height(bodyHeight); //设置遮罩层高度
$('.ws-popup').css({'top':tolHeight,'left':tolWidth}) //设置弹出层偏移位置(必须先在CSS设置position值)
$('.ws-mask').show();
$('.ws-popup').show();
}
//关闭弹出层
function wsclose(){
document.documentElement.style.overflow='auto'; //恢复浏览器滚动
$('.ws-mask').hide();
$('.ws-popup').hide();
}
</script>
</body>
</html>