Home > Web Front-end > JS Tutorial > Javascript drag and drop series article 1 - offsetParent attribute_javascript skills Untitled DocumentUntitled DocumentUntitled DocumentUntitled Document

Javascript drag and drop series article 1 - offsetParent attribute_javascript skills Untitled DocumentUntitled DocumentUntitled DocumentUntitled Document

PHP中文网
Release: 2016-05-16 19:00:52
Original
1065 people have browsed it

This series of articles mainly talks about the basic knowledge of implementing Javascript drag and drop function, and will give a complete example at the end. Suitable for people who don’t know anything about drag and drop.

Let’s first talk about the offsetParent attribute in Javascript.
Supported browsers: Internet Explorer 4.0, Mozilla 1.0, Netscape 6.0, Opera 7.0, Safari 1.0
element.offsetParent
Summary
offsetParent returns a reference to the object which is the closest (nearest in the containment hierarchy) positioned containing element. If the element is non-positioned, the root element (html in standards compliant mode; body in quirks rendering mode) is the offsetParent. offsetParent returns null when the element has style.display set to "none ".
Syntax
parentObj = element.offsetParent
Parameters
· parentObj is an object reference to the element in which the current element is offset.
Specification
DOM Level 0. Not part of specification.
Excerpt from Mozilla Developer Center website
Translated as follows:
element.offsetParent
Summary
The offsetParent property returns a reference to an object that is closest to the element calling offsetParent (the closest in the containing hierarchy) and is the container element that has been CSS positioned. If this container element is not CSS positioned, the value of the offsetParent attribute is a reference to the root element (html element in standards compatibility mode; body element in weird rendering mode). When the container element's style.display is set to "none" (Except for IE and Opera), the offsetParent property returns null .
Syntax
parentObj = element.offsetParent
Variables
· parentObj is a reference to an element in which the offset of the current element is calculated.
Specification
DOM Level 0. Not part of the specification.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript" language="JavaScript"> 
function offset_init(){ 
var pElement = document.getElementById("sonObj"); 
parentObj = pElement.offsetParent; 
alert(parentObj.tagName); 
} 
</script> 
</head> 
<body onload="offset_init()"> 
<div id="parent"> 
<p id="sonObj">测试OffsetParent属性</p> 
</div> 
</body> 
</html>
Copy after login

Test results:
Firefox3: "BODY"
Internet Explorer 7: "BODY"
Opera 9.51: "BODY"
Chrome 0.2: "BODY"
Safari 3: "BODY"
Conclusion:
When neither an element nor its parent element is CSS positioned, then The value of the offsetParent attribute of this element is the root element. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is the Body element. (In fact, regardless of the standard compatibility mode or the weird mode, the root element is the Body element)
Test code 2:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
#parent{ 
position:absolute; 
<!--position:relative;--> 
left:25px; 
top:188px; 
border:1px solid black; 
} 
</style> 
<script type="text/javascript" language="JavaScript"> 
function offset_init(){ 
var pElement = document.getElementById("sonObj"); 
parentObj = pElement.offsetParent; 
alert(parentObj.tagName); 
} 
</script> 
</head> 
<body onload="offset_init()"> 
<div id="parent">div测试代码<p id="sonObj">测试OffsetParent属性</p></div> 
</body> 
</html>
Copy after login

Test result:
Firefox3: "p"
Internet Explorer 7: "p"
Opera 9.51: "p"
Chrome 0.2: "p"
Safari 3: "p"
Conclusion:
When the parent element of an element has CSS When positioned (absolute or relative), the value of the offsetParent attribute of this element is its parent element. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is its parent element.
Test code 3:


[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute]

Test results:
Firefox3: "H1"
Internet Explorer 7: "H1"
Opera 9.51: "H1"
Chrome 0.2: "H1"
Safari 3: "H1"
Conclusion:
When someone When neither element nor its parent element is CSS positioned (absolute or relative), the value of the offsetParent attribute of this element is the element closest to it in the DOM structure hierarchy and which has been CSS positioned.

Test code 4:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
#Grandfather{ 
position:relative; 
left:25px; 
top:188px; 
border:1px solid black; 
} 
</style> 
<script type="text/javascript" language="JavaScript"> 
function offset_init(){ 
var pElement = document.getElementById("sonObj"); 
parentObj = pElement.offsetParent; 
alert(parentObj.tagName); 
} 
</script> 
</head> 
<body onload="offset_init()"> 
<h1 id="Grandfather"> 
<div id="parent"> 
<p id="sonObj">测试OffsetParent属性</p> 
</div> 
</h1> 
</body> 
</html>
Copy after login

Test result:
Firefox3: "BODY"
Internet Explorer 7: "BODY"
Opera 9.51: " BODY"
Chrome 0.2: "BODY"
Safari 3: "BODY"
Conclusion:
When only a certain element is CSS positioned, neither its parent element nor its DOM structure level is positioned. When CSS is positioned, the value of the offsetParent attribute of this element is HTMLBODYElement. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is the Body element.
Okay, that’s it for the offsetParent attribute.
As for what to write next, I haven’t decided yet.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template