html5 video button that takes video to specific time

前端 未结 2 493
名媛妹妹
名媛妹妹 2020-12-30 06:47

I\'m trying to place a video that autoplays without controls but I want to add three custom buttons below the video, each button jumps the video to a specific time. Not usin

相关标签:
2条回答
  • 2020-12-30 07:14

    You can set the currentTime attribute of a video. Using your example:

    var vidLinks = document.querySelectorAll('.navBtns a');
    
    for(var i = 0, l = vidLinks.length; ++i){
        makeVideoLink(vidLinks[i]);
    }
    
    function jumpToTime(time){
        v.currentTime = time;
    }
    
    function makeVideoLink(element){
        // Extract the `t=` hash from the link
        var timestamp = element.hash.match(/\d+$/,'')[0] * 1000;
    
        element.addEventListener('click', function videoLinkClick(e){
            jumpToTime(timestamp);
    
            return false;
        },false)
    }
    

    Mozilla's Developer Network site has a great list of HTML5 media element properties.

    0 讨论(0)
  • 2020-12-30 07:28

    Your links feature two different problems:

    1. they have the target="_blank" attribute, therefor they open a new window/tab. (get rid of those)
    2. you're linking to the video itself, not to the page with the video embedded. This just won't work as expected.

    To control a video on a page directly without leaving the page, you'll need some javascript. Here you'll find an example how this will work.

    JS Fiddle: Video CurrentTime Example

    You'll need a function to jump to the specific time on click of one of your links, that would be (for instance):

    var firstlink = document.getElementByTagName('a')[0];
    firstlink.addEventListener("click", function (event) {
        event.preventDefault();
        myvideo.currentTime = 7;
        myvideo.play();
    }, false);
    
    0 讨论(0)
提交回复
热议问题