栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

jQuery Position方法使用和兼容性

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery Position方法使用和兼容性

1、position方法

jquery api地址:http://jquery.cuishifeng.cn/position.html

position方法获取匹配元素相对父元素的偏移。

2、说明

2.1 与offset()区别

.offset()是获得该元素相对于documet的当前坐标

.position()方法可以取得元素相对于父元素的偏移位置,父元素为该元素最近的而且被定位过的祖先元素。

2.2 值计算

.元素本身所占用的边框,边距和填充的大小不计。

.父元素的边框和边距不计,父元素的填充计算在内。

3、示例代码



  
    
    jQuery position()示例
    
      html {
 line-height: 1.15;
      }
      
      .parent {
 position: relative;
 width: 200px;
 height: 400px;
 
 margin-top: 10px;
 
 border: 1px solid green;
 
 padding-top: 10px;
      }
      .child-1 {
 width: 100px;
 height: 100px;
 margin: 0 auto;
 border: 1px solid #2E8DED;
      }
      .child-2 {
 width: 100px;
 height: 100px;
 
 margin: 10px auto 0;
 
 border: 1px solid #2E8DED;
 
 padding: 10px;
      }
    
  
  
    
      

first child

second child

4、注意

对于文字的line-height等属性,浏览器(chrome、IE、Firefox)默认大小不一致,因此不同的浏览器position()在计算尺寸时会存在不一致,因此必须保证所有浏览器一致的line-height等属性。

示例代码为没有设置line-height的例子,position()在不同的浏览器上计算出的值不一样。



  
    
    jQuery position()示例
    
      html {
 
 
      }
      
      .parent {
 position: relative;
 width: 200px;
 height: 400px;
 
 margin-top: 10px;
 
 border: 1px solid green;
 
 padding-top: 10px;
      }
      .child-1 {
 width: 100px;
 height: 100px;
 margin: 0 auto;
 border: 1px solid #2E8DED;
      }
      .child-2 {
 width: 100px;
 height: 100px;
 
 margin: 10px auto 0;
 
 border: 1px solid #2E8DED;
 
 padding: 10px;
      }
    
  
  
    
      文字文字
      

first child

second child

总结

以上所述是小编给大家介绍的jQuery Position方法使用和兼容性,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/85562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号