亚洲一级免费看,特黄特色大片免费观看播放器,777毛片,久久久久国产一区二区三区四区,欧美三级一区二区,国产精品一区二区久久久久,人人澡人人草

jQuery

jQuery中offset()方法運用示例

時間:2024-09-30 14:50:41 jQuery 我要投稿
  • 相關(guān)推薦

jQuery中offset()方法運用示例

  jQuery中offset()方法運用示例

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

  語法結(jié)構(gòu)一:

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

  獲取匹配元素在當(dāng)前document的相對偏移。

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

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

  實例代碼:

  復(fù)制代碼 代碼如下:

  *{

  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("元素的偏移量坐標(biāo)是:"+a.top+"|"+a.left+"");

  })

  })

  獲取元素的坐標(biāo)

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

  語法結(jié)構(gòu)二:

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

  設(shè)置匹配元素相對于document對象的坐標(biāo)。

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

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

  參數(shù)列表:

  參數(shù) 描述  value 規(guī)定以像素計的 top 和 left 坐標(biāo)。

  可能的值:

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

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

  實例代碼:

  復(fù)制代碼 代碼如下:

  .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})

  })

  })

  點擊設(shè)置偏移量

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

  語法結(jié)構(gòu)三:

  使用函數(shù)的返回值來設(shè)置偏移坐標(biāo):

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

  參數(shù)列表:

  參數(shù) 描述  function(index,oldvalue) 規(guī)定返回被選元素新偏移坐標(biāo)的函數(shù):

  index - 可選。元素的索引。

  oldvalue - 可選。當(dāng)前坐標(biāo)。

  實例代碼:

  復(fù)制代碼 代碼如下:

  .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;

  })

  })

  })

  點擊設(shè)置偏移量

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


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

聲樂中的氣息運用技巧02-24

UI設(shè)計中的色彩運用08-24

設(shè)計中顏色的運用技巧01-04

運用Excel制作圖表的方法11-28

色彩在平面設(shè)計中的運用04-10

現(xiàn)代舞中呼吸的運用03-04

2023少兒聲樂中的“趣味教學(xué)”運用11-08

傳統(tǒng)視覺要素在動畫設(shè)計中的運用09-05

Excel中if函數(shù)使用的方法06-16

色彩搭配在平面設(shè)計中的運用12-24