wow*_*ick 3 javascript jquery events resize
我有一个简单的jQuery脚本,即使内容不够长,也会将页脚推到页面底部:
$(document).ready(function(){
positionFooter();
function positionFooter(){
//get the div's padding
var padding_top = $("#footer").css("padding-top").replace("px", "");
//get the current page height - the padding of the footer
var page_height = $(document.body).height() - padding_top;
var window_height = $(window).height();
//calculate the difference between page and window height
var difference = window_height - page_height;
if (difference < 0)
difference = 0;
//write the changes to the div
$("#footer").css({
padding: difference + "px 0 0 0"
})
}
//re-run the function if browser window is resized
$(window).resize(positionFooter)
});
Run Code Online (Sandbox Code Playgroud)
不幸的是(文档).ready触发器是提前的,并且不考虑图像和字体加载,因此计算的值是不正确的.是否有适合此类任务的触发器?
我正在构建的网站正在使用Disqus fot评论,这也会再次改变页面长度,但我认为我需要从Disqus脚本回调来考虑这一变化.
尝试 $(window).load()
您可能还想查看有关不同文档加载事件的jQuery文档:http://api.jquery.com/category/events/document-loading/
希望能帮助到你