jquery masonry 在 chrome/safari 中中断(堆叠图像),但仅在第一次加载时
似乎当我尝试加载页面时,所有图像都堆叠在一起。但是,如果您单击一个链接,将您带到同一页面(例如主页链接),那么砌体就会启动。所以我认为砌体加载得太早,就像在 jquery 准备好页面或其他东西之前一样。
这是我的 jquery 调用:
$(document).ready(function(){
$('#image_roll_container').masonry({
itemSelector: '.box'
});
....
这是有问题的页面:
它在 Firefox 和 IE8 中工作得很好。
It seems that when I try to load the page, all the images are stacked on top of one another. But if you were to click a link which takes you to the same page (like the home link) then masonry kicks in. So I think masonry is loading too early, like before jquery readies the page or something.
Here my jquery call:
$(document).ready(function(){
$('#image_roll_container').masonry({
itemSelector: '.box'
});
....
Here's the page in question:
it works just fine in firefox and IE8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我已经通过以下调整成功解决了这个问题:
I've managed to fix this problem with the following tweak:
看起来我需要一个名为 imagesLoaded 的插件,以便 Monsry 脚本能够与 chrome 和 safari 等正常工作
looks like I needed a plugin called imagesLoaded in order for the Monsry script to work properly with the likes of chrome and safari
尝试了该线程中建议的所有内容,但没有任何效果,然后发现了这个:
现在工作正常,在这里找到它:
https://github.com/desandro/masonry/issues/35
原帖作者:
https://github.com/desandro
Tried everything suggested in this thread, nothing worked, then found this:
Works fine now, found it here:
https://github.com/desandro/masonry/issues/35
Original post author:
https://github.com/desandro
您对 imagesLoaded 的看法是正确的。它在 Firefox 中工作正常,但在 Chrome/Safari 中堆叠。
这是链接https://masonry.desandro.com/layout.html#imagesloaded
代码:
You are correct about the imagesLoaded. It was working fine in Firefox but stacking in Chrome/Safari.
Here is the link https://masonry.desandro.com/layout.html#imagesloaded
Code:
我最近遇到了这个问题。为了解决这个问题,我利用了 img 的宽度和高度属性。问题自行解决了。
I recently came across this issue. To fix it, I utilized the img width and height attributes. The issue resolved itself.
另一种方法是,如果您知道图像高度,则在加载 Masonry 之前在 CSS 中分配它们,那么布局比等待图像更快。例如,如果所有图像尺寸相同,则此方法有效。然后,您的网站仍然可以在慢速连接(例如移动设备)上快速加载。
我在这里发布了一些替代方法的脚本:
http://instancia.net/loading-jquery-masonry-on-mobile/
如果您使用此脚本,请编辑数字以匹配您的数字。
Another way, if you know the image heights, is to assign them in the CSS before you load Masonry, then the layout is faster than waiting for the images. This method works if, for example, all your images are the same size. Then your site will still load quickly on slow connections, like mobile.
I posted a bit of script for alternative method here:
http://instancia.net/loading-jquery-masonry-on-mobile/
If you use this script, edit the numbers to match yours.
在 Firefox 和我的 iPad 2 上,砌体工作正常,但在 OS X 上的 chrome 和 safari 中,元素在页面加载时重叠/堆叠,直到发生窗口大小调整。在深入研究 jquery.masonry.js 的代码后,我发现我可以在创建砌体后立即触发 resize() ,以便所有元素正确重新排列。现在一切正常。
所有其他解决方案:(窗口).load,设置宽度和宽度CSS 中的高度和 img 属性等对我来说不起作用。
On Firefox and on my iPad 2 masonry was working fine but in chrome and safari on OS X the elements were overlapping/stacking on page load and until a window resize even happen. After digging in the code of jquery.masonry.js I found that I can trigger a resize() right after creating the masonry so that all elements rearrange properly. Now everything is working fine.
all of the other solutions: (window).load, setting width & height in CSS and on img attributes, etc, just didn't work for me.
正如詹妮弗所说,它需要在这些浏览器中正确显示高度。我使用 php 的 getimagesize() 函数来获取图像的高度和宽度。现在工作完美。
It needs heights in these browsers to display correctly like Jennifer said. I use php's getimagesize() function to get the height and width of the images. Works perfectly now.
如果使用 $('img').load(function() F5(refesh) => 错误
新方法:
if use $('img').load(function() F5(refesh) => error
New Methods:
“load”事件将为 DOM 中的每个图像触发,这有点过分了。当 DOM 中的最后一个图像加载时,您需要更新砌体的布局。这是代码:
The "load" event will trigger for every image in the DOM, this is overkill. You need to update the layout of the masonry when the last image in the DOM loads. Here is the code:
我遇到了所描述的相反问题:第一次加载在 Mac OS X Safari 中工作正常,但是使用所有新项目更改网格导致它们全部堆叠在左上角。此外,等待就绪事件并设置 CSS 高度和高度。我们元素上的宽度并没有解决这个问题。
在我们的网站上,我们在砌体网格中显示数据类别,并且一次仅显示一个类别。用户可以随时切换类别并触发 AJAX 请求来加载新数据。在最新的 Safari(9.1、10)和 Chrome 等浏览器中,我们可以在更改类别以交换所有新元素时简单地执行此操作:
但是,在某些版本的 Safari 中这不起作用,我们必须这样做:
因为我没有时间追踪可能受此错误影响的每个浏览器版本,所以我对所有浏览器都改用后一种方法。
I had the reverse problem as described: first load worked fine in Mac OS X Safari, but changing the grid with all new items caused them all to stack in the top left corner. Further, waiting for ready event and setting CSS height & width on our elements didn't fix it.
On our site, we have categories of data that display in the masonry grid, and only one category shows at a time. A user could switch the category at any time and trigger an AJAX request to load in the new data. In latest Safari (9.1, 10) and browsers like Chrome, we could simply do this when changing the category to swap in all new elements:
However, in some versions of Safari that wouldn't work, and we had to do this instead:
Because I don't have the time to track down every browser version that might be affected by this bug, I switched to the latter method for all browsers.