一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看

jQuery中offset()方法運用示例

時(shí)間:2024-09-09 06:33:36 jQuery 我要投稿
  • 相關(guān)推薦

jQuery中offset()方法運用示例

  jQuery中offset()方法運用示例

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

  語(yǔ)法結構一:

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

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

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

  此方法只對可見(jiàn)元素有效。

  實(shí)例代碼:

  復制代碼 代碼如下:

  *{

  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;

  }

  $(document).ready(function(){

  $("button").click(function(){

  a=$(".children").offset();

  alert("元素的偏移量坐標是:"+a.top+"|"+a.left+"");

  })

  })

  獲取元素的坐標

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

  語(yǔ)法結構二:

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

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

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

  如果對象原先的position樣式屬性是static的話(huà),會(huì )被改成relative來(lái)實(shí)現重定位。

  參數列表:

  參數 描述  value 規定以像素計的 top 和 left 坐標。

  可能的值:

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

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

  實(shí)例代碼:

  復制代碼 代碼如下:

  .father{

  border:1px solid black;

  width:400px;

  height:300px;

  }

  .children{

  height:150px;

  width:200px;

  background-color:green;

  }

  $(document).ready(function(){

  $("button").click(function(){

  $(".children").offset({top:100,left:100})

  })

  })

  點(diǎn)擊設置偏移量

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

  語(yǔ)法結構三:

  使用函數的返回值來(lái)設置偏移坐標:

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

  參數列表:

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

  index - 可選。元素的索引。

  oldvalue - 可選。當前坐標。

  實(shí)例代碼:

  復制代碼 代碼如下:

  .father{

  border:1px solid black;

  width:400px;

  height:300px;

  }

  .children{

  height:150px;

  width:200px;

  background-color:green;

  }

  $(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;

  })

  })

  })

  點(diǎn)擊設置偏移量

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


【jQuery中offset()方法運用示例】相關(guān)文章:

jQuery中prev()方法用法07-16

jQuery中replaceAll()方法用法10-15

速記在語(yǔ)文學(xué)習中的運用方法08-05

jQuery中parent()和siblings()的問(wèn)題10-16

關(guān)于jQuery實(shí)現高亮顯示的方法介紹08-20

室內設計中色彩元素的運用方法09-13

鋼琴踏板運用方法07-31

室內設計中色彩的運用及其搭配方法10-06

asp.net+jquery Jsonp使用方法介紹08-23

色彩在景觀(guān)中的運用12-11

一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看