.video-embed {
position: relative;
padding-bottom: 56.25%;
padding-top: 0px;
height: 0;
margin-bottom: 20px;
}
.video-embed iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div id="video-embeds">
<div class="video-embed" style="">
<iframe width="560" height="315" src="http://www.youtube.com/embed/pa4IxrIsr9g" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen=""></iframe>
</div>
</div>
现在,当用户向下滚动视频时,我想要这个 <div id="video-embeds">
使用 iframe 以每个 @media css 不同的大小跳到右上角,并停留在那里直到用户返回到真实的视频位置,然后视频将再次出现在页面上......
如何做到这一点?用CSS可以吗?
更新: 滚动后我有我想要的 css。
#video-embeds-fixed {
opacity: 1;
pointer-events: auto;
display: block;
width: 400px;
height: 225px;
transition-property: opacity, height;
transition-duration: 366ms;
transition-timing-function: cubic-bezier(0.05, 0, 0, 1);
position: fixed;
overflow: hidden;
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16), 0 3px 6px 0 rgba(0,0,0,0.20);
right: 12px;
bottom: 10px;
}
.video-embed-fixed iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
现在 javascript 或 jquery 在滚动时更改 #video-embeds 到 #video-embeds-fixed 和 .video-embed 到 .video-embed-fixed?
最佳答案
仅靠 css 这是不可能的,你必须使用 javascript。 但首先将您的 css 固定 id 选择器更改为类(#video-embeds-fixed 在您的 css 文件中更改为 .video-embeds-fixed)。
JQuery 解决方案:
var video = $('#video-embeds');
var sticky = video.offset().top;
var videoHeight = video.height();
$(window).on('load scroll resize', function() {
if ((window.pageYOffset) >= (sticky + videoHeight)) {
video.addClass('video-embeds-fixed');
video.children('.video-embed').addClass('video-embed-fixed');
} else {
video.removeClass('video-embeds-fixed');
video.children('.video-embed').removeClass('video-embed-fixed');
}
});
关于css - 在滚动时更改 div/iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585541/