淺談jQuery的offset()方法及示例分享
offset()方法的定義和用法:
此方法返回或設(shè)置所匹配元素相對(duì)于document對(duì)象的偏移量。
語法結(jié)構(gòu)一:
$(selector).offset()
獲取匹配元素在當(dāng)前document的相對(duì)偏移。
返回的對(duì)象包含兩個(gè)整型屬:top和left。
此方法只對(duì)可見元素有效。
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<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的偏移量。
語法結(jié)構(gòu)二:
$(selector).offset(value)
設(shè)置匹配元素相對(duì)于document對(duì)象的坐標(biāo)。
offset()方法可以讓我們重新設(shè)置元素的位置。這個(gè)元素的位置是相對(duì)于document對(duì)象的。
如果對(duì)象原先的position樣式屬性是static的話,會(huì)被改成relative來實(shí)現(xiàn)重定位。
參數(shù)列表:
參數(shù) 描述
value 規(guī)定以像素計(jì)的 top 和 left 坐標(biāo)。
可能的值:
1.值對(duì),比如 {top:200,left:10}。
2.帶有top和left 屬性的對(duì)象。
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<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的偏移量。
語法結(jié)構(gòu)三:
使用函數(shù)的返回值來設(shè)置偏移坐標(biāo):
$(selector).offset(function(index,oldoffset))
參數(shù)列表:
參數(shù) 描述
function(index,oldvalue) 規(guī)定返回被選元素新偏移坐標(biāo)的函數(shù):
index - 可選。元素的索引。
oldvalue - 可選。當(dāng)前坐標(biāo)。
實(shí)例代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<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è)置元素的偏移,不過值是通過函數(shù)返回。
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
基于jquery實(shí)現(xiàn)左右上下移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)左右上下移動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
jQuery插件之Tocify動(dòng)態(tài)節(jié)點(diǎn)目錄菜單生成器附源碼下載
Tocify是一個(gè)能夠動(dòng)態(tài)生成文章節(jié)點(diǎn)目錄的jQuery插件,通過本文給大家分享jQuery插件之Tocify動(dòng)態(tài)節(jié)點(diǎn)目錄菜單生成器,對(duì)tocify菜單生成器相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
jQuery的DOM操作之刪除節(jié)點(diǎn)示例
如果文檔中某一個(gè)元素多余,那么應(yīng)將其刪除。jQuery提供了兩種刪除節(jié)點(diǎn)的方法,remove()方法和empty()方法下面為大家詳細(xì)介紹下2014-01-01
使用jquery/js獲取iframe父子級(jí)、同級(jí)獲取元素的方法
下面小編就為大家?guī)硪黄褂胘query/js獲取iframe父子級(jí)、同級(jí)獲取元素的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
jQuery+Ajax+PHP彈出層異步登錄效果(附源碼下載)
本文我們給大家介紹如何使用jQuery+Ajax+PHP彈出層異步登錄的應(yīng)用。感興趣的朋友通過本文學(xué)習(xí)吧2016-05-05

