为了账号安全,请及时绑定邮箱和手机立即绑定

在内容上方的窗口中播放优酷帧

在内容上方的窗口中播放优酷帧

aluckdog 2022-09-16 21:02:34
我的页面上有几个小视频。如果有人点击视频,它应该在页面中心播放更大尺寸的视频。我不知道要搜索什么或如何做到这一点!有人可以给我一个建议吗?谢谢!
查看完整描述

2 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

您可以使用 Youtube API 在有人点击缩略图时动态加载视频


<html>

<head>

  <style>

    .container {

      display: flex;

      flex-direction: column;

    }


    img {

      width: 250px;

      height: 250px;


    }

  </style>

</head>

  <body>

    <div class="container"><span>Thumbnail (click me)</span>

      <div id="thumbs">

      <img class="videoThumb" src="http://i3.ytimg.com/vi/WL96WqA6e9Y/maxresdefault.jpg" data-video="WL96WqA6e9Y">

      <img class="videoThumb" src="http://i3.ytimg.com/vi/ZQy89tZ-mRU/hqdefault.jpg" data-video="ZQy89tZ-mRU">

      </div>

    </div>

    <div class="container">

      <span>Video</span>

      <div id="videoContainer"></div>

    </div>

    <script>

      // Load the Youtube IFrame Player API code asynchronously.

    var tag = document.createElement('script');

    tag.src = "https://www.youtube.com/player_api";

    var firstScriptTag = document.getElementsByTagName('script')[0];

    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);


    function enlarge(event) {

        //Get the video ID from the thumbnail

        var videoID = event.target.attributes["data-video"].value;

        //Get the video container

        var container = document.getElementById("videoContainer");

        //Clear it as youtube overwrites the element

        container.innerHTML = "";

        //Create element which will be replaced with video

        var videoFrame = document.createElement('div');

        //append to the container

        container.appendChild(videoFrame);

        //create youtube video

        videoFrame.id = 'videoFrame';

            new YT.Player('videoFrame', {

                height: '360',

                width: '640',

                videoId: videoID

        });

    }

    //Get all thumbnails

    var vids = document.getElementsByClassName("videoThumb");

    //Add event listener to all thumbnails

    for(var i = 0; i < vids.length; i++) {

        vids[i].addEventListener("click",enlarge);

    }

    </script>

</html>


查看完整回答
反对 回复 2022-09-16
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

我会使用.Bootstrap使它非常容易实现(如果你以前从未使用过它)。modal


以下是有关如何使用它的示例(只需添加自己的样式),以下是文档:CSS


文档


<head>

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

</head>

<body>

  <!-- Button trigger modal -->

  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">

    Youtube video

  </button>

  <!-- Modal -->

  <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">

    <div class="modal-dialog modal-dialog-centered">

      <div class="modal-content">

        <div class="modal-header">

          <h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>

          <button type="button" class="close" data-dismiss="modal" aria-label="Close">

            <span aria-hidden="true">&times;</span>

          </button>

        </div>

        <div class="modal-body text-center">

          <iframe style="height: 100%; width: auto;" src="https://www.youtube.com/embed/UgHKb_7884o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

        </div>

      </div>

    </div>

  </div>

  

  <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>

  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

</body>


查看完整回答
反对 回复 2022-09-16
  • 2 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信