获取页面的各种距离

vue 中使用

created(){
	this.listenerFunction();
},
beforeDestroy() {
    document.removeEventListener("scroll", this.listenerFunction);
},
methods:{
	listenerFunction() {
      document.addEventListener("scroll", this.handleScroll, true);
    },
    handleScroll(e) {
      if (
        e.target.scrollHeight < 2000 &&
        e.target.offsetHeight + e.target.scrollTop >= e.target.scrollHeight - 10
      ) {
        this.staffShow = true;
      }
      if (
        e.target.scrollHeight > 2000 &&
        e.target.offsetHeight + e.target.scrollTop >= e.target.scrollHeight - 10
      ) {
        this.employeeShow = true;
      }
    },
}

以下引用于:
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42557996/article/details/103158928

在我们进行懒加载的逻辑时,经常会遇到各种获取距离问题,比如获取当前元素到页面底部的距离,获取文档网页底部到可视区窗口距离,获取…
大脑想象图形化比较困难的同学就会感觉很绕很麻烦,其实我们只需要获取到这五个值,就可以当前页面中存在的各种距离了

我特意做了一张图:
在这里插入图片描述

例如:

文档底部与可视区窗口底部的距离 = 文档总高度 - 文档滚动高度 - 可视区窗口高度
当前元素与底部的距离 = 可视区窗口高度 + 文档滚动高度 - 当前元素与页面顶部距离 - 当前元素高度

这样就简单很多了
附获取高度代码:(获取这些高度的方法很多,这里就不一一列出来了)

	console.log(window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0) // 文档滚动高度
	    
	console.log($(document).height()) // 文档总高度
	    
	console.log(document.documentElement.clientHeight) // 可视区窗口高度
	      
	console.log($('xxx').height()) // 当前元素高度
	     
	console.log($('xxx').offset().top) // 当前元素与页面顶部距离
	网页可见区域宽:document.body.clientWidth
	网页可见区域高:document.body.clientHeight
	网页可见区域宽:document.body.offsetWidth (包括边线的宽)
	网页可见区域高:document.body.offsetHeight (包括边线的宽)
	网页正文全文宽:document.body.scrollWidth
	网页正文全文高:document.body.scrollHeight
	网页被卷去的高:document.body.scrollTop
	网页被卷去的左:document.body.scrollLeft
	网页正文部分上:window.screenTop
	网页正文部分左:window.screenLeft
	屏幕分辨率的高:window.screen.height
	屏幕分辨率的宽:window.screen.width
	屏幕可用工作区高度:window.screen.availHeight
	屏幕可用工作区宽度:window.screen.availWidth
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值