如何在 Masonry 调整大小之前执行代码

发布于 2024-12-22 09:00:08 字数 1082 浏览 3 评论 0原文

我使用 Masonry 进行布局。当浏览器窗口大小调整时,我想在 Masonry 重做布局之前执行一些计算。

到目前为止,我已经能够通过修改 jquery.masonry.js 文件并向选项添加回调函数来做到这一点。但我更愿意在不修改砌体代码的情况下完成此操作。

添加的选项如下(masonry init):

$container.masonry({
    itemSelector  : '.grid-element'
    , preResizeCallback : doPreResize
    , isResizable : true
});

然后在 doPreResize 中执行我需要执行的操作。 .js 文件中的修改位于 resize 函数中(版本 v2.0.110927 中第 293 行附近):

resize : function() {
  //added callback to be called before doing resizing-related code
  if (this.options.preResizeCallback)
  {
      this.options.preResizeCallback(this.element);
  }
  //rest of code

我想要做的是使用 isRessized 初始化 Masonry > 选项设置为false,然后半手动触发调整大小,同时保留智能调整大小事件。

是否可以设置“智能调整大小”事件来调用诸如 resizeLayout 之类的函数?

function resizeLayout(event) {
    //do precalculations

    //tell masonry to reorganize layout
    $container.masonry();
}

我缺少的是如何设置调整大小事件,使其不会不断触发,而是以与 Masonry 相同的方式在 isResizestrue 时完成。

I'm using Masonry for layout. When the browser's window resizes I'd like to perform some calculations before Masonry redoes the layout.

Until now I've been able to do this by modifying the jquery.masonry.js file and add a callback function to the options. But I'd prefer to do it without modifying the masonry code.

The option added is as follows (masonry init):

$container.masonry({
    itemSelector  : '.grid-element'
    , preResizeCallback : doPreResize
    , isResizable : true
});

and then in doPreResize do whatever it is I need to do. The modification in the .js file is in the resize function (near line 293 in version v2.0.110927):

resize : function() {
  //added callback to be called before doing resizing-related code
  if (this.options.preResizeCallback)
  {
      this.options.preResizeCallback(this.element);
  }
  //rest of code

What I'd like to do is to initialize Masonry with the isResizable option set to false and then trigger the resize semi-manually, while keeping the smart resize event.

Is it possible to set up a 'smart resize' event to call a function such as resizeLayout?

function resizeLayout(event) {
    //do precalculations

    //tell masonry to reorganize layout
    $container.masonry();
}

What I'm missing is how to setup the resize event such that it isn't triggered constantly, but instead be done the same way as Masonry does when isResizable is true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃不饱 2024-12-29 09:00:08

我可以像这样设置事件:

jQuery(window).bind( 'smartresize.masonry', function(){
    $container.masonry();
});

但这会失去 Masonry 的优化,即只有当适合容器的列数发生变化时才会重新排列砖块。相反,如果我使用,

jQuery(window).bind( 'smartresize.masonry', function(){
    $container.masonry("resize");
});

我最终会使用一个未记录的内部函数。

I can set up the event like this:

jQuery(window).bind( 'smartresize.masonry', function(){
    $container.masonry();
});

But that loses Masonry's optimization where the bricks are only rearranged if the number of columns that fit in the container change. If instead I use

jQuery(window).bind( 'smartresize.masonry', function(){
    $container.masonry("resize");
});

I end up using an internal function that isn't documented.

晨曦慕雪 2024-12-29 09:00:08

Masonry 可以选择绑定/取消绑定到调整大小事件。

我遇到了同样的问题,但通过将砌体初始化为“未绑定”到调整大小事件来修复它,然后编写一个调整大小函数,以相同的方式调用名为砌体的函数,但在我想要首先执行的代码之后。

$container.masonry({
    isResizeBound: false
});

$(window).bind('resize', function() {
    someFunction();
    $continer.masonry({
        isResizeBound: false
    });
});

Masonry has the option to bind/unbind to the resize event.

I had the same problem, but fixed it by initializing masonry 'unbound' to the resize event, and then writing a resize function that called that called masonry in the same fashion, but after the code I wanted executed first.

$container.masonry({
    isResizeBound: false
});

$(window).bind('resize', function() {
    someFunction();
    $continer.masonry({
        isResizeBound: false
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文