首页 > 其他分享 >HTMLElement: offsetParent property

HTMLElement: offsetParent property

时间:2023-11-27 18:14:24浏览次数:24  
标签:offsetParent parent positioned element HTMLElement position property

HTMLElement: offsetParent property

The HTMLElement.offsetParent read-only property returns a reference to the element which is the closest (nearest in the containment hierarchy) positioned ancestor element.

A positioned ancestor is either:

  • an element with a non-static position, or
  • td, th, table in case the element itself is static positioned.

If there is no positioned ancestor element, the body is returned.

Note: offsetParent returns null in the following situations:

  • The element or any ancestor has the display property set to none.
  • The element has the position property set to fixed (Firefox returns <body>).
  • The element is <body> or <html>.

offsetParent is useful because offsetTop and offsetLeft are relative to its padding edge. 

 

How can I find which element was used as the current element's top 50% position absolute

 

To find the parent element that a positioned child element is using as its reference, you can use JavaScript:

js

 

Copy code

let positionedElement = document.querySelector(".positioned"); let offsetParent = positionedElement.offsetParent;

The offsetParent property will return the closest positioned ancestor that the absolutely positioned element is using to offset itself.

A positioned element is one that has a position value other than the default static. This would include relative, absolute, fixed, and sticky.

So in this example:

html

 

Copy code

<div class="parent" style="position: relative"> <div class="positioned" style="position: absolute; top: 50%">...</div> </div>

The .positioned div is positioned absolutely at 50% from the top.

Its offsetParent will be the .parent div, since that has position: relative applied.

So using offsetParent allows you to dynamically find that positioned ancestor.

 

 

loop to find all positioned ancestors of element with id "userListTable_processing"

// Get reference to element
const elem = document.getElementById("userListTable_processing");

// Stores positioned ancestors  
const positionedAncestors = [];

// Loop upwards through parents 
let parent = elem.parentNode;
while (parent) {

  // Check if positioned
  if (parent.style.position === "relative" || 
      parent.style.position === "absolute" ||
      parent.style.position === "fixed") {
        
    // Add to array        
    positionedAncestors.push(parent);
  }

  // Go up to next parent 
  parent = parent.parentNode; 
}

// Print positioned ancestors
positionedAncestors.forEach(el => {
  console.log(el); 
});

 

 

 

 

 

 

标签:offsetParent,parent,positioned,element,HTMLElement,position,property
From: https://www.cnblogs.com/chucklu/p/17860000.html

相关文章

  • Failed to load property source from location ‘classpath:/bootstrap.yml‘
    日常报错之无中生有,这个错误实属低级,原因是因为bootstrap.yml文件格式错误。仔细检查下格式是否有重的地方错误配置实例因为两个spring应该都归属到spring下边server:port:9001#指定开发环境spring:这里写错了。应该放到下边application:name:sys#指向......
  • Object.defineProperty(obj,key,val)不可以监听数组变化,需要做特殊处理,所以Vue3.0使用
    关于Vue双向数据绑定说法错误的是()AVue实现双向数据绑定是采用数据劫持和发布者-订阅者模式BObject.defineProperty(obj,key,val)可以监听数组变化,不需要做特殊处理CVue2.0数据劫持是利用ES5的Object.defineProperty(obj,key,val)方法来劫持每个属性的getter和setterD......
  • 对象定义 Object.create Object.defineProperty
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-......
  • CodeForces 852C Property
    洛谷传送门CF传送门NOIP模拟赛T1,小清新几何题。要让选出的点组成的多边形面积最大,就要让正多边形的面积减去选出的点组成的多边形面积最小。而这个面积差可以表示成\(2n\)个三角形的面积,即\(\sum\limits_{i=0}^{2n-1}S_{\triangleA_iA_{(i+1)\bmodn}B_{(i+......
  • spring复习:(57)PropertyOverrideConfigurer用法及工作原理
    一、属性配置文件dataSource.url=jdbc:mysql://xxx.xxx.xxx.xxx/testdataSource.username=rootdataSource.password=xxxxxxdataSource.driverClassName=com.mysql.jdbc.Driver#dataSource.type=com.alibaba.druid.pool.DruidDataSource二、spring配置文件<?xmlversion="1.0&quo......
  • Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理
    PropertySourcePlaceholderConfigurer的用途:通过配置文件(比如.properties文件)给bean设置属性,替代属性占位符示例:属性配置文件spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/testspring.datasource.username=rootspring.datasource.password=xxxxxxspring.datasource.dri......
  • Uncaught TypeError: Cannot read property ‘addEventListener‘ of null 求助!!!!!!
    今天在项目中遇到个问题如下:vue项目中public的index.html文件script标签引入了一个外部的js文件,里面有一个方法每次调用的时候都会报错UncaughtTypeError:Cannotreadproperty‘addEventListener‘ofnull,网上查的所有办法都试过了:跟标签摆放先后位置,放到onload方法中都没......
  • INotifyPropertyChanged
      可以将TextBox控件(其他控件也基本一样)与某个变量进行绑定,做出改变变量则控件也跟着改变的效果。  首先需要声明一个类,该类用来与控件绑定:usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Runtime.CompilerServices;namespaceTestWPF{......
  • .NET 反序列化 GetterSettingsPropertyValue 攻击链
    0x01 链路1 SettingsPropertyValueSettingsPropertyValue位于命名空间 System.Configuration,用于应用程序存储和检索设置的值,此类具有Name、IsDirty、Deserialized、PropertyValue、SerializedValue等多个公共成员,其中SerializedValue属性用于获取或者设置序列化的值,便于持久......
  • JS_0077:JS 中对象操作 preventExtensions 禁止添加新属性 defineProperty 添加新属性
    1,//这是定义一个对象constnonExtensible={removalbe:true};//这是通过preventExtensions方法令指定对象无法再添加新的属性Object.preventExtensions(nonExtensib......