新聞中心
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在Vue中獲取頁面元素的相對位置,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站,為您提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設(shè)計,對服務(wù)搬家公司等多個行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗。創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報價服務(wù),我們深知市場的競爭激烈,認(rèn)真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
Vue的優(yōu)點(diǎn)
Vue具體輕量級框架、簡單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。
當(dāng)我滾動到元素的位置時候,我想把元素固定在頭部
// html 結(jié)構(gòu)
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.js
// 該函數(shù)主要功能返回,滾動的高度以及文檔占比窗口高度的百分比 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,正常顯示
當(dāng)我先滾動一段距離后,然后再次刷新,滾動條位置還會記錄之前的位置,這是top為195px,這也是正常的,因為getBoundingClientRect
是根據(jù)瀏覽器窗口進(jìn)行定位置的
而我想要的是想要不管瀏覽器滾動條位置在何處時刷新瀏覽器,我所綁定的dom元素都是根據(jù)文檔左上角進(jìn)行定位的
offsetTop
網(wǎng)上有人說用offsetTop,其實(shí)offsetTop是對當(dāng)前對象到其上級層頂部的距離。不能對其進(jìn)行賦值.設(shè)置對象到頁面頂部的距離請用style.top屬性
獲取元素距離文檔頂部距離
返回值是一個 DOMRect 對象,這個對象是由該元素的 getClientRects() 方法返回的一組矩形的集合, 即:是與該元素相關(guān)的 CSS 邊框集合。 DOMRect 對象包含了一組用于描述邊框的只讀屬性: left、top、right 和 bottom,單位為像素。除了 width 和 height 外的屬性都是相對于視口的左上角位置而言的。 getBoundingClientRect返回值 top: 元素上邊框距離視窗頂部的距離 bottom: 元素下邊框距離視窗頂部的距離 left: 元素左邊框距離視窗左側(cè)的距離 right: 元素右邊框距離視窗左側(cè)的距離
由于getBoundingClientRect它們會隨著視窗的滾動而相應(yīng)的改變,那么元素距離頁面頂部的距離就是,再加上滾動距離
this.$refs.subnav.getBoundingClientRect().top + window.scrollY; 或者 this.$refs.subnav.getBoundingClientRect().top+document.documentElement.scrollTop;
window.scrollY不兼容ie9,如需兼容請看Window.scrollY
修改上方代碼
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; }
實(shí)現(xiàn)原理
offsetTop可以返回元素距離offsetParent屬性返回元素頂部的距離(如果父元素有定位的,那么將返回距離最近的定位元素,否則返回body元素,元素可能有多個定位元素,需要通過遞歸的方式層層獲取距離,然后相加
特別說明: 需要將body的外邊距設(shè)置為0,這樣元素距離body頂部的距離就等同于距離文檔頂部的距離
修改上方代碼
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) }
效果如下
總結(jié)三種方法獲取元素距離文檔頂部位置
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; }
上述就是小編為大家分享的怎么在Vue中獲取頁面元素的相對位置了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:怎么在Vue中獲取頁面元素的相對位置
地址分享:http://www.dlmjj.cn/article/igjiej.html