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

深圳电信网络建站软装设计网站排名

深圳电信网络建站,软装设计网站排名,出口电商平台有哪些,ipv6可以做网站吗前端面试题库 #xff08;面试必备#xff09; 推荐#xff1a;★★★★★ 地址#xff1a;前端面试题库 表妹一键制作自己的五星红旗国庆头像#xff0c;超好看 最近开发了一个功能#xff0c;页面首页会加载大量的图片#xff0c;初次进入页面时#xff… 前端面试题库 面试必备            推荐★★★★★ 地址前端面试题库 表妹一键制作自己的五星红旗国庆头像超好看 最近开发了一个功能页面首页会加载大量的图片初次进入页面时会导致页面性能下降 于是乎我改进了这个功能可以让所有图片自动懒加载。 原理 这个功能主要的底层逻辑是是使用IntersectionObserver APIIntersectionObserver用于在浏览器中观察元素的可见性和位置变化。它可以帮助开发者实现一些动态行为如图片的懒加载、无限滚动等。 简单的示例如下 // 创建IntersectionObserver实例 const observer new IntersectionObserver((entries, observer) {// 遍历观察的元素entries.forEach(entry {// 如果元素可见if (entry.isIntersecting) {// 加载图片const img entry.target;const src img.getAttribute(data-src);img.setAttribute(src, src);// 停止观察该元素observer.unobserve(img);}}); });// 获取所有需要懒加载的图片元素 const lazyImages document.querySelectorAll(.lazy-image);// 观察每个图片元素 lazyImages.forEach(image {observer.observe(image); });实践 接下来我们实现一个通用的 hook基本的功能如下 给图片提供默认的占位图片 src同时提供data-src属性传入图片对应的 ref 属性。当图片进入可视区域时使用data-src属性替换 src 属性 import { onMounted, Ref } from vue; const options {// root: document.querySelector(.container), // 根元素默认为视口rootMargin: 0px, // 根元素的边距threshold: 0.5, // 可见性比例阈值once: true, };function callback(entries: IntersectionObserverEntry[],observer: IntersectionObserver ) {entries.forEach((entry) {// 处理每个目标元素的可见性变化if (entry.intersectionRatio 0) return;const img: Element entry.target;const src img.getAttribute(data-src);img.setAttribute(src, src ?? ); // 将真实的图片地址赋给 src 属性observer.unobserve(img);}); }export const useInView (ref: Ref) {const observer new IntersectionObserver(callback, options);onMounted(() {Object.keys(ref.value).forEach((e) observer.observe(ref.value[e]));}); }; script setup langts import { ref } from vue; import { useInView } from ./hooks/useInView;const imgRef ref(null); useInView(imgRef); /scripttemplateh4公众号萌萌哒草头将军/h4divv-for(_, idx) in new Array(200).fill(11)imgrefimgRefsrchttps://via.placeholder.com/200:data-srchttps://picsum.photos/200/${180 idx}altb//div /template实际效果如下 虽然基本的功能要求已经完成了但是现在还不够优雅 优化 接下来我们增加个过渡动画。每次当加载完图片就从占位图过渡到正常图片模式。 img.onload () {img.setAttribute(class, fade-in) }keyframes fadeIn {from {opacity: 0;}to {opacity: 1;} }/* 应用淡入动画到元素 */ .fade-in {animation: fadeIn 0.6s ease-in; }完整代码如下 import { onMounted, Ref } from vue; const options {// root: document.querySelector(.container), // 根元素默认为视口rootMargin: 0px, // 根元素的边距threshold: 0.5, // 可见性比例阈值once: true, };function callback(entries: IntersectionObserverEntry[],observer: IntersectionObserver ) {entries.forEach((entry) {if (entry.intersectionRatio 0) return;const img entry.target as HTMLImageElement;const src img.getAttribute(data-src);img.setAttribute(src, src ?? ); // 将真实的图片地址赋给 src 属性img.onload () {img.setAttribute(class, fade-in);};observer.unobserve(img);}); }export const useInView (ref: Ref) {const observer new IntersectionObserver(callback,options);onMounted(() {Object.keys(ref.value).forEach((e) observer.observe(ref.value[e]));}); };script setup langts import { ref } from vue; import { useInView } from ./hooks/useInView;const imgRef ref(null);useInView(imgRef);/scripttemplateh4公众号萌萌哒草头将军/h4divv-for(_, idx) in new Array(200).fill(11)stylewidth: 200px height: 200px;imgrefimgRefstyleheight: 100%srchttps://via.placeholder.com/200:data-srchttps://picsum.photos/200/${180 idx}altb//div /templatestyle scoped /* 定义淡入动画 */ keyframes fadeIn {from {opacity: 0;}to {opacity: 1;} }/* 应用淡入动画到元素 */ .fade-in {animation: fadeIn 0.6s ease-in; } /style前端面试题库 面试必备            推荐★★★★★ 地址前端面试题库 表妹一键制作自己的五星红旗国庆头像超好看
http://www.hkea.cn/news/14505243/

相关文章:

  • 鄱阳电商网站建设十堰市建设网站的公司
  • 深圳网站设计是什么谷歌浏览器网页版入口手机版
  • 天府新区规划建设国土局网站app开发专业公司
  • 网站开发需要掌握哪些技术如何做发表文章的网站
  • 莞城微网站建设公司做网站需要注意些什么
  • 云阳有没有做网站的黑龙江能建公司官网
  • 百科网站推广简单的网站建设模板
  • 福州网站建设出格网络秦皇岛汽车网站制作
  • worldpress做网站做网站设计师能10年赚100万吗
  • 网站建设一条龙怎么样聚企360做的网站
  • wordpress建立网站吗合肥建设云小程序
  • 用jsp做的二手交易网站公司名称及网址
  • 创建小型网站的步骤权威的徐州网站建设
  • 网站十大品牌ios开发用什么软件
  • 网站做app开发工具北京公司注册网址
  • 企业网站策划书下载钙网logo设计
  • 怎么做网站文章php源码项目门户网站开发
  • 自学建设网站广东深圳龙岗区地图
  • 怎么做服务器网站吗品牌网站建设 51下拉
  • 装饰公司网站建设流程怎么做一元抽奖网站
  • 没有网站可以做seo吗erp沙盘模拟实训报告
  • 上海史特网站建设微信支付申请网站建设未完成
  • 个人网站开发意义旅游网站首页设计大概图
  • 百度怎么建立自己的网站网络推广培训班哪家好
  • 工程公司有哪些广州seo网站营销
  • 免费的网页网站网站内页权重查询
  • 制作企业网站的西安租房网
  • 自己的电脑做网站空间视屏西湖专业网站设计公司
  • 西北建设有限公司网站微信商家小程序收费吗
  • 建网站书籍海口旅游类网站建设