Click the poster image the HTML5 video plays?

前端 未结 7 1730
死守一世寂寞
死守一世寂寞 2020-12-01 00:35

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

相关标签:
7条回答
  • 2020-12-01 01:40

    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();
        }
    });
    
    0 讨论(0)
提交回复
热议问题