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

May 16, 2016 pm 07:00 PM
javascript drag

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles