如何在 Masonry 调整大小之前执行代码
我使用 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 相同的方式在 isResizes
为 true
时完成。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以像这样设置事件:
但这会失去 Masonry 的优化,即只有当适合容器的列数发生变化时才会重新排列砖块。相反,如果我使用,
我最终会使用一个未记录的内部函数。
I can set up the event like this:
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
I end up using an internal function that isn't documented.
Masonry 可以选择绑定/取消绑定到调整大小事件。
我遇到了同样的问题,但通过将砌体初始化为“未绑定”到调整大小事件来修复它,然后编写一个调整大小函数,以相同的方式调用名为砌体的函数,但在我想要首先执行的代码之后。
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.