猿问
回到首页
个人中心
反馈问题
注册登录
下载APP
首页
课程
实战
体系课
手记
专栏
慕课教程
在内容上方的窗口中播放优酷帧
我的页面上有几个小视频。如果有人点击视频,它应该在页面中心播放更大尺寸的视频。我不知道要搜索什么或如何做到这一点!
有人可以给我一个建议吗?
谢谢!
aluckdog
浏览 71
回答 2
2回答
斯蒂芬大帝
您可以使用 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>
0
0
0
胡说叔叔
我会使用.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">×</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>
0
0
0
打开App,查看更多内容
随时随地看视频
慕课网APP
相关分类
JavaScript
继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续