私のコンテンツdivのスクロールバーが必要かどうかをチェックし、ビューポートの高さに基づいてdivにoverflow-yとmax-heightを設定するjQueryスクリプトを作っています。しかし、私はビューポートの高さと私のページの高さを確認する警告を行うときにいくつかの厄介な結果を取得しています。私の場合のstatmentはif(サイトの高さ>ビューポートの高さ){値の設定}でも、私はいつも、ビューポートの高さ>サイトの高さを警告し、ifを実行して値を設定します。jQueryスクリプトは私にwierdの値を与えます
誰がそれを必要とする必要がある場合は、これは作業コードです(オーバーフロー:自動車は、スクロールバーとコンテンツボックスに必要とされているとオーバーフロー:隠されたが、HTMLで必要とされ、ボディ)
// Calculate site height to check if the content_box needs a scroll bar.
function calculate_site_height() {
// Set variables needed
var header = $('#header');
var footer = $('#footer');
var wrapper = $('#wrapper');
var content_box = $('#content_box');
var win = $(window);
// Set max max height. (+5 to get some margin from footer)
var max_height = win.height() - (header.height() + footer.height() +5);
// Check if the site height is bigger then viewport height. And then set max height to trigger the scrollbar if needed
if (wrapper.height() > win.height()) {
content_box.css('max-height', max_height + 'px');
}
// Set the max height of the content box without triggering the scrollbar
else {
content_box.css('max-height', max_height + 'px');
}
}
// Trigger the fuction calculate_site_height()
$(window).ready(calculate_site_height).resize(calculate_site_height);
値を変更する前に 'alert' *を入れてみましたか? –