vue-lazyload icon indicating copy to clipboard operation
vue-lazyload copied to clipboard

Loaded event triggering on every scroll event

Open rdunk opened this issue 7 years ago • 10 comments

I'm using the loaded adapter alongside vue-router. I'm finding that on initial page load the loaded event is triggered once per image, which is expected. After changing routes I find that the loaded event is triggered on every scroll event for every visible image, even if the image has already loaded. Setup like this:

Vue.use(VueLazyload, {
  preLoad: 1.8,
  error: false,
  loading: false,
  attempt: 1,
  supportWebp: false,
  adapter: {
    loaded({ el }) {
      console.log(el);
    },
  },
});

Is this expected behaviour?

Online example here: http://requirebin.com/?gist=rdunk/1371389794270c9672bb15f4e24d40c3

Try changing route a few times and then scrolling, should see the events in the console.

rdunk avatar Mar 02 '17 13:03 rdunk

Vue.use(VueLazyload, {
  preLoad: 1.8,
  error: false,
  loading: false,
  attempt: 1,
  supportWebp: false,
  adapter: {
    loaded({ el }, formCache) {
        if (!fromCache) {
            console.log(listener)
       }
    },
  },
});

vm.$Lazyload.$on('loaded', function (listener, formCache) {
  if (!fromCache) {
    console.log(listener)
  }
})

hilongjw avatar Mar 03 '17 08:03 hilongjw

Unfortunately that means the function will only execute the first time an image loads, but not if it is lazyloaded again when returning to a previously visited route. In my case I need to execute a function each time an image is lazyloaded, even if it is cached.

I updated the requirebin example above to use app.$Lazyload.performance() and noticed that when returning to a previously viewed route, the state is always shown as 'loading' rather than 'loaded'.

screen shot 2017-03-03 at 09 58 36

rdunk avatar Mar 03 '17 09:03 rdunk

I think it's the listener update did not compare src with old src, it is fixed in v1.0.3

hilongjw avatar Mar 30 '17 05:03 hilongjw

Still seeing the same issue with v1.0.3 unfortunately.

rdunk avatar Mar 30 '17 08:03 rdunk

Still seeing this with v1.0.6. Easiest way to replicate is to use the following code, and then navigate between routes with images that have already been lazyloaded:

vm.$Lazyload.$on('loaded', () => {
  console.log(vm.$Lazyload.ListenerQueue.map(e => e.state.loaded));
});

You'll see this 'loaded' event is triggered at every scroll event for every element using the directive if that element has been previously lazyloaded, and you'll notice the state of each element is always 'loading'.

I'm trying to work out a fix now, but need to familiarise myself with the project a bit more, so may benefit from someone else weighing in.

rdunk avatar Aug 17 '17 10:08 rdunk

@rdunk Did you find the solution for that. I am also facing the same problem. As I see this is an older issue but I am still facing issue in 1.2.6

fawadaslam1993 avatar Sep 01 '20 14:09 fawadaslam1993

@fawadaslam1993 Sorry to say but I ended up dropping this library and writing my own lazyloading implementation if I recall correctly.

rdunk avatar Sep 02 '20 09:09 rdunk

@rdunk Can you suggest to me how you can do your own lazyloading?

fawadaslam1993 avatar Sep 02 '20 09:09 fawadaslam1993

@fawadaslam1993 Managed to dig it out. This is just a replacement for the component, not the directive, so not sure if it will do what you need it to do.

Hope it helps.

https://gist.github.com/rdunk/98c9c2a80dace8e73472b81e3e9ce97b

rdunk avatar Sep 08 '20 22:09 rdunk

Thanks. I'll try :)

fawadaslam1993 avatar Sep 09 '20 09:09 fawadaslam1993