【问题标题】:How to push all lower elements down when expanding a div?扩展div时如何将所有较低的元素向下推?
【发布时间】:2017-05-09 21:24:17
【问题描述】:

我有一个视频 iframe,在单击按钮时会展开。这包含在具有 100vh 高度(全屏)的页面的标题内。当视频展开时,它会将标题中的其余内容向上推,以使所有内容在标题中垂直居中。相反,我想将标题 div 的高度从 100vh 增加到 100vh 加上视频高度。但是,到目前为止我的代码不起作用。

LINK TO PAGE

$(document).ready(function(){
    $(".video" ).click(function() {    
        $(".header").height("+=315");
        document.getElementById("video__player").style.maxHeight = "50vw";
        
        document.getElementById("video-btn").style.display = "none";      
    });
});
.header {height: 100vh; width: 100vw;}

.header__content {height: calc(100vh); width: 100%; display: flex; display: -webkit-flex;}
.header__content--container {margin: auto; display: flex; display: -webkit-flex; flex-direction: column; -webkit-flex-direction: column;}
.video__container {text-align: center;}
.video {color: gray; width: 100%; height: auto;}
.play-btn {width: 20px; height: 20px; vertical-align: middle;}
.video__player {overflow: hidden; width: 300px; max-height: 0px; transition: max-height 0.5s ease-in-out; -webkit-transition: max-height 0.5s ease-in-out; margin: auto;}
<div class="header" id="header">
                
                <div class="header__menu">
                    <div class="header__menu--left">
                        <div class="logo">
                            <img class="logo__full img__full" src="assets/kbd-logo-english.svg">
                            <img class="logo__mobile img__full" src="assets/kbd-logo-mobile.svg">
                        </div>
                    </div>
                    <div class="header__menu--right">
                        <div class="phone">
                            <h9 class="strong" style="color: #5CBDE1;">1-855-636-0002</h9>
                        </div>
                        <a class="quote menu__quote clickable" href="https://pt.atrium-uw.com/Quote?Id=81" style="margin: 0;">
                            <h8 class="strong" style="margin: auto;">Get your quote</h6>
                        </a>
                        <div class="language">
                            <h9 class="clickable strong" style="color: #5CBDE1;">FR</h9>
                        </div>
                    </div>
                </div>
                
                <div class="header__content">
                    <div class="header__content--container animatedParent animateOnce">
                        <h1>Hi, we're KBD Insurance.</h1>
                        <br>
                        <h2 class="text__center">Life is chaotic. Insurance doesn't have to be.</h2>
                        <br><br>
                        <div class="quote__wrapper header__quote--wrapper">
                            <a class="quote menu__quote clickable animated fadeInDownShort slow" href="https://pt.atrium-uw.com/Quote?Id=81" style="margin: 0; height: 60px;">
                                <h8 class="strong" style="margin: auto;">Get your quote</h6>
                            </a>
                        </div>
                        <br><br>
                        <div class="video__container animated fadeInDownShort slow">
                            <a class="h6 video clickable" id="video-btn">Watch the video <img class="play-btn" src="assets/kbd-icon-play.svg"></a>
                        </div>
                        <div class="video__player" id="video__player">
                            <iframe src="https://www.youtube.com/embed/SIaFtAKnqBU" frameborder="0" allowfullscreen></iframe>
                        </div>
                    </div>
                </div>
                
            </div>

【问题讨论】:

  • Snippet 抱怨 $ 未定义。
  • @OliviaRuth sn-p 不能很好地工作,因为许多样式都没有设置。最好使用我提供的链接。
  • 这将如何运作?您的副本位于视频上方的 DOM 中。如果您希望它移到下方,那么您将让文本遍历视频 - 这看起来很奇怪。相反,您可以在包含与上述相同文本的视频下方创建一个隐藏的 div,然后在单击开始播放视频时,隐藏顶部文本并显示底部文本。
  • @Korgrue 我不太清楚你的意思。我希望视频上方的所有内容都保持在展开的位置。视频下方的所有内容都应向下移动。也许我可以根据扩展div的高度变化用javascript增加标题div的高度?
  • 您是否考虑过使用 JQuery 的 animate 来更改高度而不仅仅是更改值?您可以指定动画样式,并尝试一种符合您要求的动画

标签: javascript jquery html css


【解决方案1】:

.header__content--containermargin 当前设置为 auto。将边距更改为一些硬编码值,例如300px,它会按照您的意愿工作。

编辑:请求的函数

function setMarginTop() {
    const $header = $(".header__content--container");
    const marginTop = $header.css("margin-top");
    $header.css("margin-top", marginTop);
}

【讨论】:

  • 好的,所以边距设置为自动。有没有一种方法可以在单击按钮时使用 javascript 捕获上边距的实际值,然后设置 margin-top 以使用 javascript 保留该值?
  • 经过测试,将边距更改为设定值可使页面上的所有内容保持静态。可能是朝着正确方向迈出的一步,但不是我正在寻找的答案。
  • 在这种情况下,我建议将包含 iframe 的 div 移到 header div 之外,否则您的要求是不可能的。
  • 我刚刚对我目前的代码所做的编辑怎么样。为什么我不能通过扩展 div 的高度来增加标题 div 的高度?
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多