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

首頁 > 語言 > JavaScript > 正文

淺談jQuery的offset()方法及示例分享

2024-05-06 16:23:19
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了淺談jQuery的offset()方法及示例分享的相關資料,需要的朋友可以參考下

offset()方法的定義和用法:

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

語法結構一

:

$(selector).offset()

獲取匹配元素在當前document的相對偏移。

返回的對象包含兩個整型屬:top和left。

此方法只對可見元素有效。

實例代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta charset=" utf-8"
  5.  
  6. <style type="text/css"
  7. *{ 
  8. margin:0px; 
  9. padding:0px; 
  10. .father{ 
  11. border:1px solid black; 
  12. width:400px; 
  13. height:300px; 
  14. padding:10px; 
  15. margin:50px; 
  16. .children{ 
  17. height:150px; 
  18. width:200px; 
  19. margin-left:50px; 
  20. background-color:green; 
  21. </style> 
  22. <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
  23. <script type="text/javascript"
  24. $(document).ready(function(){ 
  25. $("button").click(function(){ 
  26. a=$(".children").offset(); 
  27. alert("元素的偏移量坐標是:"+a.top+"|"+a.left+""); 
  28. }) 
  29. }) 
  30. </script> 
  31. </head> 
  32. <body> 
  33. <div class="father"
  34. <div class="children"></div> 
  35. </div> 
  36. <button>獲取元素的坐標</button> 
  37. </body> 
  38. </html> 

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

語法結構二

:

$(selector).offset(value)

設置匹配元素相對于document對象的坐標。

offset()方法可以讓我們重新設置元素的位置。這個元素的位置是相對于document對象的。

如果對象原先的position樣式屬性是static的話,會被改成relative來實現重定位。

參數列表:

參數描述

value規定以像素計的 top 和 left 坐標。

可能的值:

1.值對,比如 {top:200,left:10}。

2.帶有top和left 屬性的對象。

實例代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta charset=" utf-8"
  5.  
  6. <style type="text/css"
  7. .father{ 
  8. border:1px solid black; 
  9. width:400px; 
  10. height:300px; 
  11. .children{ 
  12. height:150px; 
  13. width:200px; 
  14. background-color:green; 
  15. </style> 
  16. <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
  17. <script type="text/javascript"
  18. $(document).ready(function(){ 
  19. $("button").click(function(){ 
  20. $(".children").offset({top:100,left:100}) 
  21. }) 
  22. }) 
  23. </script> 
  24. </head> 
  25. <body> 
  26. <div class="father"
  27. <div class="children"></div> 
  28. </div> 
  29. <button>點擊設置偏移量</button> 
  30. </body> 
  31. </html> 

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

語法結構三

:

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

$(selector).offset(function(index,oldoffset))

參數列表:

參數描述

function(index,oldvalue)規定返回被選元素新偏移坐標的函數:

index - 可選。元素的索引。

oldvalue - 可選。當前坐標。

實例代碼:

 

 
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta charset=" utf-8"
  5. <style type="text/css"
  6. .father{ 
  7. border:1px solid black; 
  8. width:400px; 
  9. height:300px; 
  10. .children{ 
  11. height:150px; 
  12. width:200px; 
  13. background-color:green; 
  14. </style> 
  15. <script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
  16. <script type="text/javascript"
  17. $(document).ready(function(){ 
  18. $("button").click(function(){ 
  19. $(".children").offset(function(a,b){ 
  20. var newpoint= new Object(); 
  21. newpoint.top=b.top+50; 
  22. newpoint.left=b.left+50; 
  23. return newpoint; 
  24. }) 
  25. }) 
  26. }) 
  27. </script> 
  28. </head> 
  29. <body> 
  30. <div class="father"
  31. <div class="children"></div> 
  32. </div> 
  33. <button>點擊設置偏移量</button> 
  34. </body> 
  35. </html> 

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

以上所述就是本文的全部內容了,希望大家能夠喜歡。

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

圖片精選

主站蜘蛛池模板: 永春县| 张掖市| 浦北县| 汤阴县| 绍兴县| 和龙市| 金阳县| 报价| 浑源县| 鲁山县| 耒阳市| 济宁市| 阳东县| 邵武市| 镇宁| 清涧县| 株洲市| 卓尼县| 津市市| 陈巴尔虎旗| 诸城市| 华宁县| 连南| 海城市| 牙克石市| 涪陵区| 靖安县| 宁乡县| 天门市| 上蔡县| 贵阳市| 博爱县| 启东市| 玉龙| 胶南市| 柏乡县| 邹城市| 新津县| 水富县| 曲麻莱县| 亳州市|