我有一个简单的 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)
});
不幸的是 (document).ready 触发器太早了,没有考虑图像和字体加载,所以计算出的值是不正确的。是否有适合此类任务的触发器?
此外,我正在构建的网站正在使用 Disqus fot 评论,它也会再次更改页面长度,但我认为我需要 Disqus 脚本的回调来考虑该更改。
最佳答案
试试 $(window).load()
您可能还想查看有关不同文档加载事件的 jQuery 文档:http://api.jquery.com/category/events/document-loading/
希望对你有帮助
关于javascript - 加载所有媒体后运行 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6308985/