-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.html
69 lines (63 loc) · 2.24 KB
/
video.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>懒加载视频示例</title>
<style>
.video-container {
max-width: 800px;
margin: 0 auto;
}
.video-placeholder {
width: 100%;
height: auto;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.video-placeholder img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="video-container">
<div class="video-placeholder" data-video-src="https://example.com/video.mp4">
<img src="https://placehold.co/600x400" alt="点击加载视频">
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const placeholder = document.querySelector('.video-placeholder');
const videoSrc = placeholder.getAttribute('data-video-src');
placeholder.addEventListener('click', function() {
const video = document.createElement('video');
video.src = videoSrc;
video.controls = true;
video.style.width = '100%';
placeholder.parentNode.replaceChild(video, placeholder);
video.play();
});
// 使用 Intersection Observer API 进行视口检测
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// 预加载视频
const link = document.createElement('link');
link.rel = 'preload';
link.as = 'video';
link.href = videoSrc;
document.head.appendChild(link);
observer.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
observer.observe(placeholder);
});
</script>
</body>
</html>