【问题标题】:Embedding youtube video嵌入 youtube 视频
【发布时间】:2013-05-05 11:58:07
【问题描述】:

我像这样在我的网站中嵌入 youtube 视频:

<iframe width="320" height="180" src="http://www.youtube.com/embed/12345678"
    style="border: none" allowfullscreen id="myVideo"></iframe>

这个 iframe 的 href 下载一个大小为 2.7 KB 的 this

下载这些:

因此,即使用户还没有观看视频,他也下载了近 356 KB 的数据。在我的网站上显示 Youtube 视频但在用户想要观看视频时加载这些数据的更好方法是什么?

【问题讨论】:

  • 只要不使用 iframe 过度填充您的页面,我会说 356 KB 的数据并不是真正值得担心的事情。
  • 是的,但这会降低页面的移动性能..
  • 这就是您为嵌入 YouTube 视频所付出的代价。您可以尝试使用重定向到实际视频的缩略图。

标签: html performance youtube-api pagespeed


【解决方案1】:

您可以使用缩略图并仅在用户单击缩略图时加载播放器

您可以在主题资源管理器示例中查看如何使用 AngularJS 执行此操作的示例

应用:

https://yt-topic-explorer.googlecode.com/git/dist/index.html

查看:

https://code.google.com/p/yt-topic-explorer/source/browse/app/views/main.html

代码sn-p:

<div class="player-container">
        <img ng-click="videoClicked($event.target, videoResult.id)" ng-src="{{videoResult.thumbnailUrl}}" class="thumbnail-image">
        <div class="title"><a ng-href="{{videoResult.href}}" target="_blank">{{videoResult.title}}</a></div>
</div>

控制器:

https://code.google.com/p/yt-topic-explorer/source/browse/app/scripts/controllers/main.js

代码sn-p:

function playVideo(container, videoId) {
    var width = container.offsetWidth;
    var height = container.offsetHeight;

    new YT.Player(container, {
      videoId: videoId,
      width: width,
      height: height,
      playerVars: {
        autoplay: 1,
        controls: 2,
        modestbranding: 1,
        rel: 0,
        showInfo: 0
      }
    });

【讨论】:

  • 谢谢。我实现了它。
猜你喜欢
  • 2021-12-04
  • 2012-06-25
  • 2020-08-25
  • 2015-06-03
  • 2014-07-07
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多