Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How use lozad with nuxt (vue) ? #247

Open
golubvladimir opened this issue Sep 18, 2020 · 1 comment
Open

How use lozad with nuxt (vue) ? #247

golubvladimir opened this issue Sep 18, 2020 · 1 comment

Comments

@golubvladimir
Copy link

I installed lozad by npm.

I created plugin lozad.js.

import Vue from 'vue';
import lozad from 'lozad';

let observer = lozad('.lozad', {
  threshold: 0
});

Object.defineProperty(Vue.prototype, '$lozad', { value: observer });

In container mounted lifecycle called function:

    mounted() {
      this.$lozad.observe();

I want use lozad with picture tag.

      <picture class="lozad">
        <source
          srcset="https://nuxtjs.org/logos/nuxt.svg"
          media="(min-width: 751px)"
        >
        <source
          srcset="https://vuejs.org/images/logo.png"
          media="(min-width: 320px)"
        >
        <img src="https://apoorv.pro/lozad.js/banner/lozad-banner.jpg" />
      </picture>

Why my placeholder image from img tag did not load, instead, the image from the first source.

@podlebar
Copy link

const observer = lozad(this.$el.querySelector('picture.lozad'), {
    rootMargin: '50px 0px',
    threshold: 0.1,
    load(el) {
    const sources = el.querySelectorAll('source')
    sources.forEach((source) => {
      source.setAttribute('srcset', source.getAttribute('data-srcset'))
    })
  }
})
observer.observe()

something like this made it work for me but i needed to change srcset attrib to data-srcset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants