您看到脚本中的错字了吗?
var columsheight = $('.colums').height();
根据您的标记,应该是:
var columsheight = $('.columns').height();
哦。哎哟。更重要的是……您的整个 jQ 窗口大小调整功能状况不佳。您的 resize() 函数在完成其余处理之前关闭:
$(window).resize(function(){
$('.container').css('height', $(window).height() - 200);
var columsheight = $('.columns').height();
var containerheight = $('.container').height();
if (containerheight < columsheight){
$('.container').css("box-shadow","inset 0px -13px 8px -10px #868686");
};
}).resize();
更好的是,为了澄清你在做什么,并减少你的第一次 resize() 没有被错过的机会,你可能应该做这样的事情。
function resizeContainers(){
$('.container').css('height', $(window).height() - 200);
var columsheight = $('.columns').height();
// open your browser's console, and watch as your code executes
// you should see a line written every time it goes through this function
console.log(".columns height is: " + columsheight);
var containerheight = $('.container').height();
console.log("containerheight height is: " + containerheight);
if (containerheight < columsheight) {
$('.container').css("box-shadow","inset 0px -13px 8px -10px #868686");
}
}
$(window).resize(resizeContainers);
resizeContainers();
这样,您可以在需要时独立调用该函数,而无需触发全窗口 resize() 事件。 window.resize() 事件特别敏感......它会被很多不同的东西触发,如果你在移动设备上使用它会变得更糟,因为一些移动浏览器将方向变化解释为 window.resize()。
好的......现在水已经变得浑浊了,我已经整理了一个工作示例:
$(function () {
// create your method for checking the resize
function resizeContainers() {
// get a reference to the .container element and the .columns element
var $container = $('.container');
var $cols = $('.columns');
// set the height on $container
$container.css('height', $(window).height() - 200);
// THis is just here so you can see, as you resize the frame,
// that this is testing the sizes
$("#output").html("$cols.height() : " + $cols.height() + "\n$container.height() : " + $container.height());
// Now compare the height of the $cols item to the $container height
if ($cols.height() > $container.height()) {
$container.css("box-shadow", "inset 0px -13px 8px -10px #868686");
} else {
// this will remove the box shadow when the content does not exceed
// the container height
$container.css("box-shadow","");
}
}
// Now, tell the window object to listen for resize events and call resizeContainers
$(window).resize(resizeContainers);
// Call it manually once
resizeContainers();
});
你可以在http://jsfiddle.net/mori57/wV7Vt/看到这个实践
观察输出 div 并在输出窗口周围拖动框架栏以观察值的变化。