Vue获取页⾯元素的相对位置的⽅法⽰例
今天在开发源码⼀处发现有⼀处需要获取元素的相对位置⾼度,发现getBoundingClientRect有⼀个问题,它是⽤于获取某个元素相对于视窗的位置集合,达不到我想要的要求,如是看到阮⽼师写的⼀篇⽂章,关于⽤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.$BoundingClientRect){
this.scrollTop(this.$BoundingClientRect())
}
},
methods:{
// 这是封装的⼀个⽅法
scrollTop(h){
console.log(h);
this.utils.scrollTop((res)=>{
this.isFixed = res.scrollH > h ? true :false;
})
}
}
}
utils.js
// 该函数主要功能返回,滚动的⾼度以及⽂档占⽐窗⼝⾼度的百分⽐
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元素都是根据⽂档左上⾓进⾏定位的
offsetTop
⽹上有⼈说⽤offsetTop,其实offsetTop是对当前对象到其上级层顶部的距离。不能对其进⾏赋值.设置对象到页⾯顶部的距离请⽤p属性
获取元素距离⽂档顶部距离
返回值是⼀个 DOMRect 对象,这个对象是由该元素的 getClientRects() ⽅法返回的⼀组矩形的集合, 即:是与该元素相关的 CSS 边框集合。
DOMRect 对象包含了⼀组⽤于描述边框的只读属性: left、top、right 和 bottom,单位为像素。除了 width 和 height 外的属性都是相对于视⼝的左上⾓位置⽽⾔的。getBoundingClientRect返回值
top: 元素上边框距离视窗顶部的距离
bottom: 元素下边框距离视窗顶部的距离
left: 元素左边框距离视窗左侧的距离
right: 元素右边框距离视窗左侧的距离
由于getBoundingClientRect它们会随着视窗的滚动⽽相应的改变,那么元素距离页⾯顶部的距离就是,再加上滚动距离
this.$BoundingClientRect().top + window.scrollY;
或者
this.$BoundingClientRect().top+document.documentElement.scrollTop;
window.scrollY不兼容ie9,如需兼容请看
修改上⽅代码
if(this.$BoundingClientRect){
var top1 = this.$BoundingClientRect().top + window.scrollY
var top2 = this.$BoundingClientRect().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元素,元素可能有多个定位元素,需要通过递归的⽅式层层获取距离,然后相加
特别说明:需要将body的外边距设置为0,这样元素距离body顶部的距离就等同于距离⽂档顶部的距离
修改上⽅代码
if(this.$BoundingClientRect){
var top1 = this.$BoundingClientRect().top + window.scrollY
var top2 = this.$BoundingClientRect().top+document.documentElement.scrollTop;  // getElementTop在上⽅
var top3 = getElementTop(this.$refs.subnav)
console.log(top1)
如何设置滚动条的位置
console.log(top2)
console.log(top3)
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;
}
参考⽂章
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。