Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Video lazy load – background

I wanna use video as a background on my website. I have this:

<video autoplay muted loop preload="none" poster="{{ asset('img/web/intro_video_placeholder.webp') }}" loading="lazy">
 <source src="{{ asset('img/web/video/intro_video.webm') }}" type="video/webm">
</video>

It works fine, but what i wanna achieve is lazy load. I wanna load css nad js first and then start with video loading and also, if video is loaded, i wanna play it automatically.

With code above is not working properly and its blocking my css and js on slow internet connection (video has around 9MB).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there any solution? Thanks a lot.

>Solution :

your code looks fine. But a small change. The preload attribute is ignored if autoplay is present.

document.addEventListener("DOMContentLoaded", function() {
    var lazyVideo = document.getElementById('lazyVideo');
    // Function to play the video 

  });
<video id="lazyVideo" muted loop preload="none" poster="{{ asset('img/web/intro_video_placeholder.webp') }}" loading="lazy">
 <source src="{{ asset('img/web/video/intro_video.webm') }}" type="video/webm">
</video>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading