javascript - 加载所有媒体后运行 jQuery 函数

标签 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)
});

不幸的是 (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/

相关文章:

javascript - 如何使用 javaScript 重新加载页面一次

javascript - 新窗口弹出窗口在 Chrome 中为空白,但在 IE 和 Firefox 中有效

javascript - 将纯 javascript 重写为 jQuery

C++ 等待所有线程完成

javascript - 动态创建的元素上的事件绑定(bind)?

javascript - 制作轮播,点击功能动画后无法滚动(JSFiddle 链接已更新)

javascript - 如何使用 fotorama 默认加载所有拇指

javascript - MVC 中的 Ajax 表单验证

javascript - 关于 $.load() 的一个奇怪问题

javascript - 浏览器中撤消的事件监听器