简体   繁体   English

图像完成异步加载后使用Gridify

[英]Using Gridify after images finish loading ascyncronously

I am building an app in Backbone.js and using gridify to arrange my photos in a grid fashion. 我正在Backbone.js中构建一个应用程序,并使用gridify以网格方式排列我的照片。 The problem is that the images get loaded asynchronously and this seems to be causing gridify issues. 问题是图像异步加载,这似乎导致网格化问题。 Is there a way to call the gridify function after all ascyn call are finished? 在所有ascyn调用完成之后,是否可以调用gridify函数? I tried placing the call in post render, but this does not seem to help. 我尝试将呼叫放置在后期渲染中,但这似乎无济于事。 Here is my code: 这是我的代码:

In TemplateContext: 在TemplateContext中:

        var images = [];
        this.model.get('images').each(function (imageModel) {
            var image = imageModel.toJSON()                
            images.push(image);
        }, this);
        return {               
            images: images,
            view:this
        }

In Post Render: 在后期渲染中:

 var options =
      {
          srcNode: 'img',             // grid items (class, node)
          margin: '20px',             // margin in pixel, default: 0px
          width: '250px',             // grid item width in pixel, default: 220px
          max_width: '',              // dynamic gird item width if specified, (pixel)
          resizable: true,           // re-layout if window resize
          transition: 'all 0.5s ease' // support transition for CSS3, default: all 0.5s ease
                    }

$('.grid').gridify(options);

and in my template: 在我的模板中:

<div id="activity" class = "grid" style="border: 1px solid black;">
    {{#if images }}
        {{#each images}}
                <img src ="?q=myApp/api/get_images&imgId={{id}}&imgSize=medium" >
        {{/each}}
    {{else}}
        No images have been uploaded
    {{/if}}
</div>

This causes all the images to be stacked on top of each other. 这导致所有图像堆叠在一起。 I think it's because gridify doesn't know what the dimensions should be due to the fact the images get loaded after gridify is called. 我认为这是因为gridify不知道尺寸应该是多少,这是因为调用gridify之后加载了图像。 If I just use static images in my template, it works fine. 如果我仅在模板中使用静态图像,则效果很好。 Anyone have any suggestions on how to handle this either in backbone or with gridify? 任何人对如何在主干网中或通过gridify处理此问题有任何建议?

thanks jason 谢谢杰森

From the docs : 从文档:

http://mattlayton.co.uk/gridify/#examples http://mattlayton.co.uk/gridify/#examples

When images are part of your content you will want to call Gridify after all images are loaded so that the true image dimentions can be considered. 当图像成为内容的一部分时,您将需要在加载所有图像之后调用Gridify,以便可以考虑真正的图像尺寸。 Your code should look something like this: 您的代码应如下所示:

$(window).load(function() {
  $('.grid').gridify();
});

In case the images are loaded dynamically : 如果图像是动态加载的:

https://stackoverflow.com/a/2311794/566092 https://stackoverflow.com/a/2311794/566092

https://stackoverflow.com/a/5424432/566092 https://stackoverflow.com/a/5424432/566092

https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=how+to+know+all+images+are+loaded https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=how+to+know+all+images+已加载

What I ended up doing was creating a div with the given dimensions. 我最终要做的是创建具有给定尺寸的div。 Then I placed the images in the div and using gridify on the div itself. 然后我将图像放在div中,并在div本身上使用gridify。 That way, gidify does its thing to the divs and the images can load later. 这样,gidify就对div进行处理,以后可以加载图像。

The problem due to CSS3 transition set to 0.5 for all. 由于CSS3过渡而导致的所有问题均设置为0.5。 I have removed width and height from the animation. 我从动画中删除了宽度和高度。

It has already fixed in the latest sourcecode https://github.com/hongkhanh/gridify 它已经在最新的源代码https://github.com/hongkhanh/gridify中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM