Masonry JS Overlapping Items

后端 未结 7 1504
庸人自扰
庸人自扰 2021-01-30 14:37

I have a very strange problem in here: [Referral Link Removed]. The first row product items overlapped with the items in the second row.

The masonry it

7条回答
  •  青春惊慌失措
    2021-01-30 15:24

    The problem is your images. By the time masonry is called, your images haven't loaded. So it's assuming the height of your elements WITHOUT the height of the image being factored in.

    If you refresh your screen after the images are already cached, you'll see that it loads correctly. If you then clear cache and refresh, you'll see they overlap again.

    Four Five options:

    • Wait until the images are finished loading (there are plugins that you can wait until all images inside a certain div are loaded, for example)
    • Wait for the load event instead of the ready event. Instead of using jQuery(function($){ use jQuery(window).on('load', function(){ var $ = jQuery; and you'll see the results.
    • Re-apply masonry after the images load (Don't like this one... you'd see flicker)
    • My favorite, don't use masonry here! Disable JS on your page and look at the layout. It's what you want. All the divs are even heights and even widths. There's not really a reason to use masonry here. Just float your elements and let them display in the grid naturally.
    • EDIT: another option. Specify a height of the divs so the height doesn't depend on the images it's loading.

提交回复
热议问题