offset() 方法 文档偏移量

以前看视频学习听到这个offset()感觉很陌生,没有用过,马上记到笔记里了,今天翻起笔记再次看到,都已经忘记是怎么用的了,所以来到这里狠狠的记下来:

offset() 方法返回得或设置元素相对于文档的偏移(位置)。一般情况下有两个值:top和left值,经测试,不管所选的元素在哪偏移都是从整个文档开始算的

粘贴下面的代码,看效果

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>测试偏移是从哪开始的</title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.min.js"></script>
    <style type="text/css">
        .box1{height: 300px;}
        .box2{padding: 10px;border: 1px solid #ddd;}
    </style>
</head>
<body>
<div class="box1"></div>
<div class="box2">
    <p>代码征服世界 本段文字偏移left<span></span>,top<strong></strong></p>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        var x=$("p").offset();
        $("span").text(x.left);
        $("strong").text(x.top);
    });
</script>

</body>
</html>



 欢迎加入web前端交流开发群:qq 143206839

posted @ 2016-07-14 15:32  Graceing  阅读(2429)  评论(0编辑  收藏  举报