Bug - initial images not loading when the scrolling container appears on the screen
Hi @weavermedia. I鈥檓 not in the condition to check your code on code pen right now (and your debug view expired).
Question: have you set the scrolling container in the container option of the LazyLoad instance?
Could you please copy and paste the code in a comment here?
You can use the triple ` to format the code blocks.
Yep, I set the container in options. It's working when I scroll the container, just not when it first loads.
HTML:
<input type="hidden" role="uploadcare-uploader" name="my_file" />
<script src="https://ucarecdn.com/libs/widget/3.3.0/uploadcare.full.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/8.7.1/lazyload.min.js"></script>
CSS:
.uploadcare--tab_name_robots {
overflow: scroll;
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
JS:
UPLOADCARE_PUBLIC_KEY = "demopublickey";
UPLOADCARE_TABS = 'robots';
var robo_url, img_tag;
function robots(container, button, dialogApi, settings) {
button[0].title = "Robots";
var count = 500;
while (count--) {
robo_url = `https://robohash.org/${count}.png`;
img_tag = `<img width=200 height=200 data-src='${robo_url}'>`;
container.append(img_tag);
};
var myLazyLoad = new LazyLoad({
container: container[0]
});
console.log(myLazyLoad); // shows the correct container and elements
// myLazyLoad.update(); // tried this but seems to have no effect
}
uploadcare.registerTab('robots', robots);
Thanks for that.
Could you post here the working URL of the codepen you had created?
Thanks. Working on that right now.
The reason for your code is not working properly is that the uploadcare layer is shown AFTER the LazyLoad instance is created. So when it loops through elements to show, it doesn't find any.
First, very ugly solution that came to my mind and works: instantiate the LazyLoad after the uploadcare execution stack has finished. You can use a setTimeout function with 1 ms delay to do so.
setTimeout(function() {
var myLazyLoad = new LazyLoad({
container: container[0]
});
console.log(myLazyLoad);
}, 1);
Codepen here https://codepen.io/verlok/pen/xzKyKo?editors=1010
The more honest, cleaner way to do it would be to use the uploadcare Javascript API to create the LazyLoad object once the dialog is open and visible. But sincerly I've spent the last half an hour reading the uploadcare's dialog panel documentation and still didn't find a solution.
I'm sure it's there somewhere though. Maybe you know this better than me.
And this is how you do it properly using uploadcare's API
https://codepen.io/verlok/pen/NzWNvy?editors=1010
So long and thank you for all the fish.
Fantastic! Thanks for the updated pen. Working great now 馃帀
Most helpful comment
Fantastic! Thanks for the updated pen. Working great now 馃帀