单击 HTML5 视频播放的海报图像?
我有一个带有海报属性的 HTML5 视频。我想以某种方式设置它,以便您可以单击视频元素(海报图像的区域)上的任何位置,它将触发播放事件并启动视频?我觉得这是相当标准的做法,但我找不到没有闪光灯的方法。任何帮助将不胜感激。
I have an HTML5 video with a poster attribute. I would like to somehow set it up so that you can click on anywhere on the video element (the area of the poster image) and it will fire the play event and start the video? I feel like this is fairly standard practice, but I can not find a way to do this without flash. Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这对我有用安德鲁。
在你的 html 头部添加这一小段 js:
或者,直接在你的 html 元素中添加一个 onlick 属性:
This is working for me Andrew.
In your html head add this small piece of js:
Or, just add an onlick attribute directly to your html element:
发布此此处< /a> 也是如此,但这也适用于该线程。
我在做这件事时遇到了无数问题,但最终想出了一个可行的解决方案。
基本上,下面的代码是向视频添加一个点击处理程序,但忽略下部的所有点击(0.82 是任意的,但似乎适用于所有尺寸)。
Posted this here too but this applies to this thread too.
I had countless issues doing this but finally came up with a solution that works.
Basically the code below is adding a click handler to the video but ignoring all the clicks on the lower part (0.82 is arbitrary but seems to work on all sizes).
使用海报属性不会改变行为。它只是在加载视频时显示该图像。让视频自动启动(如果浏览器实现不这样做),那么您必须执行以下操作:
在 javascript 中使用 jquery:
希望有帮助
Using the poster attribute does not alter the behavior. It just shows that image while loading the video. Having the video to automatically start (if the browser implementation does not do that), then you have to do something like:
<video id="video" ...></video>
in javascript using jquery:
Hope it helps
是的,这听起来像是浏览器创建者或 html 规范编写者只是“忘记”的常规功能。
我已经编写了几个解决方案,但它们都不是真正的跨浏览器或 100% 可靠的。包括单击视频控件时出现的问题会导致视频停止的意外后果。相反,我建议您使用经过验证的解决方案,例如 VideoJS:http://videojs.com/
Yes, that sounds like a regular feature the browser creators or html spec writers just "forgot".
I've written a couple solutions but none of them are truly cross browser or 100% solid. Including problems with clicking on the video controls has the unintended consequence of stopping the video. Instead I would recommend you use a proven solution such as VideoJS: http://videojs.com/
我是在 React 和 es6 中这样做的。这是我在阅读 Daniel Golden 的解决方案后制作的现代版本:
I was doing this in react and es6. Here is a modern version I made after reading Daniel Golden's solution:
使用 get(0) 也可以
Using get(0) also works
您可以在不使用 jQuery 或任何东西的情况下完成此操作。只是普通的旧 JavaScript:
You can do it without using
jQuery
or anything. Just plain old JavaScript: