繁体   English   中英

document.ready vs document.onLoad

[英]document.ready vs document.onLoad

我想知道哪一个是正确的运行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';
     });
});

准备

当文档准备就绪时运行代码,这意味着DOM已加载 - 但不是像图像那样。 如果图像会影响高度和宽度,并且图像标签没有设置宽度和高度,那么就不能选择就绪 - 否则它可能就是。

负载

这包括图像 - 所以一切都将被加载。 这意味着它会稍后点火。

var calculateSize = 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';
}

$(document).ready(function(){
    calculateSize();

     $(window).resize(calculateSize);
});

window.onload = calculateSize ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM