2017-02-15 129 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