Skip to content

Commit 8392bac

Browse files
committed
video optimz
1 parent e265c0a commit 8392bac

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

public/css/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
display: block;
123123
object-fit: cover;
124124
background-color: #0f2c06;
125+
transform: translateZ(0);
126+
backface-visibility: hidden;
125127
}
126128

127129
#heading-container .container-left p {

public/js/script.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,53 @@ document.querySelectorAll('a[href^="#"]').forEach(anchor => {
4444

4545
document.addEventListener("DOMContentLoaded", function() {
4646
const heroVideo = document.querySelector("#heading-container .hero-video");
47+
const heroSection = document.querySelector("#heading-container");
48+
const isMobileViewport = window.matchMedia("(max-width: 900px)").matches;
49+
50+
function safePlayVideo(videoEl) {
51+
if (!videoEl) {
52+
return;
53+
}
54+
const playPromise = videoEl.play();
55+
if (playPromise && typeof playPromise.catch === "function") {
56+
playPromise.catch(function() {
57+
// Ignore autoplay interruptions from browser policies.
58+
});
59+
}
60+
}
61+
4762
if (heroVideo) {
4863
heroVideo.addEventListener("loadedmetadata", function() {
4964
heroVideo.currentTime = 5.5;
5065
}, { once: true });
5166
}
5267

68+
if (heroVideo && heroSection && isMobileViewport && "IntersectionObserver" in window) {
69+
let heroIsVisible = true;
70+
const observer = new IntersectionObserver(function(entries) {
71+
entries.forEach(function(entry) {
72+
heroIsVisible = entry.isIntersecting && entry.intersectionRatio >= 0.18;
73+
});
74+
if (heroIsVisible) {
75+
safePlayVideo(heroVideo);
76+
} else {
77+
heroVideo.pause();
78+
}
79+
}, {
80+
threshold: [0, 0.18, 0.4]
81+
});
82+
83+
observer.observe(heroSection);
84+
85+
document.addEventListener("visibilitychange", function() {
86+
if (document.hidden) {
87+
heroVideo.pause();
88+
} else if (heroIsVisible) {
89+
safePlayVideo(heroVideo);
90+
}
91+
});
92+
}
93+
5394
const heroIntroTargets = document.querySelectorAll([
5495
"#heading-container .container-left img",
5596
"#heading-container .container-left p",

themes/Begrip/static/css/index.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
display: block;
123123
object-fit: cover;
124124
background-color: #0f2c06;
125+
transform: translateZ(0);
126+
backface-visibility: hidden;
125127
}
126128

127129
#heading-container .container-left p {

themes/Begrip/static/js/script.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,53 @@ document.querySelectorAll('a[href^="#"]').forEach(anchor => {
4444

4545
document.addEventListener("DOMContentLoaded", function() {
4646
const heroVideo = document.querySelector("#heading-container .hero-video");
47+
const heroSection = document.querySelector("#heading-container");
48+
const isMobileViewport = window.matchMedia("(max-width: 900px)").matches;
49+
50+
function safePlayVideo(videoEl) {
51+
if (!videoEl) {
52+
return;
53+
}
54+
const playPromise = videoEl.play();
55+
if (playPromise && typeof playPromise.catch === "function") {
56+
playPromise.catch(function() {
57+
// Ignore autoplay interruptions from browser policies.
58+
});
59+
}
60+
}
61+
4762
if (heroVideo) {
4863
heroVideo.addEventListener("loadedmetadata", function() {
4964
heroVideo.currentTime = 5.5;
5065
}, { once: true });
5166
}
5267

68+
if (heroVideo && heroSection && isMobileViewport && "IntersectionObserver" in window) {
69+
let heroIsVisible = true;
70+
const observer = new IntersectionObserver(function(entries) {
71+
entries.forEach(function(entry) {
72+
heroIsVisible = entry.isIntersecting && entry.intersectionRatio >= 0.18;
73+
});
74+
if (heroIsVisible) {
75+
safePlayVideo(heroVideo);
76+
} else {
77+
heroVideo.pause();
78+
}
79+
}, {
80+
threshold: [0, 0.18, 0.4]
81+
});
82+
83+
observer.observe(heroSection);
84+
85+
document.addEventListener("visibilitychange", function() {
86+
if (document.hidden) {
87+
heroVideo.pause();
88+
} else if (heroIsVisible) {
89+
safePlayVideo(heroVideo);
90+
}
91+
});
92+
}
93+
5394
const heroIntroTargets = document.querySelectorAll([
5495
"#heading-container .container-left img",
5596
"#heading-container .container-left p",

0 commit comments

Comments
 (0)