当前位置: 首页 > news >正文

免费个人搭建网站东莞seo建站咨询

免费个人搭建网站,东莞seo建站咨询,贵阳做网站kuhugz,做网站备案须知偏移量(offset dimension)是javascript中一个重要的概念。javascript中涉及到偏移的有offsetWidth、offsetHeight、offsetLeft、offsetTop这四个值,offsetLeft 和 offsetTop 都是以 offsetParent 的内边距为参照的。 console.dir(元素)&…

偏移量(offset dimension)是javascript中一个重要的概念。javascript中涉及到偏移的有offsetWidth、offsetHeight、offsetLeft、offsetTop这四个值,offsetLeft 和 offsetTop 都是以 offsetParent 的内边距为参照的。

console.dir(元素),可以看到下面会有offsetParent这个属性。
在这里插入图片描述

offsetParent 定义

HTMLElement.offsetParent 是一个只读属性,返回一个距离该元素最近的、有定位属性(position不等于static)的父级元素。如果父元素中不存在定位,则offsetParent返回body。

  • 元素自身有fixed定位,offsetParent返回null;
  • 元素自身无fixed定位,且父元素也不存在定位,offsetParent返回body;
  • 元素自身无fixed定位,且父元素存在定位,offsetParent返回离自身最近的、有定位的父元素;
  • body和document 的 offsetParent,都返回null;

offsetParent详细说明

1、元素自身有fixed定位,offsetParent返回null(firefox中为body,其他浏览器返回null)。
当元素自身有fixed固定定位时,我们知道固定定位的元素是相对于视口进行定位的,此时没有定位父级,所以offsetParent的结果为null。

<div id="div0" style="position:fixed"></div>    
<script>//firefox并没有考虑固定定位的问题,返回<body>,其他浏览器都返回nullconsole.log(document.getElementById("div0").offsetParent);
</script>

2、元素自身无fixed定位,且父元素也不存在定位,offsetParent返回body。

<div><div id="div0"></div> 
</div>  
<script>console.log(document.getElementById("div0").offsetParent);//body
</script>

3、元素自身无fixed定位,且父元素存在定位,offsetParent返回离自身最近的、有定位的父元素。

<div id="div2" style="position:relative"><div id="div1" style="position:absolute"><div id="div0"></div> </div>  
</div>  
<script>console.log(document.getElementById("div0").offsetParent);//div1
</script>

4、body和document 的 offsetParent,都返回null。

console.log(document.body.offsetParent);//null
console.log(document.documentElement.offsetParent);//null

jquery中可以通过offsetParent()方法获取到元素的定位父级。

//设置最近的定位父级元素的背景颜色:
$("button").click(function(){$("p").offsetParent().css("background-color","red");
});

关于offsetWidth、offsetHeight、offsetLeft、offsetTop的详细内容可以点击查看
https://blog.csdn.net/Charissa2017/article/details/103837572

关于offsetParent的兼容补充

IE7-浏览器对于offsetParent,有以下bug。

1、当元素本身有绝对定位或者相对定位,父元素都没有定位时,IE7-浏览器中,offsetParent返回html。

<div id="div0" style="position:relative"></div>    
<script>//IE7-浏览器返回html,其他浏览器返回bodyconsole.log(document.getElementById("div0").offsetParent);
</script>
<div id="div0" style="position:absolute"></div>    
<script>//IE7-浏览器返回html,其他浏览器返回bodyconsole.log(document.getElementById("div0").offsetParent);
</script>

2、如果父元素有定位或者存在触发haslayout的元素,offsetParent返回离元素自身最近的有定位、或者触发haslayou的元素。
haslayout是IE7-浏览器特有的一种只读属性,有两个值,true或者false,当为true时,表示该元素有自己的布局,false表示该元素的布局继承于父元素。
点击查看haslayout的详细内容。

<div id="div1" style="display:inline-block;"><div id="div0"></div>    
</div>
<script>
//IE7-浏览器返回<div id="div1">,其他浏览器返回<body>
console.log(document.getElementById("div0").offsetParent);
</script>
<div id="div2" style="position:absolute;"><div id="div1" style="display:inline-block;"><div id='div0'></div>    </div>    
</div>
<script>
//IE7-浏览器返回<div id="div1">,其他浏览器返回<div id="div2">
console.log(document.getElementById("div0").offsetParent);
</script>
<div id="div2" style="display:inline-block;"><div id="div1" style="position:absolute;"><div id='div0'></div>    </div>    
</div>
<script>
//所有浏览器都返回<div id="div1">
console.log(document.getElementById("div0").offsetParent);
</script>
http://www.yidumall.com/news/8397.html

相关文章:

  • 做短视频的能跟几个网站签约苏州网站建设
  • 有人做家具网站中介吗天津谷歌优化
  • 万网速成网站seo外链代发
  • 如何做网站的营销有哪些可以推广的平台
  • 制作网站网站推荐6个免费国外自媒体平台
  • 领域网站建设微信公众号运营推广方案
  • 泰州网站制作方案定制线下推广方法有哪些
  • 制作网站时怎样做滚动字幕广东seo价格是多少钱
  • 荆州哪里做网站个人怎么做免费百度推广
  • 网页版微信登录入口手机百度seo关键词排名优化
  • 网站商城设计方案搜索引擎优化实训
  • 网络游戏那个网站做的最好免费推广的网站有哪些
  • 网站建设方案机构seo关键词优化的技巧
  • 唐山的做网站的企业排名前50名免费的网站
  • 在做好政府网站建设方面上海专业seo
  • 购物型网站用dw做怎么在百度做广告
  • 山西两学一做网站重庆百度seo整站优化
  • 北京公司做网站优化好搜移动端关键词快速排名
  • 如何注销网站图片搜索图片识别
  • 宠物网站模板seo流量增长策略
  • 手机网站怎么设计晋中网站seo
  • 东莞网站建设主要学什么百度公司全称叫什么
  • 微信平台商城开发seo发包技术教程
  • 贵阳公众号开发公司seo优化就业前景
  • 装饰logo设计图片怎么做优化
  • 网站开发备案认证杭州网络推广网络优化
  • 做水果网站需要些什么企业培训心得
  • 有哪些高端的网站谷歌浏览器下载安装(手机安卓版)
  • dedecms 招聘网站重庆森林粤语完整版在线观看免费
  • 这么做国外网站的国内镜像站谷歌浏览器官网手机版