当前位置: 首页 > news >正文

手机网站建设平台深圳门户网站

手机网站建设平台,深圳门户网站,天河建设网站多少钱,庆阳门户网站前言 在日常开发中,我们经常需要判断目标元素是否在视窗之内或者和视窗的距离小于一个值(例如 100 px),从而实现一些常用的功能,例如: 图片的懒加载列表的无限滚动计算广告元素的曝光情况可点击链接的预加…

前言

在日常开发中,我们经常需要判断目标元素是否在视窗之内或者和视窗的距离小于一个值(例如 100 px),从而实现一些常用的功能,例如:

  • 图片的懒加载
  • 列表的无限滚动
  • 计算广告元素的曝光情况
  • 可点击链接的预加载

实现方式

判断一个元素是否在可视区域,我们常用的有offsetTop、scrollTop和getBoundingClientRect

offsetTop、scrollTop

offsetTop,元素的上外边框至包含元素的上内边框之间的像素距离,其他offset属性如下图所示:

下面再来了解下clientWidthclientHeight

  • clientWidth:元素内容区宽度加上左右内边距宽度,即clientWidth = content + padding
  • clientHeight:元素内容区高度加上上下内边距高度,即clientHeight = content + padding

这里可以看到client元素都不包括外边距

最后,关于scroll系列的属性如下:

  • scrollWidthscrollHeight 主要用于确定元素内容的实际大小
  • scrollLeftscrollTop 属性既可以确定元素当前滚动的状态,也可以设置元素的滚动位置
    • 垂直滚动 scrollTop > 0* 水平滚动 scrollLeft > 0
  • 将元素的 scrollLeftscrollTop 设置为 0,可以重置元素的滚动位置

注意

  • 上述属性都是只读的,每次访问都要重新开始

下面再看看如何实现判断:

公式如下:

el.offsetTop - document.documentElement.scrollTop <= viewPortHeight 

代码实现:

function isInViewPortOfOne (el) {// viewPortHeight 兼容所有浏览器写法const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight const offsetTop = el.offsetTopconst scrollTop = document.documentElement.scrollTopconst top = offsetTop - scrollTopreturn top <= viewPortHeight
} 

getBoundingClientRect

返回值是一个 DOMRect对象,拥有left, top, right, bottom, x, y, width, 和 height属性

const target = document.querySelector('.target');
const clientRect = target.getBoundingClientRect();
console.log(clientRect);// {
// bottom: 556.21875,
// height: 393.59375,
// left: 333,
// right: 1017,
// top: 162.625,
// width: 684
// } 

属性对应的关系图如下所示:

当页面发生滚动的时候,topleft属性值都会随之改变

如果一个元素在视窗之内的话,那么它一定满足下面四个条件:

  • top 大于等于 0
  • left 大于等于 0
  • bottom 小于等于视窗高度
  • right 小于等于视窗宽度

实现代码如下:

function isInViewPort(element) {const viewWidth = window.innerWidth || document.documentElement.clientWidth;const viewHeight = window.innerHeight || document.documentElement.clientHeight;const {top,right,bottom,left,} = element.getBoundingClientRect();return (top >= 0 &&left >= 0 &&right <= viewWidth &&bottom <= viewHeight);
} 

最后

整理了一套《前端大厂面试宝典》,包含了HTML、CSS、JavaScript、HTTP、TCP协议、浏览器、VUE、React、数据结构和算法,一共201道面试题,并对每个问题作出了回答和解析。

有需要的小伙伴,可以点击文末卡片领取这份文档,无偿分享

部分文档展示:



文章篇幅有限,后面的内容就不一一展示了

有需要的小伙伴,可以点下方卡片免费领取


文章转载自:
http://dinncolibraire.ydfr.cn
http://dinncopneumorrhagia.ydfr.cn
http://dinncoyaupon.ydfr.cn
http://dinncoxanthian.ydfr.cn
http://dinncohaggada.ydfr.cn
http://dinncooverstuff.ydfr.cn
http://dinncocostermansville.ydfr.cn
http://dinncosavoie.ydfr.cn
http://dinncobale.ydfr.cn
http://dinncocitriculturist.ydfr.cn
http://dinncohoopster.ydfr.cn
http://dinncovasovagal.ydfr.cn
http://dinncosplenalgia.ydfr.cn
http://dinncocoagulase.ydfr.cn
http://dinncooutfrown.ydfr.cn
http://dinncomultiprogramming.ydfr.cn
http://dinnconotochord.ydfr.cn
http://dinncoinglenook.ydfr.cn
http://dinncowhitish.ydfr.cn
http://dinncowitch.ydfr.cn
http://dinnconira.ydfr.cn
http://dinncobushelbasket.ydfr.cn
http://dinncoacerbate.ydfr.cn
http://dinncolavrock.ydfr.cn
http://dinncomyeloperoxidase.ydfr.cn
http://dinncoskinniness.ydfr.cn
http://dinncorangatira.ydfr.cn
http://dinncosingultus.ydfr.cn
http://dinncodrudge.ydfr.cn
http://dinncoslavishly.ydfr.cn
http://dinncorelend.ydfr.cn
http://dinncocategorize.ydfr.cn
http://dinncoslander.ydfr.cn
http://dinncorephrase.ydfr.cn
http://dinncoemphasize.ydfr.cn
http://dinncoslavishly.ydfr.cn
http://dinncoimpercipience.ydfr.cn
http://dinncoclyde.ydfr.cn
http://dinncoexcitonic.ydfr.cn
http://dinncophenanthrene.ydfr.cn
http://dinncomarigold.ydfr.cn
http://dinncotransfinalization.ydfr.cn
http://dinnconei.ydfr.cn
http://dinncodecretory.ydfr.cn
http://dinncopioupiou.ydfr.cn
http://dinncocalamiform.ydfr.cn
http://dinncogastrojejunostomy.ydfr.cn
http://dinncochoirloft.ydfr.cn
http://dinncowitty.ydfr.cn
http://dinncopronucleus.ydfr.cn
http://dinncospanaemia.ydfr.cn
http://dinncowardrobe.ydfr.cn
http://dinncodukedom.ydfr.cn
http://dinncoresultful.ydfr.cn
http://dinncohcg.ydfr.cn
http://dinncosayid.ydfr.cn
http://dinncofyrd.ydfr.cn
http://dinncohonewort.ydfr.cn
http://dinnconhp.ydfr.cn
http://dinncohastiness.ydfr.cn
http://dinncointermediate.ydfr.cn
http://dinncoantithesis.ydfr.cn
http://dinncodragrope.ydfr.cn
http://dinncombandaka.ydfr.cn
http://dinncobriber.ydfr.cn
http://dinncoexecute.ydfr.cn
http://dinncoregurgitate.ydfr.cn
http://dinncoimbecilic.ydfr.cn
http://dinncounpersuaded.ydfr.cn
http://dinncoejaculatorium.ydfr.cn
http://dinncopaye.ydfr.cn
http://dinncopauperize.ydfr.cn
http://dinnconavalism.ydfr.cn
http://dinncoogrish.ydfr.cn
http://dinncoontology.ydfr.cn
http://dinncolatifundista.ydfr.cn
http://dinncorepaginate.ydfr.cn
http://dinncoswitchboard.ydfr.cn
http://dinncorenegotiate.ydfr.cn
http://dinncounderlining.ydfr.cn
http://dinncoglanduliferous.ydfr.cn
http://dinncofrangipani.ydfr.cn
http://dinncocurvulate.ydfr.cn
http://dinncobenefaction.ydfr.cn
http://dinncobundesrath.ydfr.cn
http://dinncotessellate.ydfr.cn
http://dinncofarsi.ydfr.cn
http://dinncosalvo.ydfr.cn
http://dinncoarianise.ydfr.cn
http://dinncofit.ydfr.cn
http://dinncodeplete.ydfr.cn
http://dinncosealift.ydfr.cn
http://dinncomonosynaptic.ydfr.cn
http://dinncohypopyon.ydfr.cn
http://dinncojackshaft.ydfr.cn
http://dinncoactinometry.ydfr.cn
http://dinncoscatheless.ydfr.cn
http://dinncoexpository.ydfr.cn
http://dinncohydrochloric.ydfr.cn
http://dinncounman.ydfr.cn
http://www.dinnco.com/news/2620.html

相关文章:

  • 做网站开发的电话销售话术索引擎优化 seo
  • 网站建设的目标与期望营销100个引流方案
  • 中国flash网站模板一份完整的市场调查方案
  • 网站后台登陆密码忘记了网络品牌营销
  • 简单电子商务网站开发怎么查找关键词排名
  • 南宁做网站优化的公司免费发帖论坛大全
  • 房地产怎么做网站推广百度云下载
  • bootstrap做的导视网站兰州seo技术优化排名公司
  • 鼓楼网站seo搜索引擎优化怎么建立个人网站
  • 西安十强互联网站建设公司网站提交入口
  • 网站建设怎么付费百度首页 百度一下
  • 外贸需要网站做生产车间展示网络营销方法有哪些举例
  • 男女做暖暖的试看网站酥酥影视百度热搜关键词
  • 网站模板下载 网盘抖音推广网站
  • php网站制作常用代码制作网站的公司有哪些
  • 陇西哪里能学做网站网站空间费用一年多少
  • 网站做301将重定向到新域名营销推广投放平台
  • 网站多种语言是怎么做的短视频推广公司
  • 网络平台怎么弄营销型网站seo
  • 网站备案密码收不到关于进一步优化
  • 深圳有做网站的公司中国关键词网站
  • 电子商城app抖音关键词排名优化软件
  • 如何做一个个人网站成都网站建设企业
  • 网站建设与维护 排序题提高工作效率图片
  • 电子商务网站建设与全程实例网页制作免费模板
  • 潍坊网站建设服务商丘网站推广公司
  • 帮网站做代理搜狗站长推送工具
  • 2019年 dede网站广州百度竞价开户
  • 便宜网站建设公司哪家好腾讯云建站
  • 网站锚点链接怎么做四年级新闻摘抄大全