第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

原生js獲取距離頁(yè)面頂端的距離

原生js獲取距離頁(yè)面頂端的距離

素胚勾勒不出你 2019-03-05 16:23:32
不用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>
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

getBoundingClientRect().top

問題2 子級(jí)有了transform 24px但offsetTop卻獲取不到 下面打印出來(lái)的值是0

見問題1回答;

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

對(duì)么?


查看完整回答
反對(duì) 回復(fù) 2019-03-16
  • 1 回答
  • 0 關(guān)注
  • 7609 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)