国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery中offset()方法用法實例

2024-05-06 16:14:36
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了jQuery中offset()方法用法,實例分析了offset()方法的功能、定義及返回或設置所匹配元素相對于document對象的偏移量時的使用技巧,需要的朋友可以參考下
 

本文實例講述了jQuery中offset()方法用法。分享給大家供大家參考。具體分析如下:

此方法返回或設置所匹配元素相對于document對象的偏移量。

語法結構一:

復制代碼代碼如下:
$(selector).offset()

 

獲取匹配元素在當前document的相對偏移。
返回的對象包含兩個整型屬:top和left。
此方法只對可見元素有效。

實例代碼:

 

復制代碼代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>offset()函數-武林網</title>
<style type="text/css">
*{
  margin:0px;
  padding:0px;
}
.father{
  border:1px solid black;
  width:400px;
  height:300px;
  padding:10px;
  margin:50px;
}
.children{
  height:150px;
  width:200px;
  margin-left:50px;
  background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
     a=$(".children").offset();
     alert("元素的偏移量坐標是:"+a.top+"|"+a.left+"");
   })
})
</script>
</head>
<body>
<div class="father">
  <div class="children"></div>
</div>
<button>獲取元素的坐標</button>
</body>
</html>

 

以上代碼可以彈出子div相對于document的偏移量。

語法結構二:

 

復制代碼代碼如下:
$(selector).offset(value)

 

設置匹配元素相對于document對象的坐標。
offset()方法可以讓我們重新設置元素的位置。這個元素的位置是相對于document對象的。
如果對象原先的position樣式屬性是static的話,會被改成relative來實現重定位。

參數列表:

參數 描述
value 規定以像素計的 top 和 left 坐標。
可能的值:
1.值對,比如 {top:200,left:10}。
2.帶有top和left 屬性的對象。

 

實例代碼:

 

復制代碼代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>offset()函數-武林網</title>
<style type="text/css">
.father{
  border:1px solid black;
  width:400px;
  height:300px;
}
.children{
  height:150px;
  width:200px;
  background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
     $(".children").offset({top:100,left:100})
  })
})
</script>
</head>
<body>
<div class="father">
  <div class="children"></div>
</div>
<button>點擊設置偏移量</button>
</body>
</html>

 

以上代碼可以設置div相對于document的偏移量。

語法結構三:

使用函數的返回值來設置偏移坐標:

復制代碼代碼如下:
$(selector).offset(function(index,oldoffset))

 

參數列表:

參數 描述
function(index,oldvalue) 規定返回被選元素新偏移坐標的函數:
index - 可選。元素的索引。
oldvalue - 可選。當前坐標。

 

實例代碼:

 

復制代碼代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.survivalescaperooms.com/" />
<title>offset()函數-武林網</title>
<style type="text/css">
.father{
  border:1px solid black;
  width:400px;
  height:300px;
}
.children{
  height:150px;
  width:200px;
  background-color:green;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $(".children").offset(function(a,b){
      var newpoint= new Object();
      newpoint.top=b.top+50;
      newpoint.left=b.left+50;
      return newpoint;
    })
  })
})
</script>
</head>
<body>
<div class="father">
  <div class="children"></div>
</div>
<button>點擊設置偏移量</button>
</body>
</html>

 

以上代碼同樣可以設置元素的偏移,不過值是通過函數返回。

希望本文所述對大家的jQuery程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 望谟县| 普宁市| 稷山县| 宜春市| 米脂县| 丹江口市| 莲花县| 大庆市| 泸溪县| 岳普湖县| 青州市| 米林县| 通辽市| 共和县| 沙雅县| 阜城县| 延庆县| 龙州县| 上高县| 库车县| 龙南县| 汽车| 云安县| 晋州市| 九台市| 茂名市| 宁强县| 维西| 壶关县| 尚志市| 泽州县| 伊宁县| 浦东新区| 连江县| 阳泉市| 休宁县| 南丹县| 兰考县| 布尔津县| 遂宁市| 顺义区|