close
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
#box{
padding: 50px;
background-color: #088;
display: inline-block;
margin: 50px;
position: absolute;
}
#child{
width: 100px;
height: 100px;
background-color: #08f;
}
#ground{
height: 300px;
width: 100px;
}
</style>
</head>
<body>
<div id="box">
<div id="child"></div>
</div>
<div id="ground"></div>
<script type="text/javascript">
$('#ground').after(
'<h2>小孩離螢幕左上角左距'+$('#child').offset().left+'</h2>'+
'<h2>小孩離螢幕左上角上距'+$('#child').offset().top+'</h2>'+
'<h2>小孩離父親左上角上距'+$('#child').position().top+'</h2>'+
'<h2>小孩離父親左上角上距'+$('#child').position().left+'</h2>');
/*因為是絕對定位,所以offest跟position的值不同*/
</script>
</body>
</html>
全站熱搜
留言列表