Javascript/jQuery 运行太快 - 在重新加载页面时有效

标签 javascript jquery html

我正在建立一个流动网站。如果你在其中处理一些图像,那就太糟糕了。要么 IE 有问题,要么 FF 或 Chrome 有问题。

我又遇到问题了。基本上我是通过 javascript 在运行时构建网站。我首先设置宽度。之后我设置主容器的高度。 (否则图像会出现突破问题)。

现在我的问题是:如果我在本地执行网页,它可以在所有 3 个浏览器中运行。如果我在线执行它,它不会设置主容器(包含所有内容)的高度。 (varwrapperHeight = objLogo.offsetHeight;->返回0)

如果我刷新网页,一切看起来都正常,上面的行返回有效的高度... 我将它与一个简单的 jquery 脚本一起使用来翻转 div(但是这是在我的简单脚本之后)。

注意:我还在主体上使用了相当大的背景图像,这是在 css 中设置的。 第二个注意事项:它只发生在 Chrome 中...

代码:

<head>
    <title></title>
    <link rel="stylesheet" href="css/default.css" type="text/css" />
    <script type="text/javascript" src="scripts/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="scripts/jquery.quickflip.js"></script>
    <script type="text/javascript">
        $('document').ready(function(){
                            //getting inner width/height
            var windowWidth = window.innerWidth;
            var windowHeight = window.innerHeight;

                            //getting width for the main container
            var wrapperWidth = schermBreedte*0.641;

                            //getting width for left and right div in main container
            var totalWrapperWidth = wrapperWidth - 40;
            var widthLeft = totalWrapperWidth*0.345;
            var widthRight = totalWrapperWidth*0.655;

                            //getting elements
            var objOrange = document.getElementById('orange');
            var objGreen = document.getElementById('green');
            var objFliptabs = document.getElementById('flip-tabs');
            var objLeft = document.getElementById('left');
            var objRight = document.getElementById('right');
            var objContent = document.getElementById('content');
            var objLogo = document.getElementById('main_logo');
            var objV_line = document.getElementById('v_line');

                            //setting main container (objOrange & ObjGreen are 2 main containers used for the flip jquery script, they are actually placed above eachother (see jquery ref above and script beneath)
            objOrange.style.width = wrapperWidth + "px";
            objGreen.style.width = wrapperWidth + "px";
            objFliptabs.style.width = wrapperWidth + "px";

                            //setting the left & right div
            objLeft.style.width = widthLeft + "px";
            objRight.style.width = widthRight + "px";

                            //this logo is placed in the left div. The actual main container height is determined by getting the height of this logo after setting the width
            objLogo.style.width = (widthLeft-20)+"px";

                            //the right div is splitted into two pieces: a top which contains 6 images and a bottom which contains 1 image, the objectContent refers to the bottom as the top height is set dynamically by setting the width of the 6 images in %
            objContent.style.width = widthRight  + "px";

                            //getting the height for the main container by getting the height of the image (we've set the width of the image earlier - see above - so that it is scaled proportionally)
            var wrapperHeight =  objLogo.offsetHeight;
                            //setting the height of the main containers
            objOrange.style.height = wrapperHeight + "px";
            objGreen.style.height = wrapperHeight + "px";

                           //setting the height of a 1px line next to the left div
            objV_line.style.height = 100 + "%"

                            //getting the height of the content div (=2nd and bottom div of the right div)
            var contentHeight = wrapperHeight*0.445;

                            //setting the height
            objContent.style.height = contentHeight + "px";

                            //another line
            var length = parseInt(document.getElementsByTagName("div")["right"].offsetWidth) - 20;
            var line = document.getElementById('hor_lijn');
            line.style.width = lengte + "px";   

            $('#flip-container').quickFlip();
            $('#flip-navigation li a').each(function(){
                $(this).click(function(){
                    $('#flip-navigation li').each(function(){
                        $(this).removeClass('selected');
                    });
                    $(this).parent().addClass('selected');
                    var flipid=$(this).attr('id').substr(4);
                    $('#flip-container').quickFlipper({ }, flipid, 1);
                    return false;
                });
            });


        });
    </script>
</head>

最佳答案

看起来您需要等待运行该代码,直到加载图像,以便知道它们的大小,从而可以正确计算高度。

尝试使用这个 jQuery 插件:

https://github.com/desandro/imagesloaded

$(document).ready(function() {
    $('#my-image-container').imagesLoaded(function() {
        /* your code here */
    });
});

关于Javascript/jQuery 运行太快 - 在重新加载页面时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7417245/

相关文章:

javascript - 如何在javascript for循环中打印大括号

javascript - 单击按钮时用用户选择的图像填充 div

c# - ASP.NET MVC Html Helper 扩展和呈现它们所需的 "include"s

javascript - 不同设备的 body 定心

javascript - X 和 Y 在带有偏移的 Canvas 中不匹配

html - 在段落下方右对齐显示按钮

css - 高度自动在许多 div 中不起作用

javascript - 如何在 WebGL 中实现这种类似隧道的动画?

用于固定菜单位置的 Javascript Scrolltop

javascript - 在弹出确认对话框之前执行某些操作