offset()
methodUsing the jQuery offset()
method you can easily find the position of an element relative to the document. It is only applicable for the visible elements. That means, you can get the position of elements with visibility: hidden;
but not with display: none;
.
Let's take a look at the following example to understand how it actually works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Find Element's Position Relative to the Document</title>
<style>
#box{
width:150px;
height:100px;
background: orange;
margin: 150px 100px;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function(){
var offset = $("#box").offset();
alert("Current position of the box is: (left: " + offset.left + ", top: " + offset.top + ")");
});
});
</script>
</head>
<body>
<button type="button">Get Box Position</button>
<p><strong>Note:</strong> Play with the value of margin property to see how the jQuery offest() method works.</p>
<div id="box"></div>
</body>
</html>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Import Large Database in MySQL using Commandline (CMD)?
in this article, i will share with you h...What Is the Bashrc File Used For
If you are using Linux operating system...How to get Last Record From leftJoin Table in Laravel?
get the latest record from leftJoin tabl...Laravel Get Headers From Request Example
In Laravel application, there are many w...jQuery Test if checkbox is NOT checked
JQuery is simple but very helpful and us...