最近在做視頻播放的時候,發現用jquery的播放視頻可以很好的實現我所需要的效果。在之前請把插件引用進來,廢話不多說,代碼如下:
<!DOCTYPE html>
<html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>點擊播放視頻</title><link rel="stylesheet" href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" /><style type="text/css">body{background-color: #0964}.videolist { position:relative; float:left; width:540px; height:300px;margin-left: 230px;}.videolist:hover{ cursor: pointer; }.videoed { width:50px; height:50px; position: absolute; left:45%; top:40%; z-index:99; border-radius:100%; }.videos{ display:none; border: 1px solid #080808; position:fixed; left:50%; top:50%; margin-left:-320px; margin-top:-210px; z-index:100; width:640px; height:360px; }.vclose { position:absolute;right:1%; top:1%; border-radius:100%; cursor: pointer; }.con{height: 430px; width: 1000px;margin: 0 auto;}.masklayer{position: fixed;display: none;width: 100%;height: 1000px;top: 0px;left: 0px;background-color: rgba(0, 0, 0, 0.8);z-index: 1000;-webkit-user-select: none;-ms-user-select: none;}</style></head> <body><div class="video"><div class="con" style="margin-top: 100px"><div class="videolist" vpath="../img/圖片2.png" ipath="https://blz-videos.nosdn.127.net/1/OverWatch/AnimatedShots/Overwatch_AnimatedShot_CinematicTrailer.mp4"><img src="../img/圖片2.png" width="540px" height="300px" /><!--背景圖片--><img src="../img/播放Icon.png" class="videoed"/><!--播放按鈕 --></div><div class="masklayer"><!--遮罩層--><div class="videos"></div><!--存放視頻--></div></div></div><script src="../js/jquery.min.js"></script><script>$('.videolist').each(function(){ //遍歷視頻列表$(this).click(function(){ //視頻被點擊后執行var img = $(this).attr('vpath');//獲取視頻預覽圖var video = $(this).attr('ipath');//獲取視頻路徑$('.videos').html("<video id=\"video\" poster='"+img+"' style='width: 640px' src='"+video+"' preload=\"auto\" controls=\"controls\" autoplay=\"autoplay\"></video><img onClick=\"close1()\" class=\"vclose\" src=\"../img/gb.png\" width=\"25\" height=\"25\"/>");$('.videos').show();//視頻窗口彈出$('.masklayer').show(); //遮罩層彈出$('body').css('overflow', 'hidden'); //禁止滾動winHeight = document.body.clientHeight;$(".masklayer").height(winHeight + 5000 + "px"); });});function close1(){var v = document.getElementById('video');//獲取視頻節點$('.videos').hide();//點擊關閉$('.masklayer').hide();//遮罩層隱藏v.pause();//停止$('.videos').html();}</script></body>
</html>
實現的效果如下:
點擊播放按鈕之后彈出視頻播放窗口并且顯示遮罩層: