Skip to content

Commit 94d128f

Browse files
committed
adding readme
1 parent 50dcd6f commit 94d128f

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
11
# lazy-load-bg-video
2+
23
Lazy loading background videos in Webflow with vanilla-lazyload
4+
5+
Clone an example site from Made in Webflow:
6+
7+
https://webflow.com/made-in-webflow/website/background-vid-test
8+
9+
## vanilla-lazyload
10+
11+
[vanilla-lazyload](https://github.com/verlok/vanilla-lazyload) is a super lightweight JavaScript library to help lazy load videos using the Intersection Observer API (which isn't compatible with [older browsers](https://caniuse.com/?search=intersectionObserver)).
12+
13+
If you need to support older browsers, you can look into [yall.js](https://github.com/malchata/yall.js) (Yet Another Lazy Loader).
14+
15+
For a good overview on why you may want to lazy load videos and the strategy do so, please review:
16+
17+
https://web.dev/lazy-loading-video/
18+
19+
The tldr; lazy loading videos will help with site performance for background video elements below the fold, especially on mobile devices.
20+
21+
## How it's done
22+
23+
### The HTML
24+
25+
First, you're going to want [a page in your style guide](https://webflow.com/blog/how-to-build-a-living-style-guide-in-webflow) dedicated to components where the native Webflow element can permanently live.
26+
27+
Add in your background video element here and style it however you like. Once complete, publish the page and then open the live site where the element lives.
28+
29+
Right click to inspect it and once you have it selected in dev tools. Right click and copy the element.
30+
31+
Now we need to modify the code a little bit so paste this into an IDE, a Webflow embed block, or a plain text editor.
32+
33+
On the video element we'll add a `class="lazy` and `preload="none"`. Then on the sources inside it, we'll change the `src` of the video to `data-src` and add `type="video/mp4"`. Of course you'll want the type to match, so set `webm` for WebM videos or `ogg` for OGG files.
34+
35+
Once you have that copy the code and go to the page in Webflow where you want the background video element to live and add a `Embed` block to your canvas. Now paste in the code.
36+
37+
### The JavaScript
38+
39+
Now you'll want to download the library and upload it somewhere and include it in the `before` body section of your page. You can [get a CDN link from the GitHub repo](https://github.com/verlok/vanilla-lazyload#-getting-started---script).
40+
41+
When adding to your project, you can use async:
42+
43+
```html
44+
<script
45+
async
46+
src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.3/dist/lazyload.min.js"
47+
></script>
48+
```
49+
50+
Above that, you'll want to add a script and configure vanilla-lazyload to your liking, but in our example we used:
51+
52+
```js
53+
window.lazyLoadOptions = {
54+
elements_selector: ".lazy",
55+
threshold: 1,
56+
};
57+
```
58+
59+
Here's a link to see the full list of options:
60+
61+
https://github.com/verlok/vanilla-lazyload#options
62+
63+
Now, publish your page and watch your site performance take off!! 🚀

modified-webflow-bg-element.html

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<div
2+
data-poster-url="https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-poster-00001.jpg"
3+
data-video-urls="https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-transcode.mp4,https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-transcode.webm"
4+
data-autoplay="true"
5+
data-loop="true"
6+
data-wf-ignore="true"
7+
class="w-background-video w-background-video-atom"
8+
>
9+
<video
10+
id="d9361516-0c98-5382-5de8-647eb32a65d0-video"
11+
autoplay=""
12+
loop=""
13+
style="
14+
background-image: url('https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-poster-00001.jpg');
15+
"
16+
muted=""
17+
playsinline=""
18+
data-wf-ignore="true"
19+
data-object-fit="cover"
20+
>
21+
<source
22+
src="https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-transcode.mp4"
23+
data-wf-ignore="true"
24+
/>
25+
<source
26+
src="https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-transcode.webm"
27+
data-wf-ignore="true"
28+
/></video
29+
><noscript
30+
><style>
31+
[data-wf-bgvideo-fallback-img] {
32+
display: none;
33+
}
34+
@media (prefers-reduced-motion: reduce) {
35+
[data-wf-bgvideo-fallback-img] {
36+
position: absolute;
37+
z-index: -100;
38+
display: inline-block;
39+
height: 100%;
40+
width: 100%;
41+
object-fit: cover;
42+
}
43+
}</style
44+
><img
45+
data-wf-bgvideo-fallback-img="true"
46+
src="https://assets.website-files.com/62ea9b78700b6385dd63aa68/62ea9cec925c8b4b0d0f72c3_coverr--08-20-surfing-waves-cloudy-06-5087-poster-00001.jpg"
47+
alt=""
48+
/></noscript>
49+
<div aria-live="polite">
50+
<button
51+
aria-controls="d9361516-0c98-5382-5de8-647eb32a65d0-video"
52+
type="button"
53+
data-w-bg-video-control="true"
54+
class="w-backgroundvideo-backgroundvideoplaypausebutton w-background-video--control"
55+
>
56+
<span
57+
><img
58+
src="https://assets.website-files.com/6022af993a6b2191db3ed10c/628299f8aa233b83918e24fd_Pause.svg"
59+
alt="Pause video" /></span
60+
><span hidden=""
61+
><img
62+
src="https://assets.website-files.com/6022af993a6b2191db3ed10c/628298b20ae0236682d4b87f_Play-24.svg"
63+
alt="Play video"
64+
/></span>
65+
</button>
66+
</div>
67+
</div>

0 commit comments

Comments
 (0)