我有以下JQuery代码:
$(document).ready(function () {
var $containerHeight = $(window).height();
if ($containerHeight <= 818) {
$('.footer').css({
position: 'static',
bottom: 'auto',
left: 'auto'
});
}
if ($containerHeight > 819) {
$('.footer').css({
position: 'absolute',
bottom: '3px',
left: '0px'
});
}
});
唯一的问题是,这只在浏览器第一次加载时才起作用,我希望在调整窗口大小时也检查containerHeight
?
有什么想法吗?
发布于 2012-03-23 03:06:24
将javascript移动到一个函数中,然后将该函数绑定到窗口大小调整。
$(document).ready(function () {
updateContainer();
$(window).resize(function() {
updateContainer();
});
});
function updateContainer() {
var $containerHeight = $(window).height();
if ($containerHeight <= 818) {
$('.footer').css({
position: 'static',
bottom: 'auto',
left: 'auto'
});
}
if ($containerHeight > 819) {
$('.footer').css({
position: 'absolute',
bottom: '3px',
left: '0px'
});
}
}
发布于 2015-09-04 16:43:32
尝试此解决方案。仅在页面加载后,在以预定义的resizeDelay
调整窗口大小时激发。
$(document).ready(function()
{
var resizeDelay = 200;
var doResize = true;
var resizer = function () {
if (doResize) {
//your code that needs to be executed goes here
doResize = false;
}
};
var resizerInterval = setInterval(resizer, resizeDelay);
resizer();
$(window).resize(function() {
doResize = true;
});
});
发布于 2012-03-23 03:06:55
https://stackoverflow.com/questions/9828831
复制相似问题