无涯教程-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>

这将产生以下输出-

jQuery 中的 position( )方法函数 - 无涯教程网无涯教程网提供position()方法获取元素相对于其偏移父级的顶部和左侧位置。返回的对象包含两个Intege...https://www.learnfk.com/jquery/css-position.html

猜你喜欢

转载自blog.csdn.net/w116858389/article/details/132026142