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

树莓派3b 做网站服务器重庆高端品牌网站建设

树莓派3b 做网站服务器,重庆高端品牌网站建设,wordpress限制上传大小插件,阿里云服务器可以做商业网站前端面试题库 (面试必备) 推荐:★★★★★ 地址:前端面试题库 表妹一键制作自己的五星红旗国庆头像,超好看 最近开发了一个功能,页面首页会加载大量的图片,初次进入页面时&#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,基本的功能如下:

  1. 给图片提供默认的占位图片 src,同时提供data-src属性
  2. 传入图片对应的 ref 属性。
  3. 当图片进入可视区域时,使用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 lang="ts">
import { ref } from "vue";
import { useInView } from "./hooks/useInView";const imgRef = ref(null);
useInView(imgRef);
</script><template><h4>公众号:萌萌哒草头将军</h4><divv-for="(_, idx) in new Array(200).fill(11)"><imgref="imgRef"src="https://via.placeholder.com/200":data-src="`https://picsum.photos/200/${180 + idx}`"alt="b"/></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 lang="ts">
import { ref } from "vue";
import { useInView } from "./hooks/useInView";const imgRef = ref(null);useInView(imgRef);</script><template><h4>公众号:萌萌哒草头将军</h4><divv-for="(_, idx) in new Array(200).fill(11)"style="width: 200px height: 200px;"><imgref="imgRef"style="height: 100%"src="https://via.placeholder.com/200":data-src="`https://picsum.photos/200/${180 + idx}`"alt="b"/></div>
</template><style scoped>
/* 定义淡入动画 */
@keyframes fadeIn {from {opacity: 0;}to {opacity: 1;}
}/* 应用淡入动画到元素 */
.fade-in {animation: fadeIn 0.6s ease-in;
}
</style>

 前端面试题库 (面试必备)            推荐:★★★★★

地址:前端面试题库

 表妹一键制作自己的五星红旗国庆头像,超好看

http://www.dinnco.com/news/21118.html

相关文章:

  • 下载并安装百度地图正规优化公司哪家好
  • do_action wordpress 模板广州网络优化最早的公司
  • 网页建站工具百度官网认证免费
  • 电子商务网站建设pdf营销型网站优化
  • 阿里云做的网站怎么样宣传推广计划怎么写
  • 做管理信息的网站吗加速游戏流畅的软件
  • php做的网站用什么后台广州做seo公司
  • wordpress 更新版本百度seo按天计费
  • 泰安网站建设电话数字营销策略有哪些
  • 全国今日最新疫情seo推广骗局
  • wordpress vip 插件下载郑州网站建设优化
  • 深装总建设集团股份有限公司淘宝seo对什么内容优化
  • 织梦xml网站地图优化关键词排名推广
  • 网站开发技巧网络营销师报考条件
  • 在线阅读小说网站怎么做网站百度
  • 手机app微信网站建设百度快速排名提升
  • web service做网站个人如何注册网站
  • 企业网站备案名称窍门广州百度seo公司
  • 在建设部网站上的举报链接怎么做
  • 外贸网站建设公司 迅当网公司网络推广营销
  • 自适应网站建设多少钱快手流量推广网站
  • 寻找企业网站建设百度广告投放代理商
  • 网站开发前端php 后端python深圳互联网公司50强
  • 哈密市住房和城乡建设局网站泉州关键词搜索排名
  • 网站建设 钱windows优化大师win10
  • 我的世界怎么自己做皮肤并上传网站百度地图人工客服电话
  • 萝岗做网站seo刷排名软件
  • 高端网站开发找苏州觉世品牌网络宣传方案
  • 香港域名seo软件定制
  • 做设计赚钱网站有哪些东莞哪种网站推广好