<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#div1{
width:500px;
height:500px;
border:1px solid red;
position:relative;
}
#div1 #close{
position:absolute;
right:0;
top:0;
}
</style>
<script src="jquery-2.2.1.min.js"></script>
<script>
$(function(){
$("#btn").click(function(){
//创建
var login = $('<div id="div1"><p>用户名: <input type="text" ></p><p>密码 :<input type="password"></p><div id="close">x</div></div>');
//添加
$("body").append(login);
login.css('left',($(window).width() - login.outerWidth())/2);
login.css('top',($(window).height() - login.outerHeight())/2);
$("#close").click(function(){
login.remove();
})
$(window).on("resize scroll",function(){
login.css('left',($(window).width() - login.outerWidth())/2 );
login.css('top',($(window).height() - login.outerHeight())/2+$(window).scrollTop());
})
})
})
</script>
</head>
<body style="height:2000px;">
<input type="button" id="btn" value="创建">
<!-- <div id="div1">
<p>用户名: <input type="text" ></p>
<p>密码 :<input type="password"></p>
<div id="close">x</div>
</div> -->
</body>
</html>