Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div v-if="renderComponent" v-lazyload class="image__wrapper">
  3.         <!--         <ImageSpinner v-if="!src" class="image__spinner" />-->
  4.         <img
  5.             class="image__item"
  6.             :src="src"
  7.             :data-srcset="getSrcset"
  8.             :data-src="dataSrc"
  9.             alt="random image"
  10.             width="100%"
  11.         >
  12.     </div>
  13. </template>
  14.  
  15. <script>
  16. // uncomment to use the loading spinner instead of the default gif
  17. import ImageSpinner from './ImageSpinner';
  18. import LazyLoad from '~/directives/LazyLoad';
  19. export default {
  20.     name: 'ImageItem',
  21.     // uncomment to use the loading spinner instead of the default gif
  22.     components: {
  23.         ImageSpinner,
  24.     },
  25.     directives: {
  26.         lazyload: LazyLoad,
  27.     },
  28.     props: {
  29.         // Image to lazyload
  30.         source: {
  31.             type: String,
  32.             default: '',
  33.             required: false,
  34.         },
  35.         srcset: {
  36.             type: String,
  37.             default: '',
  38.             required: true,
  39.         },
  40.         // Image while images load
  41.         src: {
  42.             type: String,
  43.             default: '/images/oval.svg',
  44.             required: false,
  45.         },
  46.         dataSrc: {
  47.             type: String,
  48.             required: false,
  49.         },
  50.     },
  51.     data() {
  52.         return {
  53.             renderComponent: true,
  54.         };
  55.     },
  56.     computed: {
  57.         getSrcset() {
  58.             return this.srcset || (this.src ? this.src : 'http://lorempixel.com/640/480');
  59.         },
  60.     },
  61.     watch: {
  62.         source: {
  63.             immediate: false,
  64.             handler() {
  65.                 this.forceRerender();
  66.             },
  67.         },
  68.         srcset: {
  69.             immediate: false,
  70.             handler() {
  71.                 this.forceRerender();
  72.             },
  73.         },
  74.     },
  75.     methods: {
  76.         forceRerender() {
  77.             // Remove my-component from the DOM
  78.             this.renderComponent = false;
  79.  
  80.             this.$nextTick(() => {
  81.                 // Add the component back in
  82.                 this.renderComponent = true;
  83.             });
  84.         },
  85.     },
  86. };
  87. </script>
  88.  
  89. <style lang="scss">
  90. .image {
  91.     &__wrapper {
  92.         display: flex;
  93.         justify-content: center;
  94.         align-items: center;
  95.         border-radius: 4px;
  96.  
  97.         &.loaded {
  98.             .image {
  99.                 &__item {
  100.                     visibility: visible;
  101.                     opacity: 1;
  102.                     border: 0;
  103.                 }
  104.  
  105.                 &__spinner {
  106.                     display: none !important;
  107.                     width: 100% !important;
  108.                     &.ripple {
  109.                         display: none !important;
  110.                         width: 100% !important;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.     &__item {
  118.         width: 100%;
  119.         /*border-radius: 4px;*/
  120.         transition: all 0.4s ease-in-out;
  121.         /* opacity: 0;
  122.         visibility: hidden; */
  123.     }
  124. }
  125. </style>
  126.  
  127.  
  128. //directive
  129.  
  130. export default {
  131.     inserted: (el) => {
  132.         function loadImage() {
  133.             const imageElement = Array.from(el.children).find((el) => el.nodeName === 'IMG');
  134.  
  135.             if (imageElement) {
  136.                 imageElement.addEventListener('load', () => {
  137.                     setTimeout(() => {
  138.                         el.classList.add('loaded');
  139.                         // if (!imageElement.dataset.lazy) {
  140.                         //  el.children[0].remove();
  141.                         // }
  142.                     }, 100);
  143.                 });
  144.                 imageElement.addEventListener('error', () => ({}));
  145.                 imageElement.src = imageElement.dataset.src;
  146.                 imageElement.srcset = imageElement.dataset.srcset;
  147.                 // imageElement.src = imageElement.dataset.src;
  148.             }
  149.         }
  150.  
  151.         function handleIntersect(entries, observer) {
  152.             entries.forEach((entry) => {
  153.                 if (!entry.isIntersecting) {
  154.                 } else {
  155.                     loadImage();
  156.                     observer.unobserve(el);
  157.                 }
  158.             });
  159.         }
  160.  
  161.         function createObserver() {
  162.             const options = {
  163.                 // circumstances under which the observer's callback is invoked
  164.                 root: null, // defaults to the browser viewport if not specified or if null
  165.                 threshold: '0', // the degree of intersection between the target element and its root (0 - 1)
  166.                 // threshold of 1.0 means that when 100% of the target is visible within
  167.                 // the element specified by the root option, the callback is invoked
  168.             };
  169.  
  170.             // Whether you're using the viewport or some other element as the root,the API works the same way,
  171.             // executing a callback function you provide whenever the visibility of the target element changes
  172.             // so that it crosses desired amounts of intersection with the root
  173.  
  174.             const observer = new IntersectionObserver(handleIntersect, options);
  175.  
  176.             observer.observe(el); // target element to watch
  177.         }
  178.  
  179.         if (!window.IntersectionObserver && typeof IntersectionObserver !== 'function') {
  180.             loadImage();
  181.         } else {
  182.             createObserver();
  183.         }
  184.     },
  185. };
  186.  
  187.  
  188. //usage
  189.  
  190. <LazyImage
  191.                 :srcset="activityListing.image_gallery && activityListing.image_gallery.cover_image && activityListing.image_gallery.cover_image.image_url"
  192.                 src="https://holobookings.imgix.net/images/image-galleries/rUXblxUtvHZGJwnXtmFlV4BbzngDp9DyieVwHZzp.jpeg?fit=crop&h=200&w=200&auto=format&q=10&blur=100"
  193.             />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement