Minimalist dependancy free Masonry layout library

Overview

MiniMasonry.js

Minimalist dependency free Masonry layout library

MiniMasonry is a lightweight dependency free Masonry layout. It will compute elements position in JavaScript and update their positions using CSS's transform attribute. This means positioning does not trigger browser layout and use the device's GPU. This also allows CSS animation during element positioning.

MiniMasonry is responsive, you give it a target width and it will adjust columns number and elements width. MiniMasonry will increase element width (until another column can fit in the layout) but will never reduce the target width.

Installation

You can install MiniMasonry by cloning the repository, downloading the build type (minimasonry.min.js / minimasonry.esm.js) or using npm :

npm install minimasonry

Then, include the minified version of MiniMasonry.js on your website :

<script src="node_modules/minimasonry/build/minimasonry.min.js"></script>

or use it via ESM :

import MiniMasonry from "minimasonry";

Usage

To use MiniMasonry you should have a container relatively positioned with your elements as children. Those children elements must be absolutely positioned.

Then you can initialise MiniMasonry :

var masonry = new MiniMasonry({
    container: '.masonry_transition'
});

Parameters

Here is the list of available parameters :

Name Default value Description
baseWidth (int) 255 Target width of elements.
container (string|HTMLElement) Null Container's selector or element. Required
gutter (int) 10 Width / height of gutter between elements. Use gutterX / gutterY to set different values.
gutterX (int) null Width of gutter between elements. Need gutterY to work, fallback to gutter.
gutterY (int) null Height of gutter between elements. Need gutterX to work, fallback to gutter.
minify (boolean) true Whether or not MiniMasonry places elements on the shortest column or keeps exact order of the list.
surroundingGutter (boolean) true Set left gutter on first columns and right gutter on last.
ultimateGutter (int) 5 Gutter applied when only 1 column can be displayed.
direction (string) "ltr" Sorting direction, "ltr" or "rtl".
wedge (boolean) false False will start to sort from center, true will start from left or right according to direction parameter.

API

Here is the list of available APIs :

Name Description
layout() If list has changed, trigger a relayout of the masonry.
destroy() Remove the resize listener and set back container as it was before initialization.

MiniMasonry will add a "resize" event listener on the window to redraw the masonry on window resize. This listener is throttled to 66ms (15fps).

Example

See website

codepen

License

MIT

Made by Spope

Comments
  • Accept a DOM element as `container` option

    Accept a DOM element as `container` option

    I want to be able to provide the DOM element of the container directly instead of through a selector. In my use-case there is no unique selector for the container.

    opened by duzun 4
  • Restore the value of gutterX when the screen is normal

    Restore the value of gutterX when the screen is normal

    When this happens, gutterX is modified and never recovers

    if (this.getCount() == 1) {
        // gutterX is modified and will never be restored
        this.conf.gutterX = this.conf.ultimateGutter;
        this._count = 1;
    }
    
    if (this._width < (this.conf.baseWidth + (2 * this.conf.gutterX))) {
        // gutterX is modified and will never be restored
        this.conf.gutterX = 0;
    }
    
    opened by lihaosen-222 3
  • Adding a destroy method to properly remove instances.

    Adding a destroy method to properly remove instances.

    I want to use minimasonry + pjax to realize the layout without refreshing the page, but when I encounter this, I will always add the resize event. I mentioned issues in pjax, and the other party suggested to delete the event addition when executing, but I can't remove the resize event.

    Every time you jump to a page, you need to perform minimasonry once, but the old page events will remain in the address. I don't think the chance of resolution is very high, but I still want to ask for advice.

    mini

    
    const postItemLayout = className => {
      const elemItem = document.querySelectorAll(className);
    
      if (elemItem.length > 0) {
    
        elemItem.forEach((ele, i) => {
          ele.setAttribute('data-minimasonry', `miniMasonry${i}`);
          new MiniMasonry({
            container: `[data-minimasonry=miniMasonry${i}]`,
            gutter: 14,
            surroundingGutter: false,
            ultimateGutter: 14
          })
        })
    
      }
    }
    
    //Every successful page Jump will be executed
    document.addEventListener("pjax:success", function () {
      
      postItemLayout('.post-item-wrap');
      
    });
    
    //This will only be executed once
    document.addEventListener("DOMContentLoaded", function () {
      pjax = new Pjax({
        elements: ["a"],
        selectors: ["main"],
        cacheBust: true
      });
      
      postItemLayout('.post-item-wrap');
      
    });
    
    opened by awnawnawn 3
  • When the item has an image, it will collapse

    When the item has an image, it will collapse

    When the image in the item is loaded slowly, the MiniMasonry will directly calculate the height of the current entity instead of the image, which leads to the inaccurate height of the container

    Snipaste_2021-04-11_15-03-34

    Effect link refresh more times

    opened by awnawnawn 2
  • basewidth appears to have no effect.

    basewidth appears to have no effect.

    Hello.

    First, thanks for the nice tool.

    I thought you might want to know that the "basewidth" parameter appears to have no effect. Using your CodePen, you can recreate the issue there. Add the basewidth parameter and change the value to any number and nothing is different in the display.

    opened by DarkMG73 2
  • Multiple instances

    Multiple instances

    Hi,

    thank you for your little and great library..

    I have one quick question, how would I add multiple masonry grids (each with its own set of rules.. gutter, width etc..) and one thing,, I can only use one class (for ex. .masonry) but I guess I need to document.querySelectorAll('.masonry') iterate each node right?

    Thanks

    opened by mkdizajn 2
  • conserve gutter with Mobile

    conserve gutter with Mobile

    Hello, Thanks for your library, she's light and does her job perfectly !

    There's a little bug when you're viewport is less than 530px

    // l62
    this._count = Math.floor((this._width - this._gutter) / (this.conf.baseWidth + this.conf.gutter));
    
    // 93
    this._columns[shortest]  += this._sizes[index] + this.conf.gutter;//margin-bottom
    

    you should prefer

    // l62
    this._count = Math.floor((this._width - this._gutter) / (this.conf.baseWidth + this._gutter));
    
    
    // 93
    this._columns[shortest]  += this._sizes[index] + this._gutter;//margin-bottom
    
    opened by lhapaipai 2
  • Fix typos in README.md

    Fix typos in README.md

    Hello Scope from Bordeaux! Thank you for this project, this is Michael from https://bestofjs.org/.

    I was about to add the project to Best of JS (under the tag "layout") when I noticed a typo about "dependency" word (well actually it's my browser who showed me the typo in the input field where I was pasting project's description 😅).

    While I was there, Google suggested me some other corrections I'm including in the PR.

    Feel free to merge or ignore because I'm also French and I may be wrong!

    opened by michaelrambeau 1
  • Why should I not just only use native CSS Flexbox?

    Why should I not just only use native CSS Flexbox?

    Hi Guys,

    I spent little minutes just for the sake to understand what do you want to achieve with your project. I mean it might turn out I do not really understand what is going on here and why I should use your project and not just only Native Browser CSS Flex Box Layouts? If it turns out to be the case, would you might be so kind and explain more in your documentation what the benefit of your project is?

    Thanks and best regards

    Tim.

    opened by TimSusa 1
  • problem when the height of the brick depends on its width

    problem when the height of the brick depends on its width

    Hello ! Another little suggestion...

    In my case, the height of each brick depends on its width (an image with a ratio). It's therefore important to define the new width of each brick before calculating its height (when you construct the array _sizes.) ?

    l-87

    for (var k = 0;k< children.length; k++) {
        children[k].style.width = Math.round(width) + 'px';
        this._sizes[k] = children[k].clientHeight;
    }
    

    have a good day

    opened by lhapaipai 1
  • Useless console.log message in the ESM version.

    Useless console.log message in the ESM version.

    https://github.com/Spope/MiniMasonry.js/blob/c4b5f7f8de818323392b96fb42fc2e7d14ed129f/build/minimasonry.esm.js#L45

    Can we get rid of this? It spams the console.

    opened by Kaperstone 0
  • Changing window size or zoom level collapses gutters

    Changing window size or zoom level collapses gutters

    Hey there, thanks for such a clean, simple library!

    Platform

    macOS 12.6, Brave Version 1.45.113 Chromium: 107.0.5304.62 (Official Build) (arm64)

    Setup

    • Parent div, set to relative, which I apply minimasonry to
    • Child divs, set to absolute (have the following Tailwind classes applied: absolute flex flex-col justify-between p-4 shadow-md border-l-4 bg-gray-100 border-gray-500 rounded-sm tracking-widest leading-loose mb-4
    new MiniMasonry({
          baseWidth: 512, // Target width of elements.
          container: this.element, // Container selector or HTMLElement. Required.
          gutter: 16, // Width / height of gutter between elements. Use gutterX / gutterY to set different values.
          surroundingGutter: false, // Set left gutter on first columns and right gutter on last.
    });
    

    Description

    I'm experiencing an issue where gutters between items are reduced in size as the result of resizing the window/viewport. All of the following screenshots were taken on the same page load (no refresh):

    Default page load:

    Xnip2022-10-31_07-46-43

    After reducing window size:

    Xnip2022-10-31_07-46-57

    After increasing window size (after being reduced):

    Xnip2022-10-31_07-47-23

    Images of other viewport sizes:

    Xnip2022-10-31_07-47-42 Xnip2022-10-31_07-47-31

    This happens on zoom level change too, not just window resize. This is after reducing and increasing and reducing zoom level a bunch:

    image

    Manually calling a redraw after window resize doesn't seem to have any effect. Looking at the library, it seems like a redraw automatically happens on resize, anyway.

    Is there anything I can do to alleviate this, even if it's a bit hacky?

    opened by dtgay 0
  • Height issue

    Height issue

    Hi, thanks for creating such an awesome library. I am facing an issue while images are loading, it is not getting their full height. I can't use fixed height in my case, so what the solution would be apart for fixed height for images?

    opened by mehtabfarooq 1
  • How can I impact the width of the columns?

    How can I impact the width of the columns?

    First of all, thank you for this library! I got it working quite well, have a question about responsive though: I want the grid to show only one column with 100% width on mobiles, 50% + gutter on tablets and 33% + gutter on desktops. So I don't have a fixed baseWidth. What approach would you use for this?

    Also, I saw that there is a certain responsiveness but so far couldn't make out the pattern that is used.

    opened by sandrooco 0
  • When there is 1 column, `.gutterY` is ignored

    When there is 1 column, `.gutterY` is ignored

    The line with this._count = 1; looks strange to me.

    In my use-case I update MiniMasonry options on resize, depending on the CSS properties of the elements of the grid, and for some screen sizes there is only one column, even though there are many items (._count > 1). And when there is only one columns with many items, the gutter between the items is missing.

    My guess is that #L139 is the issue here, it "thinks" there is only one item and there is no need of margin-bottom, but in reality there is only one column with many items.

    opened by duzun 2
Chakra UI Animations is a dependancy which offers you pre-built animations for your Chakra UI components.

Chakra UI Animations Chakra UI Animations is a dependancy which offers you pre-built animations for your Chakra UI components. Installation yarn add @

Code Chemistry Inc. 9 Nov 19, 2022
Sharerbox - Free, minimalist and lightweight JavaScript-based social-media sharer for websites

Sharerbox Free minimalist and lightweight JavaScript-based social-media sharer for websites. Version: 0.8.1 Description SharerBox is a free, minimalis

Juan Astudillo 3 Aug 22, 2022
Minimalistic portfolio/photography site with masonry grid, page transitions and big images.

Gatsby Starter Portfolio: Emilia Minimalistic portfolio/photography site with masonry grid, page transitions and big images. Themeable with Theme UI.

Cryptob3auty 1 May 20, 2022
Freewall is a cross-browser and responsive jQuery plugin to help you create grid, image and masonry layouts for desktop, mobile, and tablet...

Freewall Freewall is a cross-browser and responsive jQuery plugin to help you create many types of grid layouts: flexible layouts, images layouts, nes

Minh Nguyen 1.9k Dec 27, 2022
A javascript plugin to filter elements from a "masonry" grid.

Isolde Isolde is a lightweight, flexible, and responsive javascript plugin allow you to filter elements from a "masonry" grid. Quick start Install Thi

Tristan BOULANGER 24 Oct 13, 2022
Simple, responsive and lightweight Masonry Grid jQuery Plugin.

jquery-masonry-grid Simple, responsive and lightweight Masonry Grid jQuery Plugin. Installation Add the script before closing the <body> tag (make sur

Peter Breitzler 8 Jun 9, 2022
Customizable masonry Flatlist. it just behave like Flatlist but using ScrollView behind the scene

Would you like to support me? react-native-masonry-grid Customizable masonry Flatlist. it just behave like Flatlist but using ScrollView behind the sc

Numan 5 Sep 7, 2022
A simple library to draw option menu or other popup inputs and layout on Node.js console.

console-gui-tools A simple library to draw option menu or other popup inputs and layout on Node.js console. console-gui-tools A simple Node.js library

Elia Lazzari 12 Dec 24, 2022
Lightweight and independent Pinterest-like cascading grid layout library

Bricklayer is a Lightweight and Independent Pinterest-like Cascading Grid Layout Library ?? Simpler than any other cascading grid layout tools. ❄️ Lig

Adem ilter 2.5k Dec 22, 2022
Create a card layout that let your user flip through it like you see on Google Tips

#Tip Cards by Pete R. Create an animated card layout that let your viewer flip through it like you see on Google Tips Page. Created by Pete R., Founde

Pete R. 213 Nov 5, 2022
Use jsx to make scriptable's layout.

scriptable-jsx This project helps you to write Scriptable widgets with JSX syntax. And add some useful tools by the way. you can check demos in demo f

毛球 8 Oct 10, 2022
Chrome & Firefox extension to return old Twitter layout from 2015.

OldTwitter (2022) Chrome extension to return old Twitter layout from 2015. This extension doesn't add any CSS on top of original Twitter. It's fully o

dimden 35 Jan 4, 2023
2D HTML5 rendering and layout engine for game development

Stage.js is a 2D HTML5 JavaScript library for cross-platform game development, it is lightweight, fast and open-source. Check out examples and demos!

Ali Shakiba 2.2k Jan 3, 2023
Print seat layout for movie, flight (jQuery plugin)

seatLayout.js (movie-seat-layout) Print seat layout for movie, flight and seat selection (jQuery plugin) Demo : https://sachinkurkute.github.io/movie-

Sachin Kurkute 20 Dec 8, 2022
minimalist virtual dom library

petit-dom A minimalist virtual DOM library. Supports HTML & SVG elements. Supports Render functions and Fragments. Custom components allows to build y

Yassine Elouafi 485 Dec 12, 2022
A minimalist Javascript library to perform AJAX POST and GET Request.

minAjax.js A minimalist Javascript library to perform AJAX POST and GET Request. #Check Pretty Documentation http://flouthoc.github.io/minAjax.js/ #Us

null 6 Apr 27, 2021
Zepto.js is a minimalist JavaScript library for modern browsers, with a jQuery-compatible API

Zepto.js – a minimalist JavaScript library Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you u

Thomas Fuchs 15k Dec 31, 2022
A minimalist web app for the daily morning and night athkar.

Morning & Night Athkar | أذكار الصباح والمساء Local Development Recommended: Use nvm to manage npm dependencies and versions. This app uses node versi

Mohammed Amarnah 4 Dec 2, 2022
This is a basic website Todo Application that displays a list that looks and behaves like the part of minimalist project.

To-Do-list-microverse Description This is a basic website; a Todo Application that displays a list that looks and behaves like the part of minimalist

Dennis Akagha 7 Feb 3, 2022