如何在嵌套的 iframe 上实现 YouTube 视频的懒加载?

huangapple go评论119阅读模式
英文:

How to lazy load a youtube video embedded on an iframe?

问题

如何将延迟加载应用于嵌入视频的 iframe

  1. <iframe src="https://www.youtube.com/embed/teL-R6yimDY"
  2. allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  3. allowfullscreen=""
  4. width="560"
  5. height="315"
  6. frameborder="0"></iframe>

我尝试过添加 loading="eager"loading="auto"loading="lazyload"

英文:

How do I apply lazy loading to an iframe embedding a video?

  1. <iframe src="https://www.youtube.com/embed/teL-R6yimDY"
  2. allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  3. allowfullscreen=""
  4. width="560"
  5. height="315"
  6. frameborder="0"></iframe>

I have tried to add loading="eager", loading="auto" and loading="lazyload"

答案1

得分: 2

你可以在你的iframe标签内使用srcdoc属性来加载一张图片。请参考以下示例:

  1. <iframe
  2. width="560"
  3. height="315"
  4. src="https://www.youtube.com/embed/Y8Wp3dafaMQ"
  5. srcdoc="<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href=https://www.youtube.com/embed/Y8Wp3dafaMQ?autoplay=1><img src=https://img.youtube.com/vi/Y8Wp3dafaMQ/hqdefault.jpg alt='Video The Dark Knight Rises: What Went Wrong? – Wisecrack Edition'><span></span></a>"
  6. frameborder="0"
  7. allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  8. allowfullscreen
  9. title="The Dark Knight Rises: What Went Wrong? – Wisecrack Edition"
  10. ></iframe>

来源:

https://codepen.io/chriscoyier/pen/GRKZryx

https://css-tricks.com/lazy-load-embedded-youtube-videos/

英文:

You can use srcdoc attribute inside your iframe tag to load an image. Please see the following example as reference:

  1. &lt;iframe
  2. width=&quot;560&quot;
  3. height=&quot;315&quot;
  4. src=&quot;https://www.youtube.com/embed/Y8Wp3dafaMQ&quot;
  5. srcdoc=&quot;&lt;style&gt;*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}&lt;/style&gt;&lt;a href=https://www.youtube.com/embed/Y8Wp3dafaMQ?autoplay=1&gt;&lt;img src=https://img.youtube.com/vi/Y8Wp3dafaMQ/hqdefault.jpg alt=&#39;Video The Dark Knight Rises: What Went Wrong? – Wisecrack Edition&#39;&gt;&lt;span&gt;▶&lt;/span&gt;&lt;/a&gt;&quot;
  6. frameborder=&quot;0&quot;
  7. allow=&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture&quot;
  8. allowfullscreen
  9. title=&quot;The Dark Knight Rises: What Went Wrong? Wisecrack Edition&quot;
  10. &gt;&lt;/iframe&gt;

Source:

https://codepen.io/chriscoyier/pen/GRKZryx

https://css-tricks.com/lazy-load-embedded-youtube-videos/

huangapple
  • 本文由 发表于 2020年1月6日 16:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定