2017-02-15 14 views
0

次のHTMLでは、手作業でdivの高さを200ピクセルに設定しました。 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; 
} 

Wokring例はhereを見つけることができます。

+1

使用100VHはCSSの高さ(VHユニットは、ビューポートの高さです)。 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

のFirefox:Firefoxの19(2013)以来、サポート

クロム:クロム20(2012)以来、一部からサポート、クロム26はトータルサポート(2013)

サファリ:サファリ6.1(2013年)以来のトータルサポート、サファリ6(2012)以来、部分的にサポートされているので、

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>

また、ウィンドウの高さを取得するために.height()を使用して、jQueryのを使用してこれを行うことができます。

$(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

関連する問題