今天在開發源碼一處發現有一處須要獲取元素的相對位置高度,發現getBoundingClientRect有一個問題,它是用於獲取某個元素相對於視窗的位置集合,達不到我想要的要求,如是看到阮老師寫的一篇文章,關於用Javascript獲取頁面元素的位置,很好解決了個人我問題
當我滾動到元素的位置時候,我想把元素固定在頭部javascript
// html 結構 <div :class="['source-subnav', isFixed ? 'tab-nav-fixed' : '']" ref="subnav"> <ul> <li class="active"><a href="javascript:;">首頁推薦</a></li> <li><a href="javascript:;">最新發布</a></li> </ul> </div>
export default { data(){ return { isFixed:false, } }, mounted(){ if(this.$refs.subnav.getBoundingClientRect){ this.scrollTop(this.$refs.subnav.getBoundingClientRect()) } }, methods:{ // 這是封裝的一個方法 scrollTop(h){ console.log(h); this.utils.scrollTop((res)=>{ this.isFixed = res.scrollH > h ? true :false; }) } } }
utils.jshtml
// 該函數主要功能返回,滾動的高度以及文檔佔比窗口高度的百分比 utils.scrollTop = function(callback){ // 頁面總高 var totalH = document.body.scrollHeight || document.documentElement.scrollHeight; // 可視高 var clientH = window.innerHeight || document.documentElement.clientHeight; var result = {} window.addEventListener('scroll', function(e){ // 計算有效高 var validH = totalH - clientH // 滾動條捲去高度 var scrollH = document.body.scrollTop || document.documentElement.scrollTop // 百分比 result.percentage = (scrollH/validH*100).toFixed(2) result.scrollH = scrollH; callback && callback(result) }) }
能夠看到該元素的距離頂部595px,正常顯示
當我先滾動一段距離後,而後再次刷新,滾動條位置還會記錄以前的位置,這是top爲195px,這也是正常的,由於getBoundingClientRect
是根據瀏覽器窗口進行定位置的
而我想要的是想要無論瀏覽器滾動條位置在何處時刷新瀏覽器,我所綁定的dom元素都是根據文檔左上角進行定位的java
網上有人說用offsetTop,其實offsetTop是對當前對象到其上級層頂部的距離。不能對其進行賦值.設置對象到頁面頂部的距離請用style.top屬性git
返回值是一個 DOMRect 對象,這個對象是由該元素的 getClientRects() 方法返回的一組矩形的集合, 即:是與該元素相關的 CSS 邊框集合。 DOMRect 對象包含了一組用於描述邊框的只讀屬性: left、top、right 和 bottom,單位爲像素。除了 width 和 height 外的屬性都是相對於視口的左上角位置而言的。 getBoundingClientRect返回值 top: 元素上邊框距離視窗頂部的距離 bottom: 元素下邊框距離視窗頂部的距離 left: 元素左邊框距離視窗左側的距離 right: 元素右邊框距離視窗左側的距離
因爲getBoundingClientRect它們會隨着視窗的滾動而相應的改變,那麼元素距離頁面頂部的距離就是,再加上滾動距離github
this.$refs.subnav.getBoundingClientRect().top + window.scrollY; 或者 this.$refs.subnav.getBoundingClientRect().top+document.documentElement.scrollTop;
window.scrollY不兼容ie9,如需兼容請看Window.scrollY瀏覽器
修改上方代碼dom
if(this.$refs.subnav.getBoundingClientRect){ var top1 = this.$refs.subnav.getBoundingClientRect().top + window.scrollY var top2 = this.$refs.subnav.getBoundingClientRect().top+document.documentElement.scrollTop; console.log(top1) console.log(top2) this.scrollTop(top) }
效果以下,無論滾動條何處位置都是一個相對文檔最上面的左上角函數
function getElementTop(element){ var actualTop = element.offsetTop; var current = element.offsetParent; while (current !== null){ actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; }
offsetTop能夠返回元素距離offsetParent屬性返回元素頂部的距離(若是父元素有定位的,那麼將返回距離最近的定位元素,不然返回body元素,元素可能有多個定位元素,須要經過遞歸的方式層層獲取距離,而後相加this
特別說明: 須要將body的外邊距設置爲0,這樣元素距離body頂部的距離就等同於距離文檔頂部的距離spa
if(this.$refs.subnav.getBoundingClientRect){ var top1 = this.$refs.subnav.getBoundingClientRect().top + window.scrollY var top2 = this.$refs.subnav.getBoundingClientRect().top+document.documentElement.scrollTop; // getElementTop在上方 var top3 = getElementTop(this.$refs.subnav) console.log(top1) console.log(top2) console.log(top3) this.scrollTop(top) }
效果以下
dom.getBoundingClientRect().top + window.scrollY; 或者 dom.getBoundingClientRect().top+document.documentElement.scrollTop; 或者 function getElementTop(element){ var actualTop = element.offsetTop; var current = element.offsetParent; while (current !== null){ actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; }
用Javascript獲取頁面元素的位置
獲取元素距離頁面頂部的距離