日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

offsetLeft 解析

發布時間:2024/10/12 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 offsetLeft 解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:先看下w3c與之相關的介紹:

element.offsetHeight返回元素的高度。
element.offsetWidth返回元素的寬度。
element.offsetLeft返回元素的水平偏移位置。
element.offsetParent返回元素的偏移容器。
element.offsetTop返回元素的垂直偏移位置。

1,簡單來說就是偏移量,但它的參考對象到底是誰,我們慢慢來看.

<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>
<body>

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

效果如下:

這個demo中可以看出在默認情況下每個box的偏移值都是以瀏覽器為參考對象的.

?

2.如果我們給父級加上定位呢?(box1添加相對定位)

<body> <style> body { margin:0; } .box1 { width:300px; height:300px; margin:100px; background:#333; overflow:hidden; position:relative;} //box1設置相對定位 .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>

效果:

所以給父級添加定位之后,offset的偏移值就會變成相對于父級的偏移值.(除了position:static;外的所有定位,如fixed,relative,absolute都會使偏移值的參考對象變為父級))

?

?3.在我們給父級添加定位之后,還想獲取元素相對于瀏覽器窗口的偏移值怎么辦?

  思路很簡單,就是把元素本身的偏移量跟所有上級定位元素的偏移量都加起來就可以了,問題又來了,假如我們不知道他有幾個上級定位元素呢?

  這就需要封裝一個函數(用到了offsetParent :獲取上一級定位元素對象):

function offset(obj,direction){ //傳入對象 和 方向//將top,left首字母大寫,并拼接成offsetTop,offsetLeftvar 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; }

實際運用是這樣的:

<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><body> <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,offsetLeftvar 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>

?

運用到移動端拖拽事件(jquery):

// 拖拽事件var x1;var x2;var x_c;  //指針劃過的距離var xoffsetLeft;
$(
'#ulListMoble').on('touchstart', function (e) {var touch = e.originalEvent.targetTouches[0];x1 = touch.pageX;});$('#ulListMoble').on('touchmove', function (e) {var touch = e.originalEvent.targetTouches[0];xoffsetLeft = $('#ulListMoble')[0].offsetLeft;x2 = touch.pageX;x_c = x1 - x2;xoffsetLeft -= x_c;// console.log(xoffsetLeft);if (xoffsetLeft < 0) {$('#ulListMoble').css({ "left": xoffsetLeft + "px" })}x1 = x2;});})

?

總結

以上是生活随笔為你收集整理的offsetLeft 解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。