怎樣提高網(wǎng)頁中插入Youtube視頻的性能?
}) ?
Iframe. allowFullscreen = true ?
</Iframe>
<Script>
???? ??? ??? ?????? ?? ???? ????? iframe ??? ????? ?????? ?????? JavaScript.
<Div id = "youtube-placeholder"></div>
Var firstScriptTag = document.getElementsByTagName('script')[0] ?
?
Allowfullscreen>
Iframe. allowFullscreen = true ?
Observer. observe(document.getElementById('youtube-placeholder') ?
<Iframe
3. ??????? Intersection Observer API
Iframe. src = 'https:// www.youtube.com/embed/VIDEO_ID';
</Script>
}) ?
}
}) ?
?
???? ????? ????? ????? ??????? Intersection Observer ??? ???? iframe ??? ???? ????? ? ??? ???? ?? ????? ??????.
Iframe. allowFullscreen = true ?
Observer. observe(document.getElementById('youtube-placeholder') ?
Tag. src = "https://www.youtube.com/iframe_api" ?
Loading = "lazy"
???? YouTube ????? ????? ??????? JavaScript ???? ???? ?????? ?? ??????? ?? ?????? ?? ????? ??????? ???????.
???? ????? "iframe" ?????? ?? HTML ????? "loading =" lazy "? ????? ???? ?? ???? ????? ????? iframe ??? ?????? ???????? ??? ??????? ????????.
Window. addEventListener('load', function() {
Function onYouTubeIframeAPIReady() {
Var observer = IntersectionObserver ?????? (function(entries) {
Entries. forEach(function(entry) {
Var player;
Src = "https://www.youtube.com/embed/VIDEO_ID"
2. ?? ?????? iframe ?????????? ???????? JavaScript
Var iframe = document.createElement('iframe') ?
1. ??????? ????? 'loading = "lazy"
}
}) ?
?
Observer.unobserve (entry.tar get) ?
<Div id = "youtube-player"></div>
Observer. observe(document.getElementById('youtube-placeholder') ?
Height: "360" ?
FirstScriptTag. parentNode.insertBefore(tag, firstScriptTag) ?
??? (entry.isIntersecting) {
</Div>
}
}
5. ??????? ????? ??????? ?????? ???????
'OnReady': onPlayerReady
Var iframe = document.createElement('iframe') ?
????? ??? ???? ????? ????? ? ?? ????? iframe ??? ?? ???? ????????.
VideoId: "VIDEO _ ID" ?
?????: "640" ?
????? ??? ???? ????? ????? ? ?? ????? iframe ??? ?? ???? ????????.
}) ?
?
Function onPlayerReady(event) {
Events: {
?? ???? ???????? ???????? ????? ? ???? ????? ????? YouTube iframe ???? ???? ??? ???? ????? ?????? ?????? ????? ????????. ????? ??????? ???? ??????? ??? ?????????? ????????????? ???????.
<Img src = "placeholder.jpg" alt = "YouTube Video">
Observer. observe(document.getElementById('youtube-placeholder') ?
Function loadYoutubeVideo() {
3. ??????? Intersection Observer API
Iframe. src = 'https:// www.youtube.com/embed/VIDEO_ID';
</Script>
Document. getElementById('youtube-placeholder').innerHTML = "?
}) ?
}) ?
