不用jquery的情況下,因?yàn)橛玫氖莢ue框架,獲取元素到頁(yè)面頂端的距離demo的代碼如下:?jiǎn)栴}1 offsetTop只能獲取到有定位元素父級(jí)的距離,怎么獲取到頁(yè)面頂端距離呢問題2 子級(jí)有了transform 24px但offsetTop卻獲取不到 下面打印出來(lái)的值是0<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><p>dddddddddddddd</p><div class="box"> <div class="child"> </div></div></body><style>p{ line-height:29px; font-size:29px}.box{ width:100px; height:100px; margin-top: 120px; margin-left:180px; border:2px solid green; position: relative;}.child{width:50px; height:50px; transform: translateY(24px); border:1px solid red; }</style><script>var Child=document.getElementsByClassName('child')[0]var Box=document.getElementsByClassName('box')[0]console.log(Child.offsetTop) // 得到0console.log(Box.offsetTop); //178// console.log(Box.offsetLeft)// offsetTop //有定位元素的父級(jí)距離</script></html>
原生js獲取距離頁(yè)面頂端的距離
素胚勾勒不出你
2019-03-05 16:23:32