如何获得固定位置div的高度? (并推送以下内容)

时间:2016-02-11 11:16:11

标签: html css background-image css-position

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

问题是我试图将图像作为全屏背景,但不使用背景css函数 background-image {} ,因为我无法检索缩略图在css代码中的帖子。

所以,我将我的图像显示为位置:固定; z-index:1 。 我用宽度:100%; height:auto; 完成代码,以使图像适合屏幕大小(如果屏幕大小则相当错误与图像的比例不同......)。

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

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

真正的问题是,因为我有一个位置:已修复我无法获得图像的高度,这意味着我可以' t动态推送页脚。

一种解决方案是复制图像div并为其提供可见性:隐藏,这样我就可以获得背景图像的确切大小并推动页脚。但我认为这个解决方案不够优雅。而且我确信它必须使用资源......好吧,即使它没有出现,我也不能轻易复制内容。

以下是问题的屏幕截图和视频,后面是一些代码:

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 {
    }

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式实现全屏幕背景。

选项1:

您将图片拉出标题到<style>标记并使用background-size:cover;

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

这将在标题中显示您的缩略图。但是你可以内联并在那里获取缩略图。

选项2:

第二个选项是使用javascript调整图像大小并使其工作。我会用:

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

并将其应用于全宽/高度div。它将使图像绝对定位并且运行良好。希望你能对它进行排序。