css - 在滚动时更改 div/iframe?

标签 css 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');
    }
});

关于css - 在滚动时更改 div/iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585541/

相关文章:

firefox - 响应式 iframe 中的 YouTube 视频未在 Firefox 中显示

javascript - iframe 高度在页面内容较长后不会缩小

javascript - Power BI iframe窃取页面焦点

html - DIV css 背景图像不会在 IE 8 中显示

html - 当页面为全尺寸时 div 具有 100% 宽度,但当页面尺寸减小时宽度减小

jquery .load 在 iframe 上添加覆盖 css 样式

javascript - 如何在不加载相同 URL 的情况下镜像 iframe? CSS 未激活

html - 悬停时的 Bootstrap 下拉自定义

html - 为字段的下拉列表选项自定义 html 输出

php - 如何使用 php 将不同的 css 添加到最后一个 mysql 查询结果