css - 在滚动时更改 div / iframe?
问题描述
.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');
}
});
推荐阅读
- javascript - 如果变体是我们的库存,如何在下拉列表中的产品变体旁边添加售罄文本?
- laravel - Laravel 广播私人频道 - 广播/身份验证错误 500
- python - 使用 selenium webdriver 滚动/加载动态表
- javascript - 不同函数中的Javascript函数数组问题
- android - 如何在命令行上列出 gradle 模块?
- xamarin - Visual Studio 显示有关 PrismApplication 的错误
- asp.net - 如何通过标记复选框来选择列表框的所有元素?
- android - Sceneform - 如何添加球体可渲染?
- python - pytest如何制作一个参数化另一个夹具的夹具
- selenium - 在 Xunit Selenium C# 中使用 cookie 时得到 System.NullReferenceException:对象引用未设置为对象的实例错误