全面解析JavaScript中offsetLeft、offsetTop的用法
前言:偏移量,很多動畫效果的實現(xiàn)都是通過去改變偏移量的改變來實現(xiàn)的,但是你真的完全了解offsetLeft,offsetTop嗎?
一、第一個小例子
<body> <style> body { margin:0; } .box1 { width:300px; height:300px; margin:100px; background:#333; overflow:hidden; } .box2 { width:200px; height:200px; margin:100px; background:#666; overflow:hidden; } .box3 { width:100px; height:100px; margin:100px; background:#999;} </style> <div class="box1"> <div class="box2"> <div class="box3"></div> </div> </div> <script> var oBox1 = document.querySelector('.box1'); var oBox2 = document.querySelector('.box2'); var oBox3 = document.querySelector('.box3'); console.log('box1: '+ oBox1.offsetLeft +','+ oBox1.offsetTop); console.log('box2: '+ oBox2.offsetLeft +','+ oBox2.offsetTop); console.log('box3: '+ oBox3.offsetLeft +','+ oBox3.offsetTop); </script> </body>
①第一個例子中,三個div的上一級的定位元素都是body,body是最外層的定位元素,三個div獲取到的offsetLeft值跟offsetTop值都是相對于body的偏移量。
二、第二個小例子(給box1添加相對定位)
<body> <style> body { margin:0; } .box1 { width:300px; height:300px; margin:100px; background:#333; overflow:hidden; position:relative;} .box2 { width:200px; height:200px; margin:100px; background:#666; overflow:hidden; } .box3 { width:100px; height:100px; margin:100px; background:#999;} </style> <div class="box1"> <div class="box2"> <div class="box3"></div> </div> </div> <script> var oBox1 = document.querySelector('.box1'); var oBox2 = document.querySelector('.box2'); var oBox3 = document.querySelector('.box3'); console.log('box1: '+ oBox1.offsetLeft +','+ oBox1.offsetTop); console.log('box2: '+ oBox2.offsetLeft +','+ oBox2.offsetTop); console.log('box3: '+ oBox3.offsetLeft +','+ oBox3.offsetTop); </script> </body>
②第二個例子中,box1加上了相對定位,這時候box2,box3的上一級定位元素不再是body了,這時他們獲取到的offsetLeft值跟offsetTop值都是相對于box1的偏移量。而box1的上一級定位元素還是body,所以他的偏移量還是相對于body的。
三、第三個小例子(給box1,box2添加相對定位)
<body> <style> body { margin:0; } .box1 { width:300px; height:300px; margin:100px; background:#333; overflow:hidden; position:relative; } .box2 { width:200px; height:200px; margin:100px; background:#666; overflow:hidden; position:relative; } .box3 { width:100px; height:100px; margin:100px; background:#999;} </style> <div class="box1"> <div class="box2"> <div class="box3"></div> </div> </div> <script> var oBox1 = document.querySelector('.box1'); var oBox2 = document.querySelector('.box2'); var oBox3 = document.querySelector('.box3'); console.log('box1: '+ oBox1.offsetLeft +','+ oBox1.offsetTop); console.log('box2: '+ oBox2.offsetLeft +','+ oBox2.offsetTop); console.log('box3: '+ oBox3.offsetLeft +','+ oBox3.offsetTop); </script> </body>
③第三個例子中,box1跟box2都加上了相對定位,這時候,box3的上一級定位元素變成是box2,box2的上一級定位元素是box1,box1的上一級定位元素還是body。所以這時候就出現(xiàn)了。三個div的偏移量都為100;
四、解析
通過上面的三個例子不難看出,offsetLeft值跟offsetTop值的獲取跟父級元素沒關(guān)系,而是跟其上一級的定位元素(除position:static;外的所有定位如fixed,relative,absolute)有關(guān)系。
五、擴展(在第三個例子中,假如我想獲取到box3到瀏覽器窗口的偏移量,該怎么去獲取呢?)
思路很簡單,就是把元素本身的偏移量跟所有上級定位元素的偏移量都加起來就可以了,問題又來了,假如我們不知道他有幾個上級定位元素呢?
其實也不難。js不但提供了offsetLeft、offsetTop方法,還提供了offsetParent(獲取上一級定位元素對象)的方法。所以現(xiàn)在我們只需封裝一個函數(shù)就可以了。
function offset(obj,direction){ //將top,left首字母大寫,并拼接成offsetTop,offsetLeft var offsetDir = 'offset'+ direction[0].toUpperCase()+direction.substring(1); var realNum = obj[offsetDir]; var positionParent = obj.offsetParent; //獲取上一級定位元素對象 while(positionParent != null){ realNum += positionParent[offsetDir]; positionParent = positionParent.offsetParent; } return realNum; }
運用程序中
<body> <style> body { margin:0; } .box1 { width:300px; height:300px; margin:100px; background:#333; overflow:hidden; position:relative; } .box2 { width:200px; height:200px; margin:100px; background:#666; overflow:hidden; position:relative; } .box3 { width:100px; height:100px; margin:100px; background:#999;} </style> <div class="box1"> <div class="box2"> <div class="box3"></div> </div> </div> <script> var oBox1 = document.querySelector('.box1'); var oBox2 = document.querySelector('.box2'); var oBox3 = document.querySelector('.box3'); function offset(obj,direction){ //將top,left首字母大寫,并拼接成offsetTop,offsetLeft var offsetDir = 'offset'+ direction[0].toUpperCase()+direction.substring(1); var realNum = obj[offsetDir]; var positionParent = obj.offsetParent; //獲取上一級定位元素對象 while(positionParent != null){ realNum += positionParent[offsetDir]; positionParent = positionParent.offsetParent; } return realNum; } console.log('box1: '+ offset(oBox1,'left') +','+ offset(oBox1,'top')); console.log('box2: '+ offset(oBox2,'left') +','+ offset(oBox2,'top')); console.log('box3: '+ offset(oBox3,'left') +','+ offset(oBox3,'top')); </script> </body>
運行結(jié)果為:
到此這篇關(guān)于全面解析JavaScript中offsetLeft、offsetTop的用法的文章就介紹到這了,更多相關(guān)JavaScript offsetLeft offsetTop內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用JavaScript給圖片添加水印的實現(xiàn)方法封裝
圖片加水印是一種常見的圖像處理技術(shù),通常用于保護版權(quán)、防止盜用、增加圖片的識別度等多種場景,這篇文章主要給大家介紹了關(guān)于使用JavaScript給圖片添加水印的實現(xiàn)方法封裝,需要的朋友可以參考下2024-03-03腳本吧 - 幻宇工作室用到j(luò)s,超強推薦share.js
腳本吧 - 幻宇工作室用到j(luò)s,超強推薦share.js...2006-12-12小程序自定義tabbar導(dǎo)航欄及動態(tài)控制tabbar功能實現(xiàn)方法(uniapp)
在項目中遇到一個需求,根據(jù)不同的賬號,生成不同的tabBar,下面這篇文章主要給大家介紹了關(guān)于小程序自定義tabbar導(dǎo)航欄及動態(tài)控制tabbar功能實現(xiàn)方法(uniapp)的相關(guān)資料,需要的朋友可以參考下2022-12-12純前端JavaScript實現(xiàn)Excel IO案例分享
這篇文章主要為大家詳細介紹了純前端JavaScript實現(xiàn)Excel IO案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08JavaScript+html5 canvas繪制的圓弧蕩秋千效果完整實例
這篇文章主要介紹了JavaScript+html5 canvas繪制的圓弧蕩秋千效果,以完整實例形式分析了JavaScript集合html5的canvas繪制圖形的相關(guān)技巧,需要的朋友可以參考下2016-01-01javascript代碼調(diào)試之console.log 用法圖文詳解
對于開始學(xué)js的朋友可能不知道為什么用console.log,頁面中也看不到信息,對于這個console.log腳本之家小編特整理與介紹一下,方便需要的朋友2016-09-09關(guān)于JavaScript的面向?qū)ο蠛屠^承有利新手學(xué)習(xí)
這是一篇關(guān)于JavaScript的面向?qū)ο蠛屠^承的文章,對想學(xué)習(xí)JavaScript中面向?qū)ο蟮耐瑢W(xué)來說是很有幫助,雖然一些Javascript用戶可能永遠也不需要知道原型或面向?qū)ο笳Z言的性質(zhì),但是那些來自傳統(tǒng)面向?qū)ο蟮恼Z言的開發(fā)者使用的時候會發(fā)現(xiàn)JavaScript的繼承模型非常的奇怪2013-01-01JavaScript原生數(shù)組函數(shù)實例匯總
這篇文章主要介紹了JavaScript原生數(shù)組函數(shù)實例匯總,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10