【发布时间】:2012-11-16 16:43:48
【问题描述】:
我想知道哪一个是正确的运行 js 代码,它根据窗口高度计算垂直菜单的高度并按时设置,不迟到,不早。
我正在使用document.ready,但它并没有真正帮助我解决这个问题,有时它没有设置,我必须重新加载页面,然后它才能工作,但不是在第一次加载时。
如何解决这个问题?
这是我的代码:
$(document).ready(function(){
var winh = document.body.clientHeight;
var footer = document.getElementById('footer').offsetHeight;
document.getElementById('sidebar').style.height = winh - 5/2*footer + 'px';
document.getElementById('sidebar').style.marginBottom = footer + 'px';
$(window).resize(function(){
var winh = document.body.clientHeight;
var footer = document.getElementById('footer').offsetHeight;
document.getElementById('sidebar').style.height = winh - 5/2*footer + 'px';
document.getElementById('sidebar').style.marginBottom = footer + 'px';
});
});
【问题讨论】:
-
您是否在页面加载之前调整大小?你的 window.resize 可以在 ready 设置之前被调用。
-
@AJak 准备和调整大小做同样的事情,
-
@AJak 虽然例子中的tabbing是错误的,但是在
ready函数中添加了resize事件,所以不能在前面的代码之前触发。
标签: javascript jquery onload document-ready