2017-12-03 5 views
-1

私は2つのCTAボタンを持つdivを持っています。私はこのdivをページの90%に達するか、#footer divに到達した後で非表示にします。私がこれをやっているのは、それがフッタに干渉しないようにするためです。スクロールの一定割合の後でdivを非表示にする

私はいくつかのコードを見つけましたが、パーセンテージに基づいていない800pxスクロール後にdivを非表示にします。

$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > 800) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

任意のアイデア?

答えて

0

隠すこれは、ページの90%に達した後DIV:

あなたはMath.round($(document).height() * 90/100)

var height = Math.round($(document).height() * 90/100); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > height) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

で90%を取得する必要があります

や私#footer divのに達する

あなたは窓からのオフセット要素を取得するためoffset().topを使用する必要があります。

var height = Math.round($('#footer').offset().top); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y > height) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div> 
 
<div style="height:1000px;"></div> 
 
<div id="footer"></div>

+1

Great Pedram。私のために働いた。お返事をありがとうございます。 –

+0

しかし、@pedram uは反対をしました。フッターに到達するとbottomMenuが非表示になります。 –

+0

@MohammadGhonchesefidiは単に '>'で '<';)に変更します。 –

0

あなたは、単にあなたが必要とする割合を取得するには、いくつかの計算を行うことができます。

var limit = parseInt(($(document).height() * 70)/100); 
 
/* here I took 70% of the height of the page*/ 
 

 
console.log(limit); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 
    if (y < limit) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

0
ここ

私はパーセンテージでそれを変換しています。 PER値を更新して設定を更新してください。

//PER is Percentage value 
 
var PER = 90; 
 
var yInPixel, totalHeight; 
 

 
//If page if with dynamic height change totalHeight and yInPixel after Resize Event 
 
$(document).ready(function() { 
 
    totalHeight = $(this).height(); 
 
    yInPixel = totalHeight * (PER/100) - window.innerHeight; 
 
}); 
 

 
$(document).scroll(function() { 
 
    var y = $(this).scrollTop(); 
 

 
    if (y > yInPixel) { 
 
    $('.bottomMenu').fadeIn(); 
 
    } else { 
 
    $('.bottomMenu').fadeOut(); 
 
    } 
 
});
body { 
 
    height: 1600px; 
 
} 
 

 
.bottomMenu { 
 
    display: none; 
 
    position: fixed; 
 
    bottom: 0; 
 
    width: 100%; 
 
    height: 60px; 
 
    border-top: 1px solid #000; 
 
    background: red; 
 
    z-index: 1; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="bottomMenu"></div>

+0

これは、パフォーマンスのために非常に悪いです。あなたは無駄な各スクロールで計算をやり直します。このロジックを外に置く方が良いです。 –

+0

ページが動的な高さを持ち、何らかのイベントでそれを変更している場合はどうなりますか? –

+0

'もしページが動的な高さを持っているなら';しかしここではそうではありません。これはここでは適切ではありません(もちろん、あなたの解決策ではありません)。答えがよりよくなるように解答にコメントを追加することを検討してください; –

関連する問題