Image lazy loading and Vue routing lazy loading

Image lazy loading

What is lazy loading?

Lazy loading is also calledlazy loadingorload on demand. Delayed loading of temporarily unseen image data in long web pages can optimize web page performance and improve user experience. In a long web page or application, if there are many pictures, it will take a long time to wait for all the pictures to be loaded, which will reduce the user experience and hinder the use of functions. At the same time, the user can only see the visual That part of the picture data in the window also wastes performance. Using lazy loading of images can solve the above problems.

Advantages of lazy loading

  • Reduce useless resource loading: Using lazy loading can reduce the pressure and traffic on the server and also reduce the burden on the browser.
  • Improve user experience: Loading more pictures at the same time may take a long time to wait, which affects the user experience. Using lazy loading can reduce the waiting time and improve user experience.
  • Prevent the loading of too many images from affecting the loading of other resource files: it will affect the normal use of website functions.

The principle of lazy loading:

Because the picture is loaded through the src attribute, when the corresponding picture does not appear in the visual interface, the address corresponding to the picture is not assigned to the src attribute, but the address is stored in the custom attribute data-xxx. When the image appears on the visual interface after scrolling, the image is loaded and displayed by obtaining the value of the custom attribute data-xxx of the corresponding image and assigning it to the src attribute.

Three implementations of lazy loading:

html code:

  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <img data-src="1.jpg" alt="">
  <img data-src="2.jpg" alt="">
  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <p>
    噫吁嚱,危乎高哉!蜀道之难,难于上青天!蚕丛及鱼凫,开国何茫然!尔来四万八千岁,不与秦塞通人烟。西当太白有鸟道,可以横绝峨眉巅。地崩山摧壮士死,然后天梯石栈相钩连。上有六龙回日之高标,下有冲波逆折之回川。黄鹤之飞尚不得过,猿猱欲度愁攀援。青泥何盘盘,百步九折萦岩峦。扪参历井仰胁息,以手抚膺坐长叹。

    问君西游何时还?畏途巉岩不可攀。但见悲鸟号古木,雄飞雌从绕林间。又闻子规啼夜月,愁空山。蜀道之难,难于上青天,使人听此凋朱颜!连峰去天不盈尺,枯松倒挂倚绝壁。飞湍瀑流争喧豗,砯崖转石万壑雷。其险也如此,嗟尔远道之人胡为乎来哉!

    剑阁峥嵘而崔嵬,一夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧身西望长咨嗟!</p>
  <img data-src="3.jpg" alt="">

method one:Display the image when the distance from the top of the target image to the top of the document (including the scrolling distance) is less than the visual interface + browser scrolling length.

  const imgs = document.querySelectorAll('img'); //获取全部图片元素
  const winInner = window.innerHeight; //可视界面的高度

  function scrollFun() {
    
    
    imgs.forEach((img) => {
    
    
      const imgScrollTop = img.offsetTop; //图片距离文档顶部的距离
      const bodyScrollTop = document.body.scrollTop || document.documentElement.scrollTop; //浏览器滚动的长度
      if (imgScrollTop < winInner + bodyScrollTop) {
    
     //当图片距离文档顶部的距离小于可视界面的高度+浏览器滚动的长度时加载对应的图片
        img.setAttribute('src', img.getAttribute('data-src'));
      }
    })
  }
  
  window.onscroll = scrollFun;

Method Two:Display the picture when the distance from the top of the target picture to the top of the visual interface is smaller than the visual interface.

  const imgs = document.querySelectorAll('img'); //获取全部图片元素
  const winInner = window.innerHeight; //可视界面的高度

  function scrollFun() {
    
    
    imgs.forEach((img) => {
    
    
      const scrollTop = img.getBoundingClientRect().top; //目标图片到可视界面顶部的距离
      if (scrollTop < winInner) {
    
    
        img.setAttribute('src', img.getAttribute('data-src'));
      }
    })
  }

  window.onscroll = scrollFun;

Method three:Use the IntersectionObserver constructor to perform intersection observations. The callback function fires both when observing and when not observing.

 //当可视界面滚动到图片区域时调用该回调函数
 const imgs = document.querySelectorAll('img'); //获取全部图片元素
 
 const callback = (entries) => {
    
     //entries是一个包含全部 img 元素的数组
    entries.forEach((entry) => {
    
    
      if (entry.isIntersecting) {
    
     //通过对当前的 img 元素的 isIntersecting 属性来进行判定该图片是否在可视界面区域
        const img = entry.target; //通过 target 属性获取到当前的 img 对象
        img.setAttribute('src', img.getAttribute('data-src'));
        observer.unobserve(img); //当该图片已经加载显示后进行 unobserve 不再进行观察
      }
    })
  }

  const observer = new IntersectionObserver(callback); //创建一个 observer 对象

  imgs.forEach((img) => {
    
    
    observer.observe(img); //将全部的 img 都进行观察绑定
  })

Recommended Video: Lazy Loading of JavaScript Images-Interview Questions for Web Front-end Engineers

PS:Method 1 and method 2 will be called repeatedly as long as the picture passes through the visual interface, so the performance is not optimized enough, and method 3 will not be triggered after unobserve unbinding observation, so the performance is high, but the IntersectionObserver constructor is compatible The problem.

Vue routing lazy loading

Generally speaking, in the router folder, use import to import the required components, and then apply the components to the corresponding path. This will load all the component files as soon as you enter the page, and no more requests will occur when you switch paths later.

import Home from '../views/Home.vue';

const routes = [
  {
    
    
	 path: '/',
	 name: 'Home',
	 component: Home,
  }
]

The lazy loading of Vue routes means that before jumping to the specified path, the components corresponding to the path are not loaded, that is, they are not directly imported using import, and then loaded when the path is jumped, which can improve the user experience. experience, and can also ensure that the use of functions is not hindered.

const routes = [
  {
    
    
	 path: '/about',
	 name: 'About',
	 component: () => import('../views/About.vue'),
  }
]

Recommended Video: What is Routing Lazy Loading? [Vue Routing]

Guess you like

Origin blog.csdn.net/qq_45488467/article/details/128574410