Pergunta

I know this has been asked million times. I look at other topics for solution and so far I can't fix it.

I am using Masonry to develop grid system, like pinterest and google images. It works fine. The images load properly on the Firefox but in Chrome/Safari, they are overlapping until I refresh the page. Which is weird because images load fine on my iphone(until I refresh the page and they overlap).

Also I just noticed an error Firebug gives me in the imagesloaded script. This line.

  } else if ( typeof obj.length === 'number' ) {

Here is my page setup.

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>

    <title>Home page</title>
    <meta name="description" content="description here" />
    <link rel="stylesheet" href="css/global.css" media="screen" />
    <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
    <!-- external jQuery file to fall back on !-->
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

    <script src="js/masonry.pkgd.min.js"></script>
    <script src="js/imagesloaded.pkgd.js"></script>
    <script>
        var container = document.querySelector('#container');
        var msnry;
        // initialize Masonry after all images have loaded
        imagesLoaded( container, function() {
          msnry = new Masonry( container );
        });
    </script>
</head>
<body>

    <section id="container"  class="js-masonry" data-masonry-options='{ "columnWidth": 30, "itemSelector": ".item" }'>
        <h1 class="hidden">Main Container</h1>
        <div class="item">
            <img src="images/long.jpg">
        </div>
        <div class="item">
            <img src="images/small.jpg">
        </div>
        <div class="item">
            <img src="images/long.jpg">
        </div>
        <div class="item">
            <img src="images/small.jpg">
        </div>
        <div class="item">
            <img src="images/small.jpg">
        </div>
        <div class="item">
            <img src="images/long.jpg">
        </div>
        <div class="item">
            <img src="images/small.jpg"> 
        </div>
        <div class="item">
            <img src="images/long.jpg">
        </div>
        <div class="item">
            <img src="images/small.jpg">
        </div>
    </section>

</body>
</html>

So all in all, how do I fix this overlapping issue on refresh page?

ps. I should mention that in browsers, the images will overlap only when I refresh using ctrl + f5. Normal refresh page doesn't stack up the images.

Foi útil?

Solução

Alright the issue has been solved.

I added the following script and now it reloads fine.

<script>
    $(window).load(function() {  
        var $container = $('#container');
        $container.masonry({
            itemSelector: '.item'
        });
    }); 
</script>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top