最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery中offset()方法用法實(shí)例

 更新時(shí)間:2015年01月16日 15:21:35   投稿:shichen2014  
這篇文章主要介紹了jQuery中offset()方法用法,實(shí)例分析了offset()方法的功能、定義及返回或設(shè)置所匹配元素相對(duì)于document對(duì)象的偏移量時(shí)的使用技巧,需要的朋友可以參考下

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

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

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

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

獲取匹配元素在當(dāng)前document的相對(duì)偏移。
返回的對(duì)象包含兩個(gè)整型屬:top和left。
此方法只對(duì)可見(jiàn)元素有效。

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>offset()函數(shù)-腳本之家</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("元素的偏移量坐標(biāo)是:"+a.top+"|"+a.left+"");
   })
})
</script>
</head>
<body>
<div class="father">
  <div class="children"></div>
</div>
<button>獲取元素的坐標(biāo)</button>
</body>
</html>

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

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

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

設(shè)置匹配元素相對(duì)于document對(duì)象的坐標(biāo)。
offset()方法可以讓我們重新設(shè)置元素的位置。這個(gè)元素的位置是相對(duì)于document對(duì)象的。
如果對(duì)象原先的position樣式屬性是static的話,會(huì)被改成relative來(lái)實(shí)現(xiàn)重定位。

參數(shù)列表:

參數(shù) 描述
value 規(guī)定以像素計(jì)的 top 和 left 坐標(biāo)。
可能的值:
1.值對(duì),比如 {top:200,left:10}。
2.帶有top和left 屬性的對(duì)象。

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>offset()函數(shù)-腳本之家</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>點(diǎn)擊設(shè)置偏移量</button>
</body>
</html>

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

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

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

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

參數(shù)列表:

參數(shù) 描述
function(index,oldvalue) 規(guī)定返回被選元素新偏移坐標(biāo)的函數(shù):
index - 可選。元素的索引。
oldvalue - 可選。當(dāng)前坐標(biāo)。

實(shí)例代碼:

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

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.fzitv.net/" />
<title>offset()函數(shù)-腳本之家</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>點(diǎn)擊設(shè)置偏移量</button>
</body>
</html>

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

希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

房产| 玉屏| 额敏县| 成武县| 邯郸市| 沂源县| 柳江县| 喀什市| 洱源县| 麻江县| 镇远县| 峨眉山市| 循化| 临汾市| 呼和浩特市| 绥阳县| 东兴市| 如东县| 大城县| 祁连县| 广南县| 潜江市| 延吉市| 正定县| 澄江县| 赣州市| 同心县| 治县。| 海城市| 美姑县| 扬中市| 隆德县| 桂阳县| 横峰县| 龙泉市| 四子王旗| 太保市| 五指山市| 连山| 茂名市| 新泰市|