怎么在Vue中获取页面元素的相对位置

发布时间:2021-05-07 15:40:50 作者:Leah
来源:亿速云 阅读:2491

这期内容当中小编将会给大家带来有关怎么在Vue中获取页面元素的相对位置,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Vue的优点

Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。


当我滚动到元素的位置时候,我想把元素固定在头部

// 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.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)
  })
}

怎么在Vue中获取页面元素的相对位置

可以看到该元素的距离顶部595px,正常显示
当我先滚动一段距离后,然后再次刷新,滚动条位置还会记录之前的位置,这是top为195px,这也是正常的,因为getBoundingClientRect是根据浏览器窗口进行定位置的
而我想要的是想要不管浏览器滚动条位置在何处时刷新浏览器,我所绑定的dom元素都是根据文档左上角进行定位的

怎么在Vue中获取页面元素的相对位置

offsetTop

网上有人说用offsetTop,其实offsetTop是对当前对象到其上级层顶部的距离。不能对其进行赋值.设置对象到页面顶部的距离请用style.top属性

获取元素距离文档顶部距离

返回值是一个 DOMRect 对象,这个对象是由该元素的 getClientRects() 方法返回的一组矩形的集合, 即:是与该元素相关的 CSS 边框集合。
DOMRect 对象包含了一组用于描述边框的只读属性: left、top、right 和 bottom,单位为像素。除了 width 和 height 外的属性都是相对于视口的左上角位置而言的。
getBoundingClientRect返回值
  top: 元素上边框距离视窗顶部的距离
  bottom: 元素下边框距离视窗顶部的距离
  left: 元素左边框距离视窗左侧的距离
  right: 元素右边框距离视窗左侧的距离

由于getBoundingClientRect它们会随着视窗的滚动而相应的改变,那么元素距离页面顶部的距离就是,再加上滚动距离

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)
}

效果如下,不管滚动条何处位置都是一个相对文档最上面的左上角

怎么在Vue中获取页面元素的相对位置

阮一峰

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.$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)
}

效果如下

怎么在Vue中获取页面元素的相对位置

总结三种方法获取元素距离文档顶部位置

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中获取页面元素的相对位置了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. jquery获取元素在浏览器中位置的方法有哪些
  2. python如何获取列表中每个元素的下标位置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue

上一篇:怎么在ORACLE中配置邮件服务器

下一篇:使用tensorflow怎么自定义损失函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》