Browse Source

improved lazy loading card images

bach 2 years ago
parent
commit
1f8ae09838
19 changed files with 99 additions and 38 deletions
  1. 0 0
      web/themes/custom/materiotheme/assets/dist/main.js
  2. BIN
      web/themes/custom/materiotheme/assets/dist/main.js.gz
  3. 0 0
      web/themes/custom/materiotheme/assets/dist/module-article.bundle.js
  4. BIN
      web/themes/custom/materiotheme/assets/dist/module-article.bundle.js.gz
  5. 0 0
      web/themes/custom/materiotheme/assets/dist/module-base.bundle.js
  6. BIN
      web/themes/custom/materiotheme/assets/dist/module-base.bundle.js.gz
  7. 0 0
      web/themes/custom/materiotheme/assets/dist/module-thematique.bundle.js
  8. BIN
      web/themes/custom/materiotheme/assets/dist/module-thematique.bundle.js.gz
  9. 1 1
      web/themes/custom/materiotheme/assets/dist/report.html
  10. 0 0
      web/themes/custom/materiotheme/assets/dist/vclb.css
  11. BIN
      web/themes/custom/materiotheme/assets/dist/vclb.css.gz
  12. 17 0
      web/themes/custom/materiotheme/assets/dist/web_themes_custom_materiotheme_vuejs_components_Content_Card_vue.bundle.js
  13. BIN
      web/themes/custom/materiotheme/assets/dist/web_themes_custom_materiotheme_vuejs_components_Content_Card_vue.bundle.js.gz
  14. 7 0
      web/themes/custom/materiotheme/assets/styles/main.scss
  15. 2 2
      web/themes/custom/materiotheme/vuejs/components/Content/Card.vue
  16. 2 2
      web/themes/custom/materiotheme/vuejs/components/Content/CardThematique.vue
  17. 2 2
      web/themes/custom/materiotheme/vuejs/components/Content/MiniCard.vue
  18. 2 2
      web/themes/custom/materiotheme/vuejs/components/Content/ModalCard.vue
  19. 66 29
      web/themes/custom/materiotheme/vuejs/components/cardMixins.js

File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/main.js


BIN
web/themes/custom/materiotheme/assets/dist/main.js.gz


File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/module-article.bundle.js


BIN
web/themes/custom/materiotheme/assets/dist/module-article.bundle.js.gz


File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/module-base.bundle.js


BIN
web/themes/custom/materiotheme/assets/dist/module-base.bundle.js.gz


File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/module-thematique.bundle.js


BIN
web/themes/custom/materiotheme/assets/dist/module-thematique.bundle.js.gz


File diff suppressed because it is too large
+ 1 - 1
web/themes/custom/materiotheme/assets/dist/report.html


File diff suppressed because it is too large
+ 0 - 0
web/themes/custom/materiotheme/assets/dist/vclb.css


BIN
web/themes/custom/materiotheme/assets/dist/vclb.css.gz


File diff suppressed because it is too large
+ 17 - 0
web/themes/custom/materiotheme/assets/dist/web_themes_custom_materiotheme_vuejs_components_Content_Card_vue.bundle.js


BIN
web/themes/custom/materiotheme/assets/dist/web_themes_custom_materiotheme_vuejs_components_Content_Card_vue.bundle.js.gz


+ 7 - 0
web/themes/custom/materiotheme/assets/styles/main.scss

@@ -2864,3 +2864,10 @@ footer[role="contentinfo"]{
 
   }
 }
+
+
+img.lazy{
+  &.loading{
+    // background-color: red;
+  }
+}

+ 2 - 2
web/themes/custom/materiotheme/vuejs/components/Content/Card.vue

@@ -66,10 +66,10 @@
       <figure
         v-for="(img, index) in item.images"
         :key="img.url"
+        class="lazy"
+        v-lazy="index"
       >
         <img
-          class="lazy"
-          v-lazy="index"
           :data-src="img.style_cardmedium_url"
           :title="img.title"
         />

+ 2 - 2
web/themes/custom/materiotheme/vuejs/components/Content/CardThematique.vue

@@ -13,10 +13,10 @@
       <figure
         v-for="(img, index) in item.images"
         :key="img.url"
+        class="lazy"
+        v-lazy="index"
       >
         <img
-          class="lazy"
-          v-lazy="index"
           :data-src="img.style_cardmedium_url"
           :title="img.title"
         />

+ 2 - 2
web/themes/custom/materiotheme/vuejs/components/Content/MiniCard.vue

@@ -35,10 +35,10 @@
       <figure
         v-for="(img, index) in item.images"
         :key="img.url"
+        v-lazy="index"
+        class="lazy"
       >
         <img
-          class="lazy"
-          v-lazy="index"
           :data-src="img.style_minicard.url"
           :title="img.title"
         />

+ 2 - 2
web/themes/custom/materiotheme/vuejs/components/Content/ModalCard.vue

@@ -261,10 +261,10 @@
       <figure
         v-for="(img, index) in material.images"
         :key="img.url"
+        class="lazy"
+        v-lazy="index"
       >
         <img
-          class="lazy"
-          v-lazy="index"
           :data-src="img.style_cardfull.url"
           :title="img.title"
         />

+ 66 - 29
web/themes/custom/materiotheme/vuejs/components/cardMixins.js

@@ -2,13 +2,25 @@
 export default {
   directives: {
     lazy: {
-      bind (img, binding) {
-        // console.log('lazy bind', img, binding)
+      bind (figure, binding) {
+        // console.log('lazy bind', figure, binding)
         // show only the first image
         if (binding.value === 0) {
+          const img = figure.querySelector('img:not(.blank)')
+          figure.classList.add('loading')
+          img.addEventListener('load', function (e) {
+            console.log('img loaded', e)
+            figure.classList.remove('loading')
+            figure.classList.add('loaded')
+          })
+          img.addEventListener('error', function (e) {
+            console.error('img ERROR', e)
+            e.target.classList.remove('loading')
+            e.target.classList.add('error')
+          })
           img.setAttribute('src', img.getAttribute('data-src'))
-          img.removeAttribute('data-src')
-          img.classList.remove('lazy')
+          // img.removeAttribute('data-src')
+          // img.classList.remove('lazy')
         }
       }
     },
@@ -16,7 +28,7 @@ export default {
       inserted (el, binding) {
         // switch images on mousemove
         el.addEventListener('mousemove', function (event) {
-          const figs = this.querySelectorAll('figure')
+          const figs = this.querySelectorAll('figure.loaded')
           // console.log('mousemove', this, event, figs.length)
           // let len = figs.length
           // let w = this.clientWidth;
@@ -45,34 +57,59 @@ export default {
     console.log('card mounted', this.$options.name)
     // if (this.$options.name ==! 'ModalCard') {
     this.$el.addEventListener('mouseover', function (event) {
-      const imgs = this.querySelectorAll('.images figure img.lazy')
+      const figures = this.querySelectorAll('.images figure.lazy:not(.loaded):not(.loading)')
       // console.log('mousemove', this, imgs)
-      imgs.forEach((img) => {
+      figures.forEach((figure, index) => {
+        const img = figure.querySelector('img:not(.blank)')
         // console.log('forEach img',img)
-        img.setAttribute('src', img.getAttribute('data-src'))
-        img.removeAttribute('data-src')
-        img.classList.remove('lazy')
+        img.classList.add('loading')
+        img.addEventListener('load', function (e) {
+          // console.log('img loaded', e)
+          figure.classList.remove('loading')
+          figure.classList.add('loaded')
+        })
+        img.addEventListener('error', function (e) {
+          console.error('img ERROR', figure, e)
+        })
+        const src = img.getAttribute('data-src')
+
+        // for debug purpose
+        console.log(figures.length, index)
+        // let src = img.getAttribute('data-src')
+        // if (index > 0 && index < figures.length - 1) {
+        //   src = img.getAttribute('data-src').replace('?', '/bad?')
+        // }
+
+        img.setAttribute('src', src)
+        // img.removeAttribute('data-src')
+        // img.classList.remove('lazy')
       })
     }, { once: true })
     // }
-  },
-  methods: {
-    activateLazyLoad () {
-      console.log('card activateLazyLoad', this.$options.name)
-
-      this.$el.addEventListener('mousemove', function (event) {
-        const imgs = this.querySelectorAll('.images figure img.lazy')
-        // console.log('mousemove', this, imgs)
-        imgs.forEach((img) => {
-          // console.log('forEach img',img)
-          img.setAttribute('src', img.getAttribute('data-src'))
-          img.removeAttribute('data-src')
-          img.classList.remove('lazy')
-        })
-      }, { once: true })
-    }
-    // deg2rad (deg) {
-    //   return deg * (Math.PI / 180)
-    // },
   }
+  // ,
+  // methods: {
+  //   // not used
+  //   activateLazyLoad () {
+  //     console.log('card activateLazyLoad', this.$options.name)
+
+  //     this.$el.addEventListener('mousemove', function (event) {
+  //       const imgs = this.querySelectorAll('.images figure img.lazy')
+  //       // console.log('mousemove', this, imgs)
+  //       imgs.forEach((img) => {
+  //         // console.log('forEach img',img)
+  //         // img.classList.add('loading')
+  //         // img.onload = function (e) {
+  //         //   console.log('img loaded', e)
+  //         // }
+  //         img.setAttribute('src', img.getAttribute('data-src'))
+  //         img.removeAttribute('data-src')
+  //         img.classList.remove('lazy')
+  //       })
+  //     }, { once: true })
+  //   }
+  //   // deg2rad (deg) {
+  //   //   return deg * (Math.PI / 180)
+  //   // },
+  // }
 }

Some files were not shown because too many files changed in this diff