question By clicking a control, a floating layer is displayed below the control. The usual practice is to obtain the offset value of the control, calculate the values of the top, left and other CSS properties of the floating layer, and assign them. Then let's take a look at how to get the offset value of the control. Pure JS implementation The first thing that comes to mind is a piece of js like this. Copy code The code is as follows:document.getElementById("divFloat").style.top=document.getElementById("Button").offsetLeft+25; If you find that you need to add value units, then modify it to the following: I tested it again with IETester and FireFox, and it works fine in IE6+. As before, the pure js method I wrote was ruthlessly despised by FireFox, and the obtained value was incorrect. I checked online and found that it should be written like this: through a loop, calculating layer by layer, finally getting the accurate offset value. Copy code The code is as follows:function getOffsetLeft(o) { var left=0; var offsetParent = o; while (offsetParent != null && offsetParent != document.body) { left += offsetParent.offsetLeft; offsetParent = offsetParent.offsetParent; }</p> <p>return left; } jQuery Implementation When I looked into the related issues in more detail, I found that jQuery already contains a function to implement this function: offset(), which is very compatible with various browsers. Copy code The code is as follows:$("#Button").offset().left After downloading the source code, I found that jQuery is implemented like this Copy code The code is as follows:jQuery.fn.extend({ position: function() { if ( !this[0] ) { return null; } var elem = this[0], // Get *real* offsetParent offsetParent = this.offsetParent(), // Get correct offsets offset = this.offset(), parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset(); // Subtract element margins // note: when an element has margin: auto the offsetLeft and marginLeft // are the same in Safari causing offset.left to incorrectly be 0 offset.top -= parseFloat( jQuery.curCSS(elem, "marginTop", true) ) || 0; offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0; // Add offsetParent borders parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth", true) ) || 0; parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0; // Subtract the two offsets return { top: offset.top - parentOffset.top, left: offset.left - parentOffset.left }; }, offsetParent: function() { return this.map(function() { var offsetParent = this.offsetParent || document.body; while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) { offsetParent = offsetParent.offsetParent; } return offsetParent; }); } }); The calculation method is similar, but there is one thing to note: The offset() function does not include margin values in its calculations (but does include border values). |
<<: Detailed explanation of HTML programming tags and document structure
>>: Docker swarm simple tutorial
1. Scenario display The tomcat log occasionally r...
Introduction to jsvc In production, Tomcat should...
mysqldump tool backup Back up the entire database...
Excel is the most commonly used tool for data ana...
The CentOS Project, a 100% compatible rebuild of ...
Table of contents 1 Introduction 2 Basic usage 2....
In this section, we will learn about list element...
Each web page has an address, identified by a URL...
CSS3 can change the color of pictures. From now o...
First of all, we need to make it clear why we use...
There is a difference between src and href, and t...
Carousel animation can improve the appearance and...
HTML+CSS+JS imitates win10 brightness adjustment ...
Stop MySQL Service Windows can right-click My Com...
Table of contents 01 Background 02 Introduction 0...