如何延迟加载内嵌在iframe上的youtube视频?

时间:2020-01-06 08:31:43

标签: html iframe shopify lazy-loading

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

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

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

1 个答案:

答案 0 :(得分:0)

您可以在srcdoc标记内使用iframe属性来加载图像。请参阅以下示例作为参考:

<iframe
  width="560"
  height="315"
  src="https://www.youtube.com/embed/Y8Wp3dafaMQ"
  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>"
  frameborder="0"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
  title="The Dark Knight Rises: What Went Wrong? – Wisecrack Edition"
></iframe>

来源:

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

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