简体   繁体   English

动态加载和自动播放HTML5视频

[英]Dynamically loading and autoplaying HTML5 video

I am trying to grab a video URL from data-src and add it to src for my video. 我正在尝试从data-src抓取视频URL,并将其添加到我的视频的src中。 I have that much working when I snoop the page code but the video isn't showing up on the page and playing. 监听页面代码时,我需要做很多工作,但是视频没有显示在页面上并且无法播放。 I'm using jQuery since I have a few other bits of code using it to run some functionality. 我使用jQuery是因为我还有其他一些代码可用于运行某些功能。

Markup: 标记:

<video autoplay="autoplay" loop="loop" poster="/assets/video/bg-ss-vid.jpg">
    <source type="video/mp4" data-src="/assets/video/bg-ss-vid.mp4">
</video>

jQuery: jQuery的:

jQuery(document).ready(function ($) {
    "use strict";

    $("#site-hero video").each(function () {
        $('source', $(this)).attr("src", $('source', $(this)).data('src'));
        $('source', $(this)).removeAttr('data-src');
    });
});

How do I get it to show up / play the video after the src has been loaded? 加载src后如何显示/播放视频?

Use .play() after adding it's src . 添加src 使用.play()

 jQuery(document).ready(function($) { "use strict"; $("video").each(function() { var $source = $(this).find('source'); var src = $source.attr('data-src'); $source.removeAttr('data-src'); $source.attr('src', src); $source.detach().appendTo($(this)) $(this).get(0).play(); }); }); 
 video { width: 420px; height: 230px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <video loop="loop" poster="http://placehold.it/420x230"> <source type="video/mp4" data-src="http://techslides.com/demos/sample-videos/small.mp4"> </video> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM