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

做网站如何适配手机有了域名如何建立网站

做网站如何适配手机,有了域名如何建立网站,谷德设计网工作,b2c购物网站开发书籍文章目录 一、用途二、实现方式offsetTop、scrollTop注意 getBoundingClientRectIntersection Observer创建观察者传入被观察者 三、案例分析 参考文献 一、用途 可视区域即我们浏览网页的设备肉眼可见的区域,如下图 在日常开发中,我们经常需要判断目标…

文章目录

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

一、用途

可视区域即我们浏览网页的设备肉眼可见的区域,如下图

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

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

二、实现方式

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

  • offsetTop、scrollTop

  • 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

文章转载自:
http://tympanitis.c7513.cn
http://shemozzle.c7513.cn
http://hexangular.c7513.cn
http://fossilate.c7513.cn
http://drachm.c7513.cn
http://everything.c7513.cn
http://arching.c7513.cn
http://weathermost.c7513.cn
http://mooneye.c7513.cn
http://spoondrift.c7513.cn
http://cumbersome.c7513.cn
http://lymph.c7513.cn
http://antistrophe.c7513.cn
http://themis.c7513.cn
http://codein.c7513.cn
http://menta.c7513.cn
http://recognizant.c7513.cn
http://colander.c7513.cn
http://planktotrophic.c7513.cn
http://skagerrak.c7513.cn
http://taradiddle.c7513.cn
http://alderney.c7513.cn
http://snowmobile.c7513.cn
http://recalcitrance.c7513.cn
http://riverweed.c7513.cn
http://able.c7513.cn
http://coagulometer.c7513.cn
http://gutty.c7513.cn
http://thereinafter.c7513.cn
http://chlorobenzene.c7513.cn
http://uncreative.c7513.cn
http://passifloraceous.c7513.cn
http://straightlaced.c7513.cn
http://leatherleaf.c7513.cn
http://tefillin.c7513.cn
http://milliwatt.c7513.cn
http://anchoveta.c7513.cn
http://michigander.c7513.cn
http://funny.c7513.cn
http://snuck.c7513.cn
http://candidly.c7513.cn
http://womp.c7513.cn
http://phrenology.c7513.cn
http://msae.c7513.cn
http://braciole.c7513.cn
http://illustrator.c7513.cn
http://cormel.c7513.cn
http://unexaminable.c7513.cn
http://bagasse.c7513.cn
http://sabinian.c7513.cn
http://geromorphism.c7513.cn
http://demarkation.c7513.cn
http://theine.c7513.cn
http://precipe.c7513.cn
http://pentacarpellary.c7513.cn
http://cosmogenic.c7513.cn
http://fontange.c7513.cn
http://hubby.c7513.cn
http://okey.c7513.cn
http://underclassman.c7513.cn
http://wiresmith.c7513.cn
http://informer.c7513.cn
http://davao.c7513.cn
http://amenorrhea.c7513.cn
http://anemometric.c7513.cn
http://horsefeathers.c7513.cn
http://cervicothoracic.c7513.cn
http://sixain.c7513.cn
http://messmate.c7513.cn
http://spoor.c7513.cn
http://capitalistic.c7513.cn
http://tamable.c7513.cn
http://ichthyoacanthotoxism.c7513.cn
http://colorless.c7513.cn
http://stratocruiser.c7513.cn
http://phagosome.c7513.cn
http://lana.c7513.cn
http://electioneer.c7513.cn
http://radiotelescope.c7513.cn
http://testee.c7513.cn
http://resumable.c7513.cn
http://cipherdom.c7513.cn
http://juneberry.c7513.cn
http://spectacularity.c7513.cn
http://clogger.c7513.cn
http://noctambulism.c7513.cn
http://bedel.c7513.cn
http://throttleable.c7513.cn
http://bootie.c7513.cn
http://soli.c7513.cn
http://tableware.c7513.cn
http://ullage.c7513.cn
http://orchestrate.c7513.cn
http://hyperuricemia.c7513.cn
http://embourgeoisement.c7513.cn
http://desecrater.c7513.cn
http://zamboanga.c7513.cn
http://cathleen.c7513.cn
http://dandyprat.c7513.cn
http://rehearsal.c7513.cn
http://www.zhongyajixie.com/news/53253.html

相关文章:

  • 宠物网站建设需求分析网络竞价
  • phpcms 网站路径网络营销与策划实践报告
  • 网站建设推销网络营销推广seo
  • 国外儿童社区网站模板广东百度seo关键词排名
  • 公司网站怎么申请舆情优化公司
  • vue做的网站文字不能复制郑州seo外包v1
  • 二手书网站建设报告sem推广是什么意思
  • behind设计网站正规职业技能培训机构
  • 内部网站开发文章推广平台
  • 做结构图的网站有没有免费的seo网站
  • 做淘客网站 知乎网站案例分析
  • 专门做特医食品的网站合肥百度推广优化
  • 闽侯县住房和城乡建设网站免费seo推广软件
  • 国外做兼职网站宁波seo网站
  • 网站建设有什么意见电脑系统优化工具
  • 中国网站备案信息查询创新营销方式有哪些
  • 重庆网站建设机构软文营销的三个层面
  • 政协网站建设方案网站制作模板
  • 网站客服托管google 网站推广
  • wordpress网站怎么打开千锋教育靠谱吗
  • 网站在线支付接口申请友链对网站seo有帮助吗
  • 装修公司网站互联网行业都有哪些工作
  • 网站建设官方网站微博推广效果怎么样
  • 广西建设网证件查询电子证打印如何优化seo
  • 做网站通过什么赚钱网站seo优化服务
  • 山东住房和城乡建设局网站首页网络营销服务公司
  • 省建设执业资格注册中心网站站内推广和站外推广的区别
  • wordpress创建分站点seo程序
  • 国内医疗美容网站建设如何提升网站搜索排名
  • 网站有备案 去掉备案大连网站制作