2017-02-15 132 views
0

在以下HTML中,我手动将中间div的高度设置为200px。如何自动调整高度以使div的高度等于浏览器中可见区域的高度?这可以用纯CSS完成还是需要JavaScript?将div的高度设置为基于可见区域

<p>Scroll Up and Down this page to see the parallax scrolling effect.</p> 

<div class="parallax"></div> 

<div class="red"> 
Scroll Up and Down this page to see the parallax scrolling effect. 
This div is just here to enable scrolling. 
Tip: Try to remove the background-attachment property to remove the scrolling effect. 
</div> 

而CSS:

.parallax { 
    /* The image used */ 
    background-image: url("http://az-themes.bluxart.netdna-cdn.com/ibuki/wp-content/uploads/2014/06/blog-img.jpg"); 

    /* Set a specific height */ 
    height: 200px; 

    /* Create the parallax scrolling effect */ 
    background-attachment: fixed; 
    background-position: center; 
    background-repeat: no-repeat; 
    background-size: cover; 

} 

.red { 
    height:1000px; 
    background-color:red; 
    color:white; 
    font-size: 40px; 
} 

工作呢例子可以发现here

+1

使用100vh(VH单元是视口的高度)为在CSS的高度。 100是告诉它是100的视口高度。你也有宽度的vw。 – creativekinetix

+0

浏览器支持'vh':http://caniuse.com/#feat=viewport-units – Sebastian

+0

它得到了相当广泛的支持。即使在IE中,你也只是在vmax单位上失利。还有其他的错误与打印和3d转换等有关,但我认为这不是什么大问题。 – creativekinetix

回答

4

为此使用vh单位 - 因此200px变为100vh以填充屏幕的整个高度。

但请确保您包含min-height,对于您的内容超过视口可以显示的时间。

至于此兼容性:

http://caniuse.com/#feat=viewport-units

火狐:因为火狐19(2013)

铬支持:由于部分地支持,因为铬20(2012)中,由于铬26总支持(2013)

Safari:自从Safari 6(2012)以来的部分支持,自Safari 6.1(2013)以来的全面支持

IE:因为IE 9(2011年)的部分支持

封边:封边以来12(2015)

当我说的部分支持,在所有这些情况下,他们不支持vmax,部分支持,无论如何,你并没有使用它。

.parallax { 
 
    /* The image used */ 
 
    background-image: url("http://az-themes.bluxart.netdna-cdn.com/ibuki/wp-content/uploads/2014/06/blog-img.jpg"); 
 
    /* Set a specific height */ 
 
    height: 100vh; 
 
    /* Create the parallax scrolling effect */ 
 
    background-attachment: fixed; 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
} 
 
.red { 
 
    height: 1000px; 
 
    background-color: red; 
 
    color: white; 
 
    font-size: 40px; 
 
}
<p>Scroll Up and Down this page to see the parallax scrolling effect.</p> 
 

 
<div class="parallax"></div> 
 

 
<div class="red"> 
 
    Scroll Up and Down this page to see the parallax scrolling effect. This div is just here to enable scrolling. Tip: Try to remove the background-attachment property to remove the scrolling effect. 
 
</div>

你也可以做到这一点使用jQuery,使用.height()得到窗口的高度。

$(document).ready(function() { 
 

 
    wh = $(window).height(); 
 

 
    $(".parallax").css({ 
 
    "height": wh 
 
    }); 
 

 
});
.parallax { 
 
    /* The image used */ 
 
    background-image: url("http://az-themes.bluxart.netdna-cdn.com/ibuki/wp-content/uploads/2014/06/blog-img.jpg"); 
 
    /* Set a specific height */ 
 
    height: 100vh; 
 
    /* Create the parallax scrolling effect */ 
 
    background-attachment: fixed; 
 
    background-position: center; 
 
    background-repeat: no-repeat; 
 
    background-size: cover; 
 
} 
 
.red { 
 
    height: 1000px; 
 
    background-color: red; 
 
    color: white; 
 
    font-size: 40px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>Scroll Up and Down this page to see the parallax scrolling effect.</p> 
 

 
<div class="parallax"></div> 
 

 
<div class="red"> 
 
    Scroll Up and Down this page to see the parallax scrolling effect. This div is just here to enable scrolling. Tip: Try to remove the background-attachment property to remove the scrolling effect. 
 
</div>

+1

这很好,但我担心兼容性,因为'vh'似乎是[新功能](http:// caniuse。com /#feat = viewport-units)仅在少数浏览器中受支持。 – Meysam

+0

@Meysam在你的问题上看到我的评论,但我会更新我的回答,并提供更详细的解释 –

+0

谢谢你的帮助。如果可以的话,那么如果你能够提出关于这种做法的替代方法的想法,这将是非常好的。 – Meysam