【问题标题】:Video kept playing even after closing modal in laravel即使在 laravel 中关闭模态后,视频也会继续播放
【发布时间】:2021-06-07 07:33:59
【问题描述】:

我正在尝试制作一个流程,其中一旦点击视频,视频将在模态中播放,并且在模态关闭时视频将暂停。但是即使我关闭模态,视频也会继续播放。我的代码是:

<div class="modal fade aspire-video-modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" style="padding-top: 37px;">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
                <!-- 16:9 aspect ratio -->
                <div class="embed-responsive embed-responsive-16by9">
                    <iframe class="embed-responsive-item" src="" id="video"  
                    allowscriptaccess="always" allow="autoplay"></iframe>
                </div>
            </div>
            <div class="modal-footer col-1 align-self-center">
                <button type="button" class="btn btn-secondary aspire-video-close" data- 
                dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

脚本代码是:

$(document).ready(function(){
    $(document).on('click', '.video-btn', function(){
        event.preventDefault();

        let id = $(this).data('id');

        console.log(id);
        $.ajax({
            url: "/watchvideo/"+id,
            dataType:"json",
            success:function(getVideo){
                var d = getVideo.video_link;
                console.log(d);
                $('#video').html(getVideo.video_link);
                // $("#video").attr('src',$getVideo.video_link);
                $("#video").attr("src", d+"? 
                autoplay=1&amp;modestbranding=1&amp;showinfo=0");

                $('#myModal').modal('show');
            }
        })
    });

    $("#myModal").on('hidden.bs.modal', function (e) {
        $("#video").pause();
    });
});

【问题讨论】:

  • 这是一个 vimeo 视频吗??
  • @KenLee 我试过你的,还是不行
  • @zahidhasanemon 它在 iframe 中播放(YouTube 视频)
  • 我建议您使用以下 javascript 加载空白页面来停止视频:$('#video').html('blank.html');(但请确保您有一个空白.html 文件)

标签: javascript php jquery laravel


【解决方案1】:

您似乎正在使用 Bootstrap。这是我在我的网站上工作的 YouTube 模式,它可能会对你有所帮助。尝试实现它。

模态 HTML

<div id="myModal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content" style="background-color: transparent; border:none;">
            <div class="modal-header p-0 m-0" style="background-color: transparent!important;border:none!important;">
                <button type="button" class="close pr-3" data-dismiss="modal" style="color:white;">&times;</button>
            </div>
            <div class="modal-body p-0" style="background-color: transparent;">
                <div class="embed-responsive embed-responsive-16by9" style="border-radius: 10px;width:100%;max-width:900px;">
                    <iframe id="dash-video"
                    src="https://www.youtube.com/embed/QDVLMegSnBQ" 
                    title="YouTube video player" frameborder="0" 
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
                    </iframe>
                </div>
            </div>
        </div>
    </div>
</div>

脚本

 $(document).ready(function(){
            
            // -- script to stop the video from playing when closing modal ---
            var url = $("#dash-video").attr('src');
            
            $("#myModal").on('hide.bs.modal', function(){
                $("#dash-video").attr('src', '');
            });
            
            $("#myModal").on('show.bs.modal', function(){
                $("#dash-video").attr('src', url);
            });
 });

注意1:将此代码置于&lt;/body&gt;标签之前的末尾。

注意 2:也许可以在我的视频中试一试,然后进行必要的调整以显示您的视频。

【讨论】:

  • 我可以在 ajax 中使用它吗?
  • 你为什么使用ajax?你能解释一下你在做什么吗?
  • 我没有从组件中获取模态(onclick)中的 youtube 视频 url,这就是为什么我使用另一个 ajax 调用来获取该特定组件的视频
  • 我认为你把事情复杂化了。我会将我的 YouTube 模态上传到答案。如果它对我有用,它应该对你有用。
  • 好的,感谢您的帮助和时间(提前)
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多