2017-07-06 5 views
0

私の要件をよりよく説明するページレイアウトを添付してください。 Page layoutマイページのフッタの直前にdivを配置する

  1. フッターセクションの直前にテキストを配置します。
  2. フッターが表示されない場合があります(スクロールが必要な場合があります)。その場合は、テキストを表示領域の下に移動します。

私はこれを達成するために多くの方法を試しました。 この問題を解決するためのあらゆる指針が役立ちます。

おかげで、 Santhosh

+2

_私はこれを達成するために多くの方法を試しました。_あなたが行ったことを再現するために私たちがあなたの時間と私たちを浪費しないように正確にそれを示す[mcve]を投稿してください。 – j08691

+1

現在持っている? – DanD

答えて

3

footerは、我々が使用しますJavascriptの表示されていないときは、bottom-textを示すために、この

body { 
 
    margin: 0; 
 
    
 
    display: flex; 
 
    flex-direction: column; 
 
    min-height: 100vh; 
 
} 
 

 
.content { 
 
    /* occupy all height */ 
 
    flex: 1 0 auto; 
 
    /* nested flex container */ 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.bottom-text { 
 
    /* Move to the bottom */ 
 
    /* This works because this is flex item */ 
 
    margin-top: auto; 
 
} 
 

 
/* styles just for demo */ 
 
body { 
 
    text-align: center; 
 
} 
 

 
header { 
 
    background-color: tomato; 
 
} 
 

 
.content { 
 
    background-color: lightsteelblue; 
 
} 
 

 
.bottom-text { 
 
    background-color: moccasin; 
 
} 
 

 
footer { 
 
    background-color: lime; 
 
}
<header>Page header</header> 
 
<section class="content"> 
 
    Page content 
 
    <div class="bottom-text">Place a text just before footer</div> 
 
</section> 
 
<footer>Page footer</footer>

を達成するためにフレキシボックスを使用することができます。

// Checks if element is visible on screen 
 
function checkVisible(element) { 
 
    var rect = element.getBoundingClientRect(); 
 
    var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight); 
 
    return !(rect.bottom < 0 || rect.top - viewHeight >= 0); 
 
} 
 

 
var footer = document.querySelector("footer"); 
 
var bottomText = document.querySelector(".bottom-text"); 
 
var bottomTextFixedClassName = "bottom-text--fixed"; 
 

 
// Sets element position as fixed 
 
// when footer is not visible on screen 
 
function setFixedButtonText() { 
 
    if (checkVisible(footer)) 
 
    bottomText.classList.remove(bottomTextFixedClassName); 
 
    else 
 
    bottomText.classList.add(bottomTextFixedClassName); 
 
} 
 

 
window.addEventListener("scroll", setFixedButtonText); 
 

 
setFixedButtonText();
body { 
 
    margin: 0; 
 
    display: flex; 
 
    flex-direction: column; 
 
    min-height: 100vh; 
 
} 
 

 
.content { 
 
    /* occupy all height by flex-grow: 1 */ 
 
    /* Don't shrink using flex-shrink: 0 */ 
 
    /* Setting flex-basis to 1500px to emulate long content */ 
 
    /* Replace 1500px with auto in production code */ 
 
    flex: 1 0 1500px; 
 
    /* nested flex container */ 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.bottom-text { 
 
    /* Move to the bottom */ 
 
    /* This works because this is flex item */ 
 
    margin-top: auto; 
 
} 
 

 
.bottom-text--fixed { 
 
    position: fixed; 
 
    left: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
} 
 

 
/* styles just for demo */ 
 
body { 
 
    text-align: center; 
 
} 
 

 
header { 
 
    background-color: tomato; 
 
} 
 

 
.content { 
 
    background-color: lightsteelblue; 
 
} 
 

 
.bottom-text { 
 
    background-color: moccasin; 
 
} 
 

 
footer { 
 
    background-color: lime; 
 
}
<header>Page header</header> 
 
<section class="content"> 
 
    Page content 
 
    <div class="bottom-text">Place a text just before footer</div> 
 
</section> 
 
<footer>Page footer</footer>

あなたはIEが必要な場合は、height: 100vh;に変更min-height: 100vh;を使用することができますsuppost。これはflex-direction: column;でflexのバグのIEのmin-heightバグの回避策です。

+0

これは処理していないようです*「フッターが表示されない(スクロールする必要がある)場合がありますが、その場合はテキストを表示領域の下に移動してください」* –

+0

これを実現する方法はありますか別のチームによって作成された反応アプリケーションに取り組んでいました。 – kallada

+0

@kallada Javascript(cc @MichaelCoker)を使用してスクロール動作を追加しました(画面下部に下のテキストを表示) –

関連する問題