jQuery 中的 position( )方法函数

首页 / jQuery入门教程 / jQuery 中的 position( )方法函数

position()方法获取元素相对于其偏移父级的顶部和左侧位置。

返回的对象包含两个Integer整数属性,即top和left。为了进行准确的计算,请确保将像素值用于边距,边框和填充。此方法仅适用于可见元素。

position( ) - 语法

selector.position( )

position( ) - 示例

以下是一个简单的示例,简单说明了此方法的用法-

无涯教程网

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" 
         src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type="text/javascript" language="javascript">
         $(document).ready(function() {
			
            $("div").click(function () {
               var position=$(this).position();
               $("#lresult").html("left position: <span>" + position.left + "</span>.");
               $("#tresult").html("top position: <span>" + position.top + "</span>.");
            });
				
         });
      </script>
		
      <style>
         div { width:60px; height:60px; margin:5px; float:left; }
      </style>
   </head>
	
   <body>
      <p>Click on any square:</p>
      <span id="lresult"> </span>
      <span id="tresult"> </span>
		
      <div  style="background-color:blue;"></div>
      <div  style="background-color:pink;"></div>
      <div  style="background-color:#123456;"></div>
      <div  style="background-color:#f11;"></div>
   </body>
</html>

这将产生以下输出-

链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/css-position.html

来源:LearnFk无涯教程网

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出区块链 -〔陈浩〕

软件测试52讲 -〔茹炳晟〕

OpenResty从入门到实战 -〔温铭〕

黄勇的OKR实战笔记 -〔黄勇〕

设计模式之美 -〔王争〕

如何看懂一幅画 -〔罗桂霞〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

如何成为学习高手 -〔高冷冷〕

说透区块链 -〔自游〕

好记忆不如烂笔头。留下您的足迹吧 :)