亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript中offset、client、scroll的屬性總結(jié)

 更新時間:2015年08月13日 09:00:22   投稿:hebedich  
這篇文章主要介紹了javascript中offset、client、scroll的屬性總結(jié)的相關(guān)資料,需要的朋友可以參考下

HTML元素有幾個offset、client、scroll開頭的屬性,總是讓人摸不著頭腦。在書中看到記下來,分享給需要的小伙伴。主要是以下幾個屬性:

  第一組:offsetWidth,offsetHeight,offsetLeft,offsetTop,offsetParent

   第二組:clientWidth,clientHeight,clientLeft,clientTop

  第三組:scrollWidth,scrollHeight,scrollLeft,scrollTop

  詳細定義如下:

  1.1 HTML元素的offsetWidth,offsetHeight以CSS像素返回它的屏幕尺寸,包含元素的邊框和內(nèi)邊距,不包含外邊距。

  1.2 offsetLeft和offsetTop屬性返回元素的X和Y坐標。通常,它們返回值即是文檔坐標。但對于已定位元素的后代元素和一些其他元素(如表格單元),這些屬性返回的坐標是相對于祖先元素的而非文檔。

  1.3 offsetParent屬性指定offsetLeft,offsetTop相對的父元素。如果offsetParent為null,后兩者的返回值則為文檔坐標。因此一般來說,用offsetLeft和offsetTop來計算元素e的位置需要一個循環(huán):

//計算元素的文檔坐標
function getElementPosition(e){
  var x=0,y=0;
  while(e !=null){
    x +=e.offsetLeft;
    y +=e.offsetTop;
    e=e.offsetParent; 
  }
  return {x:x, y:y} ;  
} 

該方法計算的位置也不總是正確的,推薦使用內(nèi)置的getBoundingClientRect()方法。

  2.1 clientWidth和clientHeight類似于offsetWidth和offsetHeight屬性,不同的是它們不包含邊框大小,只包含內(nèi)容和內(nèi)邊距。同時,如果瀏覽器在內(nèi)邊距和邊框之間添加了滾動條,clientWidth和clientHeight的返回值也不包含滾動條。注意,對于類型<i>,<code>和<span>這些內(nèi)聯(lián)元素,clientWidth和clientHeight總是返回0。

  2.2 clientLeft和clientTop返回元素的內(nèi)邊距的外邊緣和它的邊框的外邊緣之間的水平距離和垂直距離,通常這些值就等于左邊和上邊的邊框?qū)挾?。但是如果元素有滾動條,并且瀏覽器將這些滾動條旋轉(zhuǎn)在左側(cè)或頂部,他們就還包含了滾動條的寬度。對于內(nèi)聯(lián)元素,它們總是為0。通常clientLeft和clientTop用處不大。

  3.1 scrollWidth和scollHeight是元素的內(nèi)容區(qū)域加上它的內(nèi)邊距再加上任何溢出內(nèi)容的尺寸。當內(nèi)容正好和內(nèi)容區(qū)域匹配而沒有溢出時,這些屬性與clientWidth和clientHeight是相等的。但當溢出時,它們就包含溢出的內(nèi)容,返回值比clientWidth和clientHeight要大。

  3.2 scrollLeft和scrollTop指定元素的滾動條的位置。注意,scrollLeft和scrollTop是可寫的,通過設(shè)置它們來讓元素中的內(nèi)容滾動(HTML元素并沒有類似Window對象的scrollTo()方法)。

obj.offset[WidthHeightTopLeft]  取控件相對于父控的位置
event.offset[XY] 取鼠標相在觸發(fā)事件的控件中的坐標
event.screen[XY] 鼠標相對于屏幕坐標
event.client[XY] 鼠標相對于網(wǎng)頁坐標在在
obj.scroll[WidthHeightTopLeft] 獲取對象滾動的大小
obj.client[WidthHeightTopLeft] 獲取對象可見區(qū)域的大小

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<style type="text/css">
<!--
div{
font-family: "宋體";
font-size: 12px;
color: #000000;
}
#div1{
position:absolute;
background-color:#f0f0f0;
width:200px;
height:200px;
left:20px;
top:0px;
z-index:1;
}
#div2{
background-color:#cfc0cf;
width:200px;
height:210px;
position:absolute;
left:261px;
top:347px;
z-index:100;
}
#div3{
background-color:#abbfbf;
width:200px;
height:200px;
position:absolute;
left:20px;
top:247px;
z-index:100;
}
#div4{
background-color:#cfcfcf;
width:200px;
height:200px;
position:absolute;
left:461px;
top:147px;
z-index:100;
}
-->
</style>
</head>
<body>
<div id='div1' >offset 控件相對于父窗體的位置</div>
<script>
function offset(ids){
ids.innerHTML="offsetLeft ="+ids.offsetLeft+"<BR>";
ids.innerHTML+="offsetWidth ="+ids.offsetWidth+"<BR>";
ids.innerHTML+="offsetHeight ="+ids.offsetHeight+"<BR>";
ids.innerHTML+="offsetTop ="+ids.offsetTop+"<BR>";
ids.innerHTML+="event.offset 鼠標相對于控件的位置<BR>";
ids.innerHTML+="offsetX ="+event.offsetX+"<BR>";
ids.innerHTML+="offsetY ="+event.offsetY+"<BR>";
}
function screen(ids){
ids.innerHTML="scrollWidth ="+ids.scrollWidth+"<BR>";
ids.innerHTML+="scrollHeight ="+ids.scrollHeight+"<BR>";
ids.innerHTML+="scrollTop ="+ids.scrollTop+"<BR>";
ids.innerHTML+="scrollLeft ="+ids.scrollLeft+"<BR>";
}
function client(ids){
ids.innerHTML="clientWidth ="+ids.clientWidth+"<BR>";
ids.innerHTML+="clientHeight ="+ids.clientHeight+"<BR>";
ids.innerHTML+="clientTop ="+ids.clientTop+"<BR>";
ids.innerHTML+="clientLeft ="+ids.clientLeft+"<BR>";
}
function eventc(ids){
ids.innerHTML="鼠標相對于屏幕坐標<BR>event.screenX="+event.screenX+"<BR>";
ids.innerHTML+="event.screenY ="+event.screenY+"<BR>";
ids.innerHTML+="鼠標相對于網(wǎng)頁坐標event.clientX="+event.clientX+"<BR>";
ids.innerHTML+="event.clientY ="+event.clientY+"<BR>";
}
</script>
</body>
</html>

各瀏覽器都有這些屬性,有些值可能不一樣。

自己寫代碼,對比一下結(jié)果,就明白了。

相關(guān)文章

最新評論