亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

原生js獲取距離頁面頂端的距離

原生js獲取距離頁面頂端的距離

素胚勾勒不出你 2019-03-05 16:23:32
不用jquery的情況下,因為用的是vue框架,獲取元素到頁面頂端的距離demo的代碼如下:問題1 offsetTop只能獲取到有定位元素父級的距離,怎么獲取到頁面頂端距離呢問題2 子級有了transform 24px但offsetTop卻獲取不到 下面打印出來的值是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 //有定位元素的父級距離</script></html>
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

問題1 offsetTop只能獲取到有定位元素父級的距離,怎么獲取到頁面頂端距離呢

getBoundingClientRect().top

問題2 子級有了transform 24px但offsetTop卻獲取不到 下面打印出來的值是0

見問題1回答;

https://img1.sycdn.imooc.com//5c8c8cb300012fc302680104.jpg

對么?


查看完整回答
反對 回復 2019-03-16
  • 1 回答
  • 0 關注
  • 7620 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號