【问题标题】:How to get the height of a fixed positionned div? (and push following content)如何获得固定定位 div 的高度? (并推送以下内容)
【发布时间】:2016-02-19 16:17:13
【问题描述】:

我正在尝试为 wordpress 博客创建模板。

问题是我试图将图像作为全屏背景,但没有使用背景 css 函数 background-image {},因为我无法在CSS 代码。

所以,我将图像显示为 position: fixed; z-index:1。 我用 width: 100%;height: auto; 完成代码,以使图像适合屏幕尺寸(如果屏幕和图片的比例不一样……)。

然后我显示可以具有动态高度的页脚。对于这种情况,我为整个页面使用了一个 flexbox。

我的担忧是: 背景图像可以有不同的比例,我希望它无论如何都能填满视口,但不要溢出它。

真正的问题是,因为我有一个 position: fixed 我无法获得图像的 height,这意味着我无法动态推动页脚。 p>

一种解决方案是复制图像 div 并为其赋予 visibility: hidden 以便我获得背景图像的确切大小并推动页脚。但我认为这个解决方案不优雅。而且我确信它必须使用资源......好吧,即使它没有出现,我也对复制内容感到不满意。

这里是问题的截图和视频,后面是一些代码:

Screenshot 1

Video of the concern

HTML

<?php get_header(); ?>

<div id="single.php">
  <?php include("menu.php"); ?>
  <section id="content">
    <div class="row">
      <div class="col">
        <?php if(have_posts()) :?>
        <?php while(have_posts()) : the_post(); ?>
        <div class="postThumbnail">
          <?php
              if ( has_post_thumbnail() ) {
                  the_post_thumbnail();
              }
            ?>
        </div>
        <div class="col-1">
          <div class="col-1-1">             
              <div id="postMetadata">
                <div id="postTitle">
                  <h2>
                    <?php the_title(); ?>
                  </h2>
                </div>
                <div id="postCategory">
                  <p>
                    <?php
                    $category = get_the_category();
                    echo $category[0]->slug;
                  ?>
                  </p>
                </div>
                <div id="postDate">
                  <p>
                    <?php the_time("d F") ?>
                  </p>
                </div>
              </div>
              <div id="postContent" >
                <?php the_content(); ?>
              </div>
            </div>
          </div>
        <?php endwhile; ?>
        <?php endif; ?>
      </div>
      </div>
  </section>
</div>

<?php get_footer(); ?>

CSS

/* ----- ----- ----- ----- ----- */
/* html */

html {
    width: 100%;
    height: 100%;    
}

/* ----- ----- ----- ----- ----- */
/* body */

body {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-width: 640px;
    height: 100%;
    font-family: Open Sans, Arial;
    font-size: 1em;
    line-height: 1em;
}

/* ----- ----- ----- ----- ----- */
/* header */

header {
    position: fixed;
    z-index: 999; /* do not remove = header always on top of content */
    width: 100%;
    height: 48px;
    font-size: 1.2em;
    color: rgb(255, 255, 255);
    background: rgb(0, 0, 0);
    transition: all 1s ease;
    box-shadow: rgb(0, 0, 0) 0px 0px 8px;
}

    header:hover {
        transition: all 1s ease;
        box-shadow: rgb(40, 40, 40) 0px 0px 12px;
    }

#headerMenu {
    position: relative;
    display: block;
    height: 100%;
    line-height: 48px;
    margin: 0 32px 0 32px;
}


#siteLogo {
    position: absolute;
    left: 0;
    top: 0;
    line-height: 48px;
}

    #siteLogo a img {
        display: inline-block;
        vertical-align: middle;
        width: 24px;
        height: 24px;
    }

    #siteLogo a h1 {
        display: inline-block;
        vertical-align: middle;
        letter-spacing: 0.3em;
        color: rgb(255,255,255);
    }


#searchEngine {
    position: absolute;
    top: 8px;
    right: 0;
}

#searchForm input {
    position: relative;
    width: 164px;
    height: 32px;
    display: block;
    line-height: 32px;
    vertical-align: middle;
    margin: 0 32px 0 0;
    padding: 0 16px 0 16px;
    font-size: 1em;
    color: rgb(0, 0, 0);
    background: rgb(255, 255, 255);
    border: none;
    transition: width 0.5s ease;
}

    #searchForm input:focus {
        width: 226px;
    }

#index\.php #searchForm input:focus {
    width: 226px;
}

#search\.php #searchForm input {
    transition: width 0.5s ease;
    animation-name: searchFormResize;
    animation-duration: 0.5s;
}

@keyframes searchFormResize {
    from {
        width: 226px;
    }

    to {
        width: 164px;
    }
}

/* ----- ----- ----- ----- ----- */
/* footer */

footer {
    position: relative;
    z-index: 998;
}

    footer, footer a {
        color: rgb(255, 255, 255);
        line-height: 2em;
    }

#twitter {
    background: rgb(99, 185, 251);
    background: linear-gradient(to bottom, rgb(99, 185, 251)0%,rgb(56, 110, 151) 100%);
    transition: all 1s ease;
    box-shadow: rgb(99, 185, 251) 0 0 4px;
}

    #twitter:hover {
        transition: all 1s ease;
        box-shadow: rgb(99, 185, 251) 0 0 8px;
    }

    #twitter .twitterTimeline {
        width: 520px;
        margin: 0 auto;
        padding: 32px 0 24px 0;
    }

    #twitter #twitterIcon {
        display: block;
        width: 32px;
        height: 32px;
        margin: 0 auto 16px auto;
    }

    #twitter #lastTweet {
        min-height: 90px;
    }

#infoFooter {
    padding: 16px 0 16px 0;
    background: rgb(40, 40, 40);
    background: linear-gradient(to bottom, rgb(40, 40, 40) 0%,rgb(0, 0, 0) 100%);
    transition: all 1s ease;
    box-shadow: rgb(40, 40, 40) 0 0 8px;
}

    #infoFooter:hover {
        transition: all 1s ease;
        box-shadow: rgb(40, 40, 40) 0 0 12px;
    }

#siteMap {
    font-size: 0.8em;
}

    #siteMap li a {
        color: rgb(150, 150, 150);
    }

    #siteMap h3, #siteMap #iconHeart, #siteMap #iconContact, #siteMap #iconCategories {
        display: inline-block;
        vertical-align: middle;
    }

    #siteMap #iconHeart, #siteMap #iconContact, #siteMap #iconCategories {
        width: 16px;
        height: 16px;
        position: relative;
        left: 8px;
        top: 1px;
    }

#copyright {
    padding: 16px 0 0 0;
    font-size: 0.7em;
}

    #copyright p {
        color: rgb(75, 75, 75);
        text-align: center;
    }

    #copyright a {
        color: rgb(100, 100, 100);
    }

    #copyright #siteName {
        letter-spacing: 0.3em;
    }

/* ----- ----- ----- ----- ----- */
/* misc */

#index\.php,
#single\.php,
#page\.php,
#search\.php,
#\404\.php {
    flex: 1;
}

#content {
    position: relative;
    margin: 48px 0 0 0;
    z-index: 997;
}

.row {
    width: 100%;
}

.col { /* /!\ must not have width setting*/
    display: block;
    max-width: 960px;
    height: 100%;
    margin: 0 auto;
    padding: 0 16px 0 16px;
}

.col-1-1 {
    display: inline-block;
    width: calc(100% - 32px);
    margin: 0 16px 0 16px;
}

.col-1-2 {
    display: inline-block;
    vertical-align: top;
    width: calc(50% - 32px);
    margin: 0 16px 0 16px;
}

.col-1-3 {
    display: inline-block;
    vertical-align: top;
    width: calc((100% / 3) - 32px);
    margin: 0 16px 0 16px;
}

.col-2-3 {
    display: inline-block;
    vertical-align: top;
    width: calc(((100% / 3) * 2) - 32px);
    margin: 0 16px 0 16px;
}

.col-1-4 {
    display: inline-block;
    vertical-align: top;
    width: calc(25% - 32px);
    margin: 0 16px 0 16px;
}

.col-2-4 {
    display: inline-block;
    vertical-align: top;
    width: calc(50% - 32px);
    margin: 0 16px 0 16px;
}

.col-3-4 {
    display: inline-block;
    vertical-align: top;
    width: calc(75% - 32px);
    margin: 0 16px 0 16px;
}

/* ----- ----- ----- ----- ----- */
/* responsive */

@media screen and (max-width: 640px) {
    #siteMap .col-1-3 {
        display: inline-block;
        width: calc(100% - 32px);
        margin: 0 16px 16px 16px;
    }
}

/* ----- ----- ----- ----- ----- */
/* single.php */

#single\.php .postThumbnail img {
    position: fixed;
    width: 100%;
    height: auto;
    z-index: 1;
}

#single\.php, #single\.php #content {
    min-height: 100%;
}

    #single\.php #postMetadata {
    }

    #single\.php #postContent {
    }

【问题讨论】:

    标签: html css background-image css-position


    【解决方案1】:

    您有两个选项可以实现全屏背景。

    选项 1:

    您将图像拉出到标题中并放入&lt;style&gt; 标记并使用background-size:cover;

    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
    

    这将使您的缩略图帖子出现在标题中。但是,您可以内联并在那里获取缩略图。

    选项 2:

    第二种选择是使用 javascript 来调整图像大小并使其正常工作。我会使用:

    http://johnpolacek.github.io/imagefill.js/

    并将其应用于全宽/高 div。它将图像绝对定位并运行良好。希望你能解决这个问题。

    【讨论】:

    • 嗨,安迪,感谢您的回答。不幸的是,我不想乱来,因为我不是开发人员。而且我不想将图像放在背景中,因为我需要它的高度来放置内容+页脚。基本上,图像的高度可以小于视口的高度,所以我需要一个与固定 div 大小完全一致的 div 来推送内容。但第二种选择可能是解决方案的一部分。
    • 理论上你不需要使用图像高度来使页脚保持粘性。本教程向您展示如何。 ryanfait.com/sticky-footer
    • 这不是真正的粘性页脚。也许我真的不清楚我打算做什么:我想显示一个固定的图像。然后,在图像之后,无论图像是什么高度,我都想要一些文字。但在图像之后。这在这里很困难,因为固定图像没有物理高度。这是一种视差效果:在固定图像上滚动的内容。但我只想在 CSS 中制作,图像可以尊重视口的宽度大小。
    • 我几乎做了我想做的事。 http://extatic.net 随便点一篇文章自己看。但我有——至少——还有一个担忧。显示文本/内容的白色背景 div 必须位于屏幕高度(最小高度)减去页脚高度,以便内容完全覆盖背景图像。有什么想法吗?
    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多