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

我的页面上有几个小视频。如果有人点击视频,它应该在页面中心播放更大尺寸的视频。我不知道要搜索什么或如何做到这一点!

有人可以给我一个建议吗?

谢谢!


aluckdog
浏览 71回答 2
2回答

斯蒂芬大帝

您可以使用 Youtube API 在有人点击缩略图时动态加载视频<html><head>&nbsp; <style>&nbsp; &nbsp; .container {&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; flex-direction: column;&nbsp; &nbsp; }&nbsp; &nbsp; img {&nbsp; &nbsp; &nbsp; width: 250px;&nbsp; &nbsp; &nbsp; height: 250px;&nbsp; &nbsp; }&nbsp; </style></head>&nbsp; <body>&nbsp; &nbsp; <div class="container"><span>Thumbnail (click me)</span>&nbsp; &nbsp; &nbsp; <div id="thumbs">&nbsp; &nbsp; &nbsp; <img class="videoThumb" src="http://i3.ytimg.com/vi/WL96WqA6e9Y/maxresdefault.jpg" data-video="WL96WqA6e9Y">&nbsp; &nbsp; &nbsp; <img class="videoThumb" src="http://i3.ytimg.com/vi/ZQy89tZ-mRU/hqdefault.jpg" data-video="ZQy89tZ-mRU">&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; <span>Video</span>&nbsp; &nbsp; &nbsp; <div id="videoContainer"></div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; // Load the Youtube IFrame Player API code asynchronously.&nbsp; &nbsp; var tag = document.createElement('script');&nbsp; &nbsp; tag.src = "https://www.youtube.com/player_api";&nbsp; &nbsp; var firstScriptTag = document.getElementsByTagName('script')[0];&nbsp; &nbsp; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);&nbsp; &nbsp; function enlarge(event) {&nbsp; &nbsp; &nbsp; &nbsp; //Get the video ID from the thumbnail&nbsp; &nbsp; &nbsp; &nbsp; var videoID = event.target.attributes["data-video"].value;&nbsp; &nbsp; &nbsp; &nbsp; //Get the video container&nbsp; &nbsp; &nbsp; &nbsp; var container = document.getElementById("videoContainer");&nbsp; &nbsp; &nbsp; &nbsp; //Clear it as youtube overwrites the element&nbsp; &nbsp; &nbsp; &nbsp; container.innerHTML = "";&nbsp; &nbsp; &nbsp; &nbsp; //Create element which will be replaced with video&nbsp; &nbsp; &nbsp; &nbsp; var videoFrame = document.createElement('div');&nbsp; &nbsp; &nbsp; &nbsp; //append to the container&nbsp; &nbsp; &nbsp; &nbsp; container.appendChild(videoFrame);&nbsp; &nbsp; &nbsp; &nbsp; //create youtube video&nbsp; &nbsp; &nbsp; &nbsp; videoFrame.id = 'videoFrame';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new YT.Player('videoFrame', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: '360',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: '640',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; videoId: videoID&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; //Get all thumbnails&nbsp; &nbsp; var vids = document.getElementsByClassName("videoThumb");&nbsp; &nbsp; //Add event listener to all thumbnails&nbsp; &nbsp; for(var i = 0; i < vids.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; vids[i].addEventListener("click",enlarge);&nbsp; &nbsp; }&nbsp; &nbsp; </script></html>

胡说叔叔

我会使用.Bootstrap使它非常容易实现(如果你以前从未使用过它)。modal以下是有关如何使用它的示例(只需添加自己的样式),以下是文档:CSS文档<head>&nbsp; <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"></head><body>&nbsp; <!-- Button trigger modal -->&nbsp; <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">&nbsp; &nbsp; Youtube video&nbsp; </button>&nbsp; <!-- Modal -->&nbsp; <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">&nbsp; &nbsp; <div class="modal-dialog modal-dialog-centered">&nbsp; &nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal" aria-label="Close">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span aria-hidden="true">&times;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-body text-center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp;&nbsp;&nbsp; <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>&nbsp; <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>&nbsp; <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript