单击 HTML5 视频播放的海报图像?

发布于 2024-10-21 19:52:01 字数 117 浏览 4 评论 0原文

我有一个带有海报属性的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明天过后 2024-10-28 19:52:01

这对我有用安德鲁。

在你的 html 头部添加这一小段 js:

var video = document.getElementById('video');
video.addEventListener('click',function(){
    video.play();
},false);

或者,直接在你的 html 元素中添加一个 onlick 属性:

<video src="your_video" width="250" height="50" poster="your_image" onclick="this.play();"/>

This is working for me Andrew.

In your html head add this small piece of js:

var video = document.getElementById('video');
video.addEventListener('click',function(){
    video.play();
},false);

Or, just add an onlick attribute directly to your html element:

<video src="your_video" width="250" height="50" poster="your_image" onclick="this.play();"/>
弄潮 2024-10-28 19:52:01

发布此此处< /a> 也是如此,但这也适用于该线程。

我在做这件事时遇到了无数问题,但最终想出了一个可行的解决方案。

基本上,下面的代码是向视频添加一个点击处理程序,但忽略下部的所有点击(0.82 是任意的,但似乎适用于所有尺寸)。

$("video").click(function(e){

    // handle click if not Firefox (Firefox supports this feature natively)
    if (typeof InstallTrigger === 'undefined') {

        // get click position 
        var clickY = (e.pageY - $(this).offset().top);
        var height = parseFloat( $(this).height() );

        // avoids interference with controls
        if (clickY > 0.82*height) return;

        // toggles play / pause
        this.paused ? this.play() : this.pause();
    }
});

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).

$("video").click(function(e){

    // handle click if not Firefox (Firefox supports this feature natively)
    if (typeof InstallTrigger === 'undefined') {

        // get click position 
        var clickY = (e.pageY - $(this).offset().top);
        var height = parseFloat( $(this).height() );

        // avoids interference with controls
        if (clickY > 0.82*height) return;

        // toggles play / pause
        this.paused ? this.play() : this.pause();
    }
});
薄凉少年不暖心 2024-10-28 19:52:01

使用海报属性不会改变行为。它只是在加载视频时显示该图像。让视频自动启动(如果浏览器实现不这样做),那么您必须执行以下操作:

在 javascript 中使用 jquery:

$('#video').click(function(){
   document.getElementById('video').play();
});

希望有帮助

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:

$('#video').click(function(){
   document.getElementById('video').play();
});

Hope it helps

写给空气的情书 2024-10-28 19:52:01

是的,这听起来像是浏览器创建者或 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/

幸福丶如此 2024-10-28 19:52:01

我是在 React 和 es6 中这样做的。这是我在阅读 Daniel Golden 的解决方案后制作的现代版本:

const Video = ({videoSources, poster}) => {
  return (
    <video
      controls
      poster={poster}
      onClick={({target: video}) => video.paused ? video.play() : video.pause()}
    >
      {_.map(videoSources, ({url, type}, i) =>
        <source key={i} src={url} type={type} />
      )}
    </video>
  )
}

I was doing this in react and es6. Here is a modern version I made after reading Daniel Golden's solution:

const Video = ({videoSources, poster}) => {
  return (
    <video
      controls
      poster={poster}
      onClick={({target: video}) => video.paused ? video.play() : video.pause()}
    >
      {_.map(videoSources, ({url, type}, i) =>
        <source key={i} src={url} type={type} />
      )}
    </video>
  )
}
活泼老夫 2024-10-28 19:52:01

使用 get(0) 也可以

    var play = $('.playbutton');
    var video = $('#video');
    play.click(function(){
        video.get(0).play();
    })

Using get(0) also works

    var play = $('.playbutton');
    var video = $('#video');
    play.click(function(){
        video.get(0).play();
    })
通知家属抬走 2024-10-28 19:52:01

您可以在不使用 jQuery 或任何东西的情况下完成此操作。只是普通的旧 JavaScript:

  1. 创建一个变量来存储视频元素。
  2. 检测事件(您可以使用您喜欢的任何按钮)。
  3. 我在示例中检测到鼠标左键。
  4. 最后一行将切换播放和暂停。
var video = document.getElementById('yourVideoId');
    document.onmousedown = function(e) {
    if (event.button == 0){
        video.paused ? video.play() : video.pause();
    }
}

You can do it without using jQuery or anything. Just plain old JavaScript:

  1. Create a variable to store the video element.
  2. Detect the event (you can use any button you like).
  3. I detected the left mouse button in my example.
  4. The final line will toggle play and pause.
var video = document.getElementById('yourVideoId');
    document.onmousedown = function(e) {
    if (event.button == 0){
        video.paused ? video.play() : video.pause();
    }
}
三人与歌 2024-10-28 19:52:01
<script type="text/javascript">
    function actualNextSibling(el) {    // needed to smooth out default firefox/IE behavior
        do { el = el.nextSibling } while (el && el.nodeType !== 1);
            return el;
    }
</script>
<div onclick="actualNextSibling(this).style.display='block'; this.style.display='none'">
    <img src="splash.jpg" alt="splash" style="cursor: pointer" />
</div>
<div style="display: none">
    <iframe width="440" height="248" src="//www.youtube.com/embed/9FOZEbEpyA8?rel=0&autoplay=1"frameborder="0" allowfullscreen></iframe>
</div>
<script type="text/javascript">
    function actualNextSibling(el) {    // needed to smooth out default firefox/IE behavior
        do { el = el.nextSibling } while (el && el.nodeType !== 1);
            return el;
    }
</script>
<div onclick="actualNextSibling(this).style.display='block'; this.style.display='none'">
    <img src="splash.jpg" alt="splash" style="cursor: pointer" />
</div>
<div style="display: none">
    <iframe width="440" height="248" src="//www.youtube.com/embed/9FOZEbEpyA8?rel=0&autoplay=1"frameborder="0" allowfullscreen></iframe>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文