我正在尝试按照指示here在一个简单的CSS模式窗口中实现一个视频,它到目前为止工作得很好。我得到的唯一问题是在模态窗口打开之前视频自动启动(我必须保持视频“自动启动”选项)。当模态窗口关闭时,视频也不会停止。
是否有任何CSS / HTML解决方案?如果没有任何CSS / HTML解决方案,您可以帮助我使用JavaScript解决方案。
body{
margin: 0;
padding-left: 10%;
padding-right: 10%;
padding-top: 2%;
padding-bottom: 1%;
}
.boxspace {
overflow: hidden;
}
.box {
float: left;
position: relative;
width: 16.6666%;
padding-bottom: 16.6666%;
}
.boxInner {
position: absolute;
left: 2%;
right: 2%;
top: 2%;
bottom: 2%;
overflow: hidden;
border: thin solid #969696;
border-radius: 4%;
}
.boxInner img {
width: 100%;
}
.boxInner .titleBox {
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin-bottom: -42%;
background: #000000;
background: rgba(0, 0, 0, 0.8);
color: #FFFFFF;
padding-top: 2%;
padding-bottom: 2%;
padding-left: 2%;
padding-right: 2%;
text-align: center;
font-size: 1.2vw;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.boxInner .titleBox header{
color: #FFFFFF;
font-size: 1.4vw;
}
.boxInner .titleBox p{
color: #FFFFFF;
font-size: 1.0vw;
}
body.no-touch .boxInner:hover .titleBox, body.touch .boxInner.touchFocus .titleBox {
margin-bottom: 0px;
}
.modalVideo {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalVideo:target {
opacity:1;
pointer-events: auto;
}
.modalVideo > div {
position: relative;
margin-top: 2vw;
margin-left: 20vw;
margin-right: 20vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left:1.05vw;
padding-right:1.05vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.modalVideo video {
width: 100%;
}
.modalProgram {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalProgram:target {
opacity:1;
pointer-events: auto;
}
.modalProgram > div {
position: relative;
margin-top: 2vw;
margin-left: 10vw;
margin-right: 10vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left:2vw;
padding-right:2vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.close {
background: #FF0000;
color: #FFFFFF;
position: absolute;
top: 4vw;
right: -4.2vw;
text-align: center;
padding-left:0.75vw;
padding-right:0.75vw;
padding-bottom:0.25vw;
padding-top:0.25vw;
text-decoration: none;
border-top-left-radius: 0vw;
border-bottom-left-radius: 0vw;
border-top-right-radius: 1vw;
border-bottom-right-radius: 1vw;
}
.close:hover {
background: #464646;
}
<body class="no-touch">
<div class="boxspace">
<!-- tiles: -->
<div class="box">
<div class="boxInner">
<img src="http://dominicm.com/wp-content/uploads/2016/01/steam-arch-linux.png" />
<div class="titleBox">
<header>
Linux/C/ARM
</header>
<p>
<a href="#linux-c-arm_video">video</a><br>
<a href="#linux-c-arm_program">program</a><br>
prijavnica
</p>
</div>
</div>
</div>
</div>
<!-- end of tiles and wrap -->
<!--Linux/C/ARM - program-->
<div id="linux-c-arm_program" class="modalProgram">
<div>
<a href="#close" title="Close" class="close">close</a>
<h3>HEADER</h3>
<p>
Paragraph
</p>
</div>
</div>
<!--Linux/C/ARM - video-->
<div id="linux-c-arm_video" class="modalVideo">
<div>
<a href="#close" title="Close" class="close">close</a>
<video controls autoplay>
<source src="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4">
</video>
</div>
</div>
<!-- end "no-touch" parameter for body-->
<script type="text/javascript">
$(function(){
// See if this is a touch device
if ('ontouchstart' in window){
// Set the correct [touchscreen] body class
$('body').removeClass('no-touch').addClass('touch');
// Add the touch toggle to show text when tapped
$('div.boxInner img').click(function(){
$(this).closest('.boxInner').toggleClass('touchFocus');
});
}
});
</script>
</body>
答案 0 :(得分:3)
不能通过HTML或CSS完成此操作。
但你可以轻松地使用JavaScript,因为你已经有了一些jQuery代码,我建议你也使用jQuery,因为在这种情况下你可以通过在脚本中添加以下部分来轻松实现这一点
<强>的jQuery 强>
var $video = $(".modalVideo video")[0];
$video.autoplay = false;
$("a[href='#linux-c-arm_video']").click(function(){
$video.play();
});
$(".close").click(function(){
$video.pause();
});
$(function() {
// See if this is a touch device
if ('ontouchstart' in window) {
// Set the correct [touchscreen] body class
$('body').removeClass('no-touch').addClass('touch');
// Add the touch toggle to show text when tapped
$('div.boxInner img').click(function() {
$(this).closest('.boxInner').toggleClass('touchFocus');
});
}
});
var $video = $(".modalVideo video")[0];
$video.autoplay = false;
$("a[href='#linux-c-arm_video']").click(function() {
$video.play();
});
$(".close").click(function() {
$video.pause();
$video.currentTime = 0;
});
&#13;
body {
margin: 0;
padding-left: 10%;
padding-right: 10%;
padding-top: 2%;
padding-bottom: 1%;
}
.boxspace {
overflow: hidden;
}
.box {
float: left;
position: relative;
width: 16.6666%;
padding-bottom: 16.6666%;
}
.boxInner {
position: absolute;
left: 2%;
right: 2%;
top: 2%;
bottom: 2%;
overflow: hidden;
border: thin solid #969696;
border-radius: 4%;
}
.boxInner img {
width: 100%;
}
.boxInner .titleBox {
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin-bottom: -42%;
background: #000000;
background: rgba(0, 0, 0, 0.8);
color: #FFFFFF;
padding-top: 2%;
padding-bottom: 2%;
padding-left: 2%;
padding-right: 2%;
text-align: center;
font-size: 1.2vw;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.boxInner .titleBox header {
color: #FFFFFF;
font-size: 1.4vw;
}
.boxInner .titleBox p {
color: #FFFFFF;
font-size: 1.0vw;
}
body.no-touch .boxInner:hover .titleBox,
body.touch .boxInner.touchFocus .titleBox {
margin-bottom: 0px;
}
.modalVideo {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalVideo:target {
opacity: 1;
pointer-events: auto;
}
.modalVideo > div {
position: relative;
margin-top: 2vw;
margin-left: 20vw;
margin-right: 20vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left: 1.05vw;
padding-right: 1.05vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.modalVideo video {
width: 100%;
}
.modalProgram {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalProgram:target {
opacity: 1;
pointer-events: auto;
}
.modalProgram > div {
position: relative;
margin-top: 2vw;
margin-left: 10vw;
margin-right: 10vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left: 2vw;
padding-right: 2vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.close {
background: #FF0000;
color: #FFFFFF;
position: absolute;
top: 4vw;
right: -4.2vw;
text-align: center;
padding-left: 0.75vw;
padding-right: 0.75vw;
padding-bottom: 0.25vw;
padding-top: 0.25vw;
text-decoration: none;
border-top-left-radius: 0vw;
border-bottom-left-radius: 0vw;
border-top-right-radius: 1vw;
border-bottom-right-radius: 1vw;
}
.close:hover {
background: #464646;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body class="no-touch">
<div class="boxspace">
<!-- tiles: -->
<div class="box">
<div class="boxInner">
<img src="http://dominicm.com/wp-content/uploads/2016/01/steam-arch-linux.png" />
<div class="titleBox">
<header>
Linux/C/ARM
</header>
<p>
<a href="#linux-c-arm_video">video</a>
<br>
<a href="#linux-c-arm_program">program</a>
<br>prijavnica
</p>
</div>
</div>
</div>
</div>
<!-- end of tiles and wrap -->
<!--Linux/C/ARM - program-->
<div id="linux-c-arm_program" class="modalProgram">
<div>
<a href="#close" title="Close" class="close">close</a>
<h3>HEADER</h3>
<p>
Paragraph
</p>
</div>
</div>
<!--Linux/C/ARM - video-->
<div id="linux-c-arm_video" class="modalVideo">
<div>
<a href="#close" title="Close" class="close">close</a>
<video controls autoplay>
<source src="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4">
</video>
</div>
</div>
</body>
&#13;
如果您希望能够拥有多个视频,这些视频是通过特定ID动态调用的,则必须进行以下更改
首先,我建议将视频操作play()
,pause()
排除在名为playVideo()
的函数之外。
<强>功能强>
function playVideo(id) {
var $video = $(id + " video")[0];
$video.autoplay = false;
$video.play();
$(".close").click(function() {
$video.pause();
$video.currentTime = 0;
});
}
重要的是要注意,有了这个建议,该函数需要一个参数id。这将是您单击的链接的ID。这是有效的,因为链接的href具有与视频容器相同的id。
最后,为了获得视频应该显示的链接的ID,您必须将其添加到您的代码中。
$(".box a[href*='video']").click(function() {
var id = $(this).attr("href");
playVideo(id);
});
$(function() {
// See if this is a touch device
if ('ontouchstart' in window) {
// Set the correct [touchscreen] body class
$('body').removeClass('no-touch').addClass('touch');
// Add the touch toggle to show text when tapped
$('div.boxInner img').click(function() {
$(this).closest('.boxInner').toggleClass('touchFocus');
});
}
});
$(".box a[href*='video']").click(function() {
var id = $(this).attr("href");
playVideo(id);
});
function playVideo(id) {
var $video = $(id + " video")[0];
$video.autoplay = false;
$video.play();
$(".close").click(function() {
$video.pause();
$video.currentTime = 0;
});
}
&#13;
body {
margin: 0;
padding-left: 10%;
padding-right: 10%;
padding-top: 2%;
padding-bottom: 1%;
}
.boxspace {
overflow: hidden;
}
.box {
float: left;
position: relative;
width: 16.6666%;
padding-bottom: 16.6666%;
}
.boxInner {
position: absolute;
left: 2%;
right: 2%;
top: 2%;
bottom: 2%;
overflow: hidden;
border: thin solid #969696;
border-radius: 4%;
}
.boxInner img {
width: 100%;
}
.boxInner .titleBox {
position: absolute;
bottom: 0;
left: 0;
right: 0;
margin-bottom: -42%;
background: #000000;
background: rgba(0, 0, 0, 0.8);
color: #FFFFFF;
padding-top: 2%;
padding-bottom: 2%;
padding-left: 2%;
padding-right: 2%;
text-align: center;
font-size: 1.2vw;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.boxInner .titleBox header {
color: #FFFFFF;
font-size: 1.4vw;
}
.boxInner .titleBox p {
color: #FFFFFF;
font-size: 1.0vw;
}
body.no-touch .boxInner:hover .titleBox,
body.touch .boxInner.touchFocus .titleBox {
margin-bottom: 0px;
}
.modalVideo {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalVideo:target {
opacity: 1;
pointer-events: auto;
}
.modalVideo > div {
position: relative;
margin-top: 2vw;
margin-left: 20vw;
margin-right: 20vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left: 1.05vw;
padding-right: 1.05vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.modalVideo video {
width: 100%;
}
.modalProgram {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalProgram:target {
opacity: 1;
pointer-events: auto;
}
.modalProgram > div {
position: relative;
margin-top: 2vw;
margin-left: 10vw;
margin-right: 10vw;
padding-top: 1vw;
padding-bottom: 1vw;
padding-left: 2vw;
padding-right: 2vw;
border-radius: 1vw;
background: #ffffff;
font-size: 1.1vw;
}
.close {
background: #FF0000;
color: #FFFFFF;
position: absolute;
top: 4vw;
right: -4.2vw;
text-align: center;
padding-left: 0.75vw;
padding-right: 0.75vw;
padding-bottom: 0.25vw;
padding-top: 0.25vw;
text-decoration: none;
border-top-left-radius: 0vw;
border-bottom-left-radius: 0vw;
border-top-right-radius: 1vw;
border-bottom-right-radius: 1vw;
}
.close:hover {
background: #464646;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body class="no-touch">
<div class="boxspace">
<!-- tiles: -->
<div class="box">
<div class="boxInner">
<img src="http://dominicm.com/wp-content/uploads/2016/01/steam-arch-linux.png" />
<div class="titleBox">
<header>
Linux/C/ARM
</header>
<p>
<a href="#linux-c-arm_video">video</a>
<br>
<a href="#linux-c-arm_program">program</a>
<br>prijavnica
</p>
</div>
</div>
</div>
<div class="box">
<div class="boxInner">
<img src="http://dominicm.com/wp-content/uploads/2016/01/steam-arch-linux.png" />
<div class="titleBox">
<header>
Linux/Eagle/ARM
</header>
<p>
<a href="#linux-eagle_video">video</a>
<br>
<a href="#linux-eagleprogram">program</a>
<br>prijavnica
</p>
</div>
</div>
</div>
</div>
<!-- end of tiles and wrap -->
<!--Linux/C/ARM - program-->
<div id="linux-c-arm_program" class="modalProgram">
<div>
<a href="#close" title="Close" class="close">close</a>
<h3>HEADER</h3>
<p>
Paragraph
</p>
</div>
</div>
<!--Linux/C/ARM - video-->
<div id="linux-c-arm_video" class="modalVideo">
<div>
<a href="#close" title="Close" class="close">close</a>
<video controls autoplay>
<source src="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4">
</video>
</div>
</div>
<div id="linux-eagle_video" class="modalVideo">
<div>
<a href="#close" title="Close" class="close">close</a>
<video controls autoplay>
<source src="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" type="video/mp4">
</video>
</div>
</div>
</body>
&#13;
答案 1 :(得分:2)
据我所知,没有办法用CSS控制HTML视频播放器。 使用HTML时,无法告诉视频在打开/关闭模式时启动/停止,这就是javascript的用途。
要使用js控制视频,您可以在模态打开时使用普通javascript:
document.getElementById('videoId').play();
答案 2 :(得分:2)
从视频元素中删除autoplay
属性 - 当页面加载时您实际上不希望它播放,您希望在模式窗口打开时播放它。
将您的jQuery代码更改为以下内容:
$(function(){
// See if this is a touch device
if ('ontouchstart' in window){
// Set the correct [touchscreen] body class
$('body').removeClass('no-touch').addClass('touch');
// Add the touch toggle to show text when tapped
$('div.boxInner img').click(function(){
$(this).closest('.boxInner').toggleClass('touchFocus');
});
}
/////////////////////////
// Changes start here
/////////////////////////
// Play clicked video
var playClickedVideo = function () {
var videoId = $(this).attr('href');
var video = $(videoId + ' video')[0];
video.play();
};
$('.titleBox a').click(playClickedVideo);
// Pause video when the close button is clicked
var stopVideos = function () {
$('video').each(function (i, video) {
video.pause();
video.currentTime = 0; // To rewind (optional)
});
};
$('.close').click(stopVideos);
});
我在这里有一个CodePen演示:http://codepen.io/tinacious/pen/gLvwyy
我不得不修改你的一些CSS来查看链接,这样我就可以点击它们,所以只复制jQuery。