Я знаю, может быть, это слишком поздно для вас, но для поиска хорошего решения вашего вопроса у меня уйдет несколько ... дней; -)
Мне нужно было найти правильное отношение к первой ширине видео(самый большой со своим отступом) и шириной экрана.Ну вот, наконец-то я нашел (для видео с соотношением сторон 16/9):
r= (n-1)/n + 14p/9w * (n-1)/(n+1)
Где n
- номер видео, p
- ваш отступ, а w
- экран с экраном.
Это решение, после того как вам нужно всего лишь поместить его в математическую систему для расчета видео и их контейнеров по ширине и высоте:
const videoRap = 0.5625; // 16/9
const padding = 16; // 1rem
const nVideo = $(".video").length;
var widthVideo,
widthScreen,
heightVideo,
widthContainer,
newHeight,
rapp
function calc() {
widthScreen = $(".video-container").outerWidth();
rapp = (nVideo - 1) / (nVideo) + ((14 * padding) / (9 * widthScreen)) * ((nVideo - 1) / (nVideo + 1));
widthContainer = parseInt(widthScreen * rapp, 10);
widthVideo = parseInt(widthScreen - widthContainer, 10);
newHeight = parseInt(((widthContainer - (padding * 2)) * videoRap) + (padding * 2), 10);
heightVideo = parseInt((newHeight / (nVideo - 1)), 10);
$(".video").css({
"height": heightVideo,
"width": widthVideo
});
$(".video-container .video:first-child").css({
"width": widthContainer
});
$(".video-container").css({
"height": newHeight
});
}
Теперь вы можете указать, сколько видео вы хотите, jQuery выполняет все операции за вас: вы должны изменить только ваш отступ, если вы хотите изменить его.
const padding = 16; // 1rem
В CSS вы должны изменить только ваш «отступ» здесь:
.video-embed {
margin: auto;
position: relative;
height: calc(100% - 2rem); /* your padding * 2 */
width: calc(100% - 2rem); /* your padding * 2 */
}
Это весь код в действии (я добавляю пример с 4 небольшими видео, но вы можете указать, сколько видео вы хотите):
$(function () {
const videoRap = 0.5625; // 16/9
const padding = 16; // 1rem
const nVideo = $(".video").length;
var widthVideo,
widthScreen,
heightVideo,
widthContainer,
newHeight,
rapp
function calc() {
widthScreen = $(".video-container").outerWidth();
rapp = (nVideo - 1) / (nVideo) + ((14 * padding) / (9 * widthScreen)) * ((nVideo - 1) / (nVideo + 1));
widthContainer = parseInt(widthScreen * rapp, 10);
widthVideo = parseInt(widthScreen - widthContainer, 10);
newHeight = parseInt(((widthContainer - (padding * 2)) * videoRap) + (padding * 2), 10);
heightVideo = parseInt((newHeight / (nVideo - 1)), 10);
$(".video").css({
"height": heightVideo,
"width": widthVideo
});
$(".video-container .video:first-child").css({
"width": widthContainer
});
$(".video-container").css({
"height": newHeight
});
}
calc();
$(window).resize(function () {
calc();
});
});
* {
box-sizing: border-box;
}
.video-container {
display: flex;
flex-wrap: wrap;
flex-direction: column;
width: 100%;
}
.video{
flex: 1 1 auto;
width:auto;
display:flex;
}
.video-container .video:first-child {
height:100% !important;
}
.video-embed {
margin: auto;
position: relative;
height: calc(100% - 2rem); /* your padding * 2 */
width: calc(100% - 2rem); /* your padding * 2 */
}
.video iframe {
width: 100%;
height: 100%;
position: absolute;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<div class="video-container">
<div class="video"><div class="video-embed"><iframe frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/25300082?autoplay=0"></iframe></div></div>
<div class="video"><div class="video-embed"><iframe frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/25300082?autoplay=0"></iframe></div></div>
<div class="video"><div class="video-embed"><iframe frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/25300082?autoplay=0"></iframe></div></div>
<div class="video"><div class="video-embed"><iframe frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/25300082?autoplay=0"></iframe></div></div>
<div class="video"><div class="video-embed"><iframe frameborder="0" allowfullscreen="allowfullscreen" src="https://player.vimeo.com/video/25300082?autoplay=0"></iframe></div></div>
</div>
Было действительно интересно найти решение с использованием только flexbox и некоторого javascript.Большое спасибо за вопрос!;)