页面加载图像大小/位置脚本随机不运行

时间:2013-06-20 04:01:03

标签: jquery html image object center

我有一个jQuery脚本,它将我的徽标集中在屏幕中间。它设置为在页面加载和窗口大小调整上运行。它每次在窗口调整大小时都有效,在10次加载时有9次。但有一段时间它完全无法在负载下运行。

http://vividconference.tv/beta/网站和相关脚本:

$(function(){
    $(window).resize(function(){
       var height = $('#logo').height();
       $('#logo').css('margin-top', + height / -2 + 'px');
    }).resize();
});

1 个答案:

答案 0 :(得分:0)

不要忘记,如果尚未加载图片,则高度为0

让我们试试这个更全面的报道:

function redraw() {
    var height = $('#logo').height();
    $('#logo').css('margin-top', (height / -2) + 'px');
}

$(document).ready(function(){
    $(window).load(redraw).resize(redraw);
    $('#logo, #logo img').ready(redraw);
    redraw();
});
相关问题