


Javascript gets document coordinates and viewport coordinates_javascript skills
The position of an element is measured in pixels, with the X coordinate increasing to the right and the Y coordinate increasing going down, however, there are two different points as the origin of the coordinate system: the X and Y coordinates of an element can be relative At the upper left corner of the document or relative to the upper left corner of the viewport in which the document is displayed.
In rated windows and tabs, the "viewport" is only the part of the browser that actually displays the document's content: it does not include the browser's "shell" (such as menus, toolbars, and tabs).
For the document displayed in the frame, the
If the document is smaller than the viewport, or scrolling has not yet occurred, the upper left corner of the document is the upper left corner of the viewport, and the document and viewport coordinate systems are the same. But generally speaking, to convert between two coordinate systems, the scroll offset must be added or subtracted. For example, if an element's Y coordinate is 200 pixels in document coordinates, and the user has scrolled the browser down 75 pixels, then the element's Y coordinate in viewport coordinates is 125 pixels. Likewise, if an element's X coordinate is 400 pixels in viewport coordinates, and the user has scrolled the viewport 200 pixels horizontally, then the X coordinate of the element in document coordinates is 600 pixels.
Document coordinates are more basic than viewport coordinates, and they do not change as the user scrolls. However, it is very common to use viewport coordinates in client-side programming. Document coordinates are used when specifying the position of elements using CSS. But the simplest way to query the position of an element: getBoundingClientRect() returns the position in viewport coordinates. Similarly, when you register an event handler function for a mouse event, the coordinates of the mouse pointer reported are in the viewport coordinate system.
In order to transform in the coordinate system, we need to determine the position of the scroll bar of the browser window. The pageXoffset and pageYOffset properties of the Window object provide these values in all browsers except IE8 and earlier. IE (and all modern browsers) can also obtain the scrollbar position through the scrollLeft and scrollTop properties. What's confusing is that normally you get these properties by looking up the document's root node (document.documentElement), but in weird mode you have to query them on the document's
element (documeng.body). The following shows how to easily query the position of the scroll bar.functon getScrollOffsets(w){ w = w || window; var sLeft,sTop; if(w.pageXOffset != null) { sLeft = w.pageXOffset; sTop = w.pageYOffset; return {x:sLeft,y:sTop}; } if(document.compatMode == "CSS1Compat"){ sLeft = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft; sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop; return {x:sLeft,y:sTop}; }else if(document.compatMode == "BackCompat"){ sLeft = document.body.scrollLeft; sTop = document.body.scrollTop; return {x:sLeft,y:sTop}; } }
It is sometimes useful to be able to determine the size of the viewport, for example, to determine which portion of the document is currently visible. The simple method of querying the viewport's dimensions using the scroll offset does not work in IE8 and earlier, and the technique's ability to work in IE depends on whether the browser is in weird mode or standards mode.
Attributes under window:
innerHeight: The height of the window content part including the scroll bar
innerWidth: The width of the window content part including the scroll bar
outerHeight: The height of the entire browser, including all components of the interface.
outerWidth: The width of the entire browser, including all components of the interface.
pageXOffset: The position of the X-axis of the scroll bar of the browser window
pageYOffset: The position of the Y-axis of the scroll bar of the browser window
scrollX: The position of the X-axis of the scroll bar of the browser window
scrollY: The position of the Y-axis of the scroll bar of the browser window
Properties
document.documentElement document.body
clientHeight The size of the visible content in the viewport, excluding scrolling parts and scroll bars.
clientWidth
clientLeft
clientTop
offsetHeight content size, including scroll bars.
offsetWidth
offsetLeft
offsetTop
scrollHeight The size of the scrolling content, including the scrolling part, but not including the scroll bar.
scrollWidth
scrollTop
scrollWidth
Viewport size of query window:
function getViewportSize(w){ w = w || window; var cWidth,cHeight; if(w.innerWidth != null){ cWidth = w.innerWidht; cHeight = w.innerHeight; return {w:cWidth,h:w.cHeight}; } if(document.compatMode == "CSS1Compat"){ cWidth = document.documentElement.clientWidth; cHeight = doument.documentElement.clientHeight; return {w:cWidth,h:w.cHeight}; }else if(document.compatMode == "BackCompat"){ cWidth = document.body.clientWidth; cHeight = doument.body.clientHeight; return {w:cWidth,h:w.cHeight}; } }
The above is the entire content of this article, I hope you all like it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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 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.

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

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

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

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

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).
