我已经使用Twitter Bootstrap 4, perfect-scrollbar plugin 和一些自定义CSS制作了水平的视频(和字幕)轮播。
我的目标是使视频的宽度从0过渡到100%,并进行平滑过渡。我只想为宽度 设置动画,但是高度也可以设置动画。我到目前为止编写的代码:
if ($('#carousel').length > 0) {
var ps = new PerfectScrollbar('#carousel', {
useBothWheelAxes: true,
maxScrollbarLength: 100
});
$('.video-box').each(function() {
var vid = $(this).find('video');
vid.closest('.video-container').addClass('loaded');
vid.hover(function() {
$(this).get(0).play();
}, function() {
$(this).get(0).pause();
})
});
}
.hero {
height: 100vh;
background: #111;
}
#carousel {
display: flex;
list-style-type: none;
position: relative;
min-width: 100vw;
margin: 0;
padding: 0;
justify-content: left;
align-items: center;
}
#carousel li {
padding: 0;
flex-shrink: 0;
position: relative;
}
#carousel li a {
text-decoration: none;
color: #fff;
display: block;
}
#carousel li .video-container video {
transition: width 1s ease-in-out;
width: 0;
height: 100%;
}
#carousel li .video-container.loaded video {
width: 100%;
}
#carousel li .caption {
padding: 20px 20px 0 20px;
}
#carousel li h2 {
font-size: 22px;
font-weight: 900;
line-height: 1;
margin: 0;
padding: 0;
}
#carousel li p {
font-size: 9px;
padding: 0;
margin: 5px 0 0 0;
}
#carousel .ps__rail-x {
background: #5C5C5C;
height: 3px;
margin: 0 40% 10vh 40%;
}
#carousel .ps__thumb-x {
height: 3px;
margin-bottom: -2px;
border-radius: 0;
background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://code-love.tk/demos/prop/lib/js/perfect-scrollbar.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="http://code-love.tk/demos/prop/lib/css/perfect-scrollbar.css" />
<div class="hero carousel-container d-flex">
<ul id="carousel">
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="caz.html">
<div class="video-container">
<video src="//code-love.tk/video/commerciala.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem ipsun dolor</h2>
<p>A true story</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<div class="video-container">
<video src="//code-love.tk/video/commerciala.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem</h2>
<p>Lorem ipsum dolor sit</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<div class="video-container">
<video src="//code-love.tk/video/flamenco.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem ipsum</h2>
<p>Lorem ipsum dolor sit.</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<video src="//code-love.tk/video/koffee.mp4" loop muted></video>
<div class="caption">
<h2>Into the wild</h2>
<p>Lorem ipsum dolor sit amet, consectetur</p>
</div>
</a>
</li>
</ul>
</div>
“实时”页面为 HERE 。我究竟做错了什么?我该如何处理这些视频?
答案 0 :(得分:1)
您可以为此使用纯JS修复程序。像这样:
var curWidth = $(this).width();
$(this).css('width', 'auto');
var autoWidth = $(this).width();
$(this).width(curWidth).animate({
width: autoWidth
}, 500);
此代码段基本上将宽度从0设置为自动,将值保存为变量,然后在将动画设置为自动宽度之前将宽度重置为0。这是我通常的设置,用于设置宽度或高度从0到自动的动画。
伴随着一点css:
.video-container video {
width: 0;
}
答案 1 :(得分:0)
声明高度并标记两者对于视频本身很重要。但是,这会使视频的宽高比失真。不知道为什么要对视频执行此操作,但是我很确定这会起作用。
video {
width: 100%; !important
height: 100%; !important /* or whatever you want the height to be doesn't have to be 100% */
}
答案 2 :(得分:0)
我认为,您应该像这样将<video src="//code-love.tk/video/koffee.mp4" loop muted></video>
用<div class="video-container"></div>
包裹起来:
<div class="video-container">
<video src="//code-love.tk/video/koffee.mp4" loop muted>
</div>
if ($('#carousel').length > 0) {
var ps = new PerfectScrollbar('#carousel', {
useBothWheelAxes: true,
maxScrollbarLength: 100
});
$('.video-box').each(function() {
var vid = $(this).find('video');
vid.closest('.video-container').addClass('loaded');
vid.hover(function() {
$(this).get(0).play();
}, function() {
$(this).get(0).pause();
})
});
}
.hero {
height: 100vh;
background: #111;
}
#carousel {
display: flex;
list-style-type: none;
position: relative;
min-width: 100vw;
margin: 0;
padding: 0;
justify-content: left;
align-items: center;
}
#carousel li {
padding: 0;
flex-shrink: 0;
position: relative;
}
#carousel li a {
text-decoration: none;
color: #fff;
display: block;
}
#carousel li .video-container video {
transition: width 1s ease-in-out;
width: 0;
height: 100%;
}
#carousel li .video-container.loaded video {
width: 100%;
}
#carousel li .caption {
padding: 20px 20px 0 20px;
}
#carousel li h2 {
font-size: 22px;
font-weight: 900;
line-height: 1;
margin: 0;
padding: 0;
}
#carousel li p {
font-size: 9px;
padding: 0;
margin: 5px 0 0 0;
}
#carousel .ps__rail-x {
background: #5C5C5C;
height: 3px;
margin: 0 40% 10vh 40%;
}
#carousel .ps__thumb-x {
height: 3px;
margin-bottom: -2px;
border-radius: 0;
background: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://code-love.tk/demos/prop/lib/js/perfect-scrollbar.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="http://code-love.tk/demos/prop/lib/css/perfect-scrollbar.css" />
<div class="hero carousel-container d-flex">
<ul id="carousel">
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="caz.html">
<div class="video-container">
<video src="//code-love.tk/video/commerciala.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem ipsun dolor</h2>
<p>A true story</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<div class="video-container">
<video src="//code-love.tk/video/commerciala.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem</h2>
<p>Lorem ipsum dolor sit</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<div class="video-container">
<video src="//code-love.tk/video/flamenco.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Lorem ipsum</h2>
<p>Lorem ipsum dolor sit.</p>
</div>
</a>
</li>
<li class="video-box col-xs-12 col-sm-6 col-md-4">
<a href="#">
<div class="video-container">
<video src="//code-love.tk/video/koffee.mp4" loop muted></video>
</div>
<div class="caption">
<h2>Into the wild</h2>
<p>Lorem ipsum dolor sit amet, consectetur</p>
</div>
</a>
</li>
</ul>
</div>