Yoda 说:只有 JavaScript 才能解决的问题,你有。
http://jsfiddle.net/6MCLN/7/
CSS:
body {
background-color: #ff0000;
background: -ms-linear-gradient(top left, #d00 0%, #f33 50%, #611 100%);
background: linear-gradient(top left, #d00 0%, #f33 50%, #611 100%);
margin: 0;
overflow: hidden;
}
h1, nav, footer {
background-color: rgba(40, 40, 40, 0.4);
margin: 0;
padding: 10px;
}
section {
overflow-y: scroll;
padding: 10px;
}
前面提到的 JavaScript:
$(function () {
var thereIsNoTry = function () {
$('section').css({ height:
$(window).height() -
$('h1').height() -
$('nav').height() -
$('footer').height() -
parseInt($('h1').css('padding-top')) -
parseInt($('h1').css('padding-bottom')) -
parseInt($('nav').css('padding-top')) -
parseInt($('nav').css('padding-bottom')) -
parseInt($('footer').css('padding-top')) -
parseInt($('footer').css('padding-bottom')) -
parseInt($('section').css('padding-top')) -
parseInt($('section').css('padding-bottom'))
});
};
$(window).resize(thereIsNoTry);
thereIsNoTry();
});
编辑
对这种事情使用 javascript 的最大警告是,如果标题中的 DOM 更改而不调整窗口大小,则必须手动调整“部分”的大小。但是,以下附加 JavaScript 将使其在大多数情况下保持最新:
$(function () {
var thereIsNoTry = function () {
$('section').css({ height:
$(window).height() -
$('h1').height() -
$('nav').height() -
$('footer').height() -
parseInt($('h1').css('padding-top')) -
parseInt($('h1').css('padding-bottom')) -
parseInt($('nav').css('padding-top')) -
parseInt($('nav').css('padding-bottom')) -
parseInt($('footer').css('padding-top')) -
parseInt($('footer').css('padding-bottom')) -
parseInt($('section').css('padding-top')) -
parseInt($('section').css('padding-bottom'))
});
};
$(window).resize(thereIsNoTry);
thereIsNoTry();
//In case you're updating the DOM
$(document).ajaxSuccess(thereIsNoTry);
var oldAnimate = jQuery.fn.animate;
//In case the header can be animated
jQuery.fn.animate = function (properties, duration,
animation, easing) {
if (arguments.length == 4) {
return oldAnimate.call(this, properties,
duration, animation, easing);
}
if (arguments.length == 3) {
return oldAnimate.call(this, properties,
duration, animation);
}
if (arguments.length == 2 && typeof duration === 'object') {
var options = {
progress: function (animation, progress, remainingMs) {
if (duration.progress) {
duration.progress.call(this, animation,
progress, remainingMs);
}
thereIsNoTry();
}
}
var option = $.extend({}, duration, options);
return oldAnimate.call(this, properties, option);
}
if (arguments.length == 2) {
return oldAnimate.call(this, properties, {
duration: duration,
progress: thereIsNoTry
});
}
return oldAnimate.call(this, properties);
};
$('nav').animate({ height: '100px' }, { duration: 'slow' });
});
编辑
添加溢出:隐藏到 BODY 以防止“闪烁”垂直滚动条