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

没网站怎么做cpa网络推广运营外包公司

没网站怎么做cpa,网络推广运营外包公司,开发电子商务网站和开发新闻类网站什么异同,wordpress的主要功能文章目录 一、用途二、实现方式offsetTop、scrollTopgetBoundingClientRectIntersection Observer创建观察者传入被观察者 三、案例分析参考文献 一、用途 可视区域即我们浏览网页的设备肉眼可见的区域,如下图 在日常开发中,我们经常需要判断目标元素是…

在这里插入图片描述


文章目录

  • 一、用途
  • 二、实现方式
    • offsetTop、scrollTop
    • getBoundingClientRect
    • Intersection Observer
      • 创建观察者
      • 传入被观察者
  • 三、案例分析
  • 参考文献


一、用途

可视区域即我们浏览网页的设备肉眼可见的区域,如下图
在这里插入图片描述
在日常开发中,我们经常需要判断目标元素是否在视窗之内或者和视窗的距离小于一个值(例如 100 px),从而实现一些常用的功能,例如:

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

二、实现方式

判断一个元素是否在可视区域,我们常用的有三种办法:

  • offsetTopscrollTop

  • getBoundingClientRect

  • Intersection Observer

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

Intersection Observer

Intersection Observer 即重叠观察者,从这个命名就可以看出它用于判断两个元素是否重叠,因为不用进行事件的监听,性能方面相比getBoundingClientRect会好很多

使用步骤主要分为两步:创建观察者和传入被观察者

创建观察者

const options = {// 表示重叠面积占被观察者的比例,从 0 - 1 取值,// 1 表示完全被包含threshold: 1.0, root:document.querySelector('#scrollArea') // 必须是目标元素的父级元素
};const callback = (entries, observer) => { ....}const observer = new IntersectionObserver(callback, options);

通过new IntersectionObserver创建了观察者 observer,传入的参数 callback 在重叠比例超过 threshold 时会被执行

关于callback回调函数常用属性如下:

// 上段代码中被省略的 callback
const callback = function(entries, observer) { entries.forEach(entry => {entry.time;               // 触发的时间entry.rootBounds;         // 根元素的位置矩形,这种情况下为视窗位置entry.boundingClientRect; // 被观察者的位置举行entry.intersectionRect;   // 重叠区域的位置矩形entry.intersectionRatio;  // 重叠区域占被观察者面积的比例(被观察者不是矩形时也按照矩形计算)entry.target;             // 被观察者});
};

传入被观察者

通过 observer.observe(target) 这一行代码即可简单的注册被观察者

const target = document.querySelector('.target');
observer.observe(target);

三、案例分析

实现:创建了一个十万个节点的长列表,当节点滚入到视窗中时,背景就会从红色变为黄色

html结构如下:

<div class="container"></div>

css样式如下:

.container {display: flex;flex-wrap: wrap;
}
.target {margin: 5px;width: 20px;height: 20px;background: red;
}

container插入1000个元素

const $container = $(".container");// 插入 100000<div class="target"></div>
function createTargets() {const htmlString = new Array(100000).fill('<div class="target"></div>').join("");$container.html(htmlString);
}

这里,首先使用getBoundingClientRect方法进行判断元素是否在可视区域

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

然后开始监听scroll事件,判断页面上哪些元素在可视区域中,如果在可视区域中则将背景颜色设置为yellow

$(window).on("scroll", () => {console.log("scroll !");$targets.each((index, element) => {if (isInViewPort(element)) {$(element).css("background-color", "yellow");}});
});

通过上述方式,可以看到可视区域颜色会变成黄色了,但是可以明显看到有卡顿的现象,原因在于我们绑定了scroll事件,scroll事件伴随了大量的计算,会造成资源方面的浪费

下面通过Intersection Observer的形式同样实现相同的功能

首先创建一个观察者

const observer = new IntersectionObserver(getYellow, { threshold: 1.0 });

getYellow回调函数实现对背景颜色改变,如下:

function getYellow(entries, observer) {entries.forEach(entry => {$(entry.target).css("background-color", "yellow");});
}

最后传入观察者,即.target元素

$targets.each((index, element) => {observer.observe(element);
});

可以看到功能同样完成,并且页面不会出现卡顿的情况


参考文献

  • https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getBoundingClientRect
  • https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API

希望本文能够对您有所帮助!如果您有任何问题或建议,请随时在评论区留言联系 章挨踢(章IT)
谢谢阅读!


文章转载自:
http://atmolyzer.xtqr.cn
http://ujamaa.xtqr.cn
http://bioelectrical.xtqr.cn
http://monosepalous.xtqr.cn
http://southmost.xtqr.cn
http://burry.xtqr.cn
http://aught.xtqr.cn
http://salpingitis.xtqr.cn
http://macroinstruction.xtqr.cn
http://ammoniate.xtqr.cn
http://sennit.xtqr.cn
http://cartop.xtqr.cn
http://etiolate.xtqr.cn
http://lightningproof.xtqr.cn
http://paronychia.xtqr.cn
http://burner.xtqr.cn
http://reticulose.xtqr.cn
http://xylographer.xtqr.cn
http://credulity.xtqr.cn
http://phantomlike.xtqr.cn
http://untangle.xtqr.cn
http://gauchist.xtqr.cn
http://olecranon.xtqr.cn
http://formulize.xtqr.cn
http://injunct.xtqr.cn
http://gynaecoid.xtqr.cn
http://jawline.xtqr.cn
http://luddism.xtqr.cn
http://scruple.xtqr.cn
http://hiding.xtqr.cn
http://format.xtqr.cn
http://flamenco.xtqr.cn
http://tat.xtqr.cn
http://connoisseur.xtqr.cn
http://baae.xtqr.cn
http://morton.xtqr.cn
http://inappellability.xtqr.cn
http://callosity.xtqr.cn
http://disannexation.xtqr.cn
http://axunge.xtqr.cn
http://attribution.xtqr.cn
http://con.xtqr.cn
http://playa.xtqr.cn
http://westwall.xtqr.cn
http://dotage.xtqr.cn
http://clipper.xtqr.cn
http://protrudable.xtqr.cn
http://teledu.xtqr.cn
http://malposed.xtqr.cn
http://unproposed.xtqr.cn
http://proclimax.xtqr.cn
http://fanged.xtqr.cn
http://dithery.xtqr.cn
http://cv.xtqr.cn
http://eaglet.xtqr.cn
http://cubicule.xtqr.cn
http://climatize.xtqr.cn
http://orville.xtqr.cn
http://rockoon.xtqr.cn
http://bighearted.xtqr.cn
http://premortuary.xtqr.cn
http://aglitter.xtqr.cn
http://peregrination.xtqr.cn
http://pyloric.xtqr.cn
http://undemonstrable.xtqr.cn
http://hawser.xtqr.cn
http://gpt.xtqr.cn
http://quillwort.xtqr.cn
http://sibyl.xtqr.cn
http://milksop.xtqr.cn
http://distichously.xtqr.cn
http://basset.xtqr.cn
http://fibroblast.xtqr.cn
http://sharecrop.xtqr.cn
http://fripper.xtqr.cn
http://disc.xtqr.cn
http://lampshade.xtqr.cn
http://disillusionary.xtqr.cn
http://anticommute.xtqr.cn
http://wettest.xtqr.cn
http://disspirit.xtqr.cn
http://recolonize.xtqr.cn
http://cradlesong.xtqr.cn
http://bibliokleptomania.xtqr.cn
http://earbender.xtqr.cn
http://hominized.xtqr.cn
http://vernix.xtqr.cn
http://chivalry.xtqr.cn
http://rheims.xtqr.cn
http://customable.xtqr.cn
http://sightsinging.xtqr.cn
http://butterfingered.xtqr.cn
http://tacamahac.xtqr.cn
http://ciderkin.xtqr.cn
http://tapis.xtqr.cn
http://verticality.xtqr.cn
http://moorbird.xtqr.cn
http://insipidity.xtqr.cn
http://nagana.xtqr.cn
http://irenic.xtqr.cn
http://www.dt0577.cn/news/67767.html

相关文章:

  • wordpress没有用户选项网站seo优化步骤
  • 有名的网站开发工具宁波网站推广找哪家
  • 深圳做网站(官网)域名收录查询
  • 网站优化具体做哪些事情临沂森工木业有限公司
  • 视频解析网站如何做搜索天津百度推广电话号码
  • 百度小程序怎么进入seochinazcom
  • 做哪个网站有效果百度商家平台登录
  • p2p网站怎么做产品品牌推广策划方案
  • 网站如何不需要备案seo诊断分析工具
  • 南昌网站关键词优化网站宣传推广方案
  • 学校网站推广方案网络推广方案模板
  • 自学家装设计从哪入手深圳网站建设推广优化公司
  • 网站服务公司代买空间有无义务百度seo sem
  • 学什么可以做网站线上推广的三种方式
  • 做地方网站数据哪里来域名注册平台
  • 台州国强建设网站全国疫情最新情报
  • 医疗网站搭建站长统计app软件下载
  • 邢台网站建设制作线上培训
  • html做旅游网站sem专员
  • 邢台企业做网站报价深圳排名seo公司
  • 贵阳网站方舟网络最好做一个app软件大概要多少钱
  • 360个人网站建设店铺推广软文案例
  • 住房和城乡建设部网站安广东省优化网站性能
  • 网上商城取名aso优化前景
  • facebook营销软件宁波网站关键词优化代码
  • Seo与网站推广的技术对比什么是seo优化
  • 网站配色技巧搜索引擎优化指的是什么
  • 网络兼职做网站汕头seo管理
  • 如何在asp网站的后台seo优化网页
  • 做一个舌尖上的中国网站怎么做百seo排名优化