How to stop a Vimeo video with jQuery

后端 未结 7 1273
情话喂你
情话喂你 2020-12-13 13:05

When I hide a YouTube video, it stops playing. However, this is not the case for Vimeo videos. Is there another way to stop a Vimeo video?

相关标签:
7条回答
  • 2020-12-13 13:23

    Using the Vimeo JavaScript API, it can be done with:

    player.unload()
    

    https://github.com/vimeo/player.js#unload-promisevoid-error

    0 讨论(0)
  • 2020-12-13 13:24

    First, add an ID to your iFrame. Then add this to your javascript close window click function:

    var $frame = $('iframe#yourIframeId');
    
    // saves the current iframe source
    var vidsrc = $frame.attr('src');
    
    // sets the source to nothing, stopping the video
    $frame.attr('src',''); 
    
    // sets it back to the correct link so that it reloads immediately on the next window open
    $frame.attr('src', vidsrc);
    
    0 讨论(0)
  • 2020-12-13 13:24

    I recently needed to pause a Vimeo video that was inside a Bootstrap modal when the modal was closed.

    The Vimeo video was embedded in an iframe.

    This is what worked for me:

    $("#my-bootstrap-modal").on('hidden.bs.modal', function (e) {
        var div = document.getElementById("my-bootstrap-modal");
        var iframe = div.getElementsByTagName("iframe")[0].contentWindow;
        iframe.postMessage('{"method":"pause"}', '*');
    });
    
    0 讨论(0)
  • 2020-12-13 13:29

    To restore the SRC attribute, use the following before clearing:

    var source = $('iframe#yourVideoId').attr('src');
    

    Next, SRC attribute clear:

    $('iframe#yourVideoId').attr('src', '');
    

    Callback previous SRC attribute:

    $('iframe#yourVideoId').attr('src', source);
    
    0 讨论(0)
  • 2020-12-13 13:39
        var vidUrl = $("iframe#video-frame").attr('src');
    
    
        //Basically stops and starts the video on modal open/close
        $('#video').on('hidden.bs.modal', function (e) {
            $("iframe#video-frame").attr('src','');
        });
    
        $('#video').on('show.bs.modal', function (e) {
            $("iframe#video-frame").attr('src', vidUrl);
        })
    
    0 讨论(0)
  • 2020-12-13 13:41

    Another answer along the lines of David's...you can use jQuery to clear the SRC attribute of the iFrame.

    $('iframe#targetID').attr('src','');
    

    I'm using this with a Vimeo video and a lightbox effect. When the lightbox is triggered again, I add the video URL back to the iFrame SRC before showing it.

    0 讨论(0)
提交回复
热议问题