Javascript 在灯箱或页面加载时打开 YouTube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3266836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:57:33  来源:igfitidea点击:

Opening a YouTube video in a lightbox or something on page load

javascriptjqueryvideolightboxshadowbox

提问by talkingD0G

My client wants a YouTube video (embeded object) to popup when a user comes to their site.

我的客户希望在用户访问他们的网站时弹出一个 YouTube 视频(嵌入对象)。

Does anyone know how it do this and what to use. It should be a lightbox or shadowbox, something that's not a popup.

有谁知道它是如何做到这一点以及使用什么的。它应该是一个灯箱或阴影箱,而不是弹出窗口。

The video should run automatically and ideally close when it's over. Maybe I can specify a time until auto-close or something?

视频应该自动运行,最好在结束时关闭。也许我可以指定自动关闭之前的时间或什么?

Please help. Thanks all!

请帮忙。谢谢大家!

回答by gearsdigital

This is a full working solution. Just copy this html. Don't forget to change the path to the js and css.

这是一个完整的工作解决方案。只需复制此html。不要忘记更改js和css的路径。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="./fancybox/jquery.fancybox-1.3.1.js"></script>
    <link rel="stylesheet" type="text/css" href="./fancybox/jquery.fancybox-1.3.1.css" media="screen" />

    <script type="text/javascript">
        $(document).ready(function() {

            $("#yt").click(function() {
                $.fancybox({
                        'padding'        : 0,
                        'autoScale'      : false,
                        'transitionIn'   : 'none',
                        'transitionOut'  : 'none',
                        'title'          : this.title,
                        'width'          : 680,
                        'height'         : 495,
                        'href'           : this.href.replace(new RegExp("watch\?v=", "i"), 'v/'),
                        'type'           : 'swf',
                        'swf'            : {
                            'wmode'              : 'transparent',
                            'allowfullscreen'    : 'true'
                        }
                    });
                return false;
            });
        });
        $('#foo').bind('click', function() {
              alert($(this).text());
            });
            $('#foo').trigger('click');
    </script>
</head>

<body onload='$("#yt").trigger("click");'>

        <h1>fancybox example</h1>
        <p><a id="yt" title="" href="http://www.youtube.com/watch?v=2y8G7wMgIPY&amp;fs=1&amp;autoplay=1"></a></p>
</body>
</html>

回答by Kapil

May you use a fancybox ?

你可以用fancybox吗?

Here is a link for it - http://fancybox.net/api

这是它的链接 - http://fancybox.net/api

回答by gearsdigital

<script type="text/javascript">
    $(document).ready(function() {
        $("#trigger").fancybox().trigger('click');
    });
</script>

Put your link #trigger somewhere in your document and hide it using css.

将链接 #trigger 放在文档中的某个位置并使用 css 隐藏它。

EDIT

编辑

<!DOCTYPE html>
<html>
<head>
    <!-- Css -->
    <link href="/path/to/fancybox.css" rel="stylesheet" type="text/css" />

    <!-- Javascript -->
    <script src="/path/to/jquery.js" type="text/javascript"></script>
    <script src="/path/to/fancybox.js" type="text/javascript"></script>

    <script type="text/javascript">
           $(document).ready(function() {
               $("#trigger").fancybox().trigger('click');
           });
    </script>        
</head>
<body>

    <!--hide trigger using display:none -->
    <p style="display:none"><a href="path/to/youtube" id="trigger"></a></p>

</body>
</html>

Here is the howto: http://fancybox.net/howto

这是操作方法:http: //fancybox.net/howto

回答by JJ_Coder4Hire

You can use the boom-lightbox for free. it is pure javascript, super light weight and doesn't require jquery. show responsive content, youtube videos, etc.

您可以免费使用吊杆灯箱。它是纯 javascript,超轻量级,不需要 jquery。显示响应式内容、YouTube 视频等。

https://github.com/boominteractive/boom-lightbox

https://github.com/boominteractive/boom-lightbox