Mason.js for creating a perfect grid with jQuery.

Related tags

React Mason
Overview

forthebadge

MasonJS

Mason.js is a jQuery plugin that allows you to create a perfect grid of elements.

This is not Masonry, or Isotope or Gridalicious. Mason fills in those ugly gaps, and creates a perfectly filled space.

Bower Version

2.0.2

USE

Basic CSS

#container {
	width: 100%;
	position: relative;
}
.box {
	float: left;
	background-color: #00ffff;
	position: relative;
}

Call Mason.js (BASIC)

$("#container").mason({
	itemSelector: ".box",
	ratio: 1.5,
	sizes: [
		[1,1],
		[1,2],
		[2,2]
	]
});

OPTIONS

Mason.js has a number of options:

$("#container").mason({
	itemSelector: ".box",
	ratio: 1.5,
	sizes: [
		[1,1],
		[1,2],
		[2,2]
	],
	columns: [
		[0,480,1],
		[480,780,2],
		[780,1080,3],
		[1080,1320,4],
		[1320,1680,5]
	],
	promoted: [
		['class_name', 2, 1],
		['class_name', 2, 3],
		['class_name', 3, 3],
	],
	filler: {
		itemSelector: '.fillerBox',
		filler_class: 'custom_filler',
		keepDataAndEvents: false
	},
	layout: 'fluid',
	gutter: 10
},function(){
	console.log("COMPLETE!")
});
  • itemSelector
    • the element that makes up your grid
  • ratio
    • The ratio is a number that is used to create the blocks based on column count and width. This is based on the number of columns requested and the browser width.
  • sizes
    • Sizes are an array of sizes you wish to use in your grid. These are composed of block numbers. (ex: [1,1] means 1 block high, 1 block wide)
  • columns
    • columns are an array of break points for your columns. Think of this like media queries. start small and grow. They should be formatted as [min,max,cols]
  • filler
    • itemSelector: This describes the elements to be used to fill in blank spaces. This will default to the original itemSelector if there is nothing
    • filler_class: This is a class given to filler elements within the grid, used for cleaning up if a grid set to fluid
    • keepDataAndEvents: Mason creates a clone of the filler elements before adding them to the grid, this boolean (true/false) property tells Mason to retain the events and data that have already been bound to the filler elements
  • promoted
    • Accepts an array of ['CLASS_NAME', WIDTH, HEIGHT] these items will be forced to those dimensions.
  • Layout
    • There are two layouts, fluid and fixed. Mason will default to fixed. Fluid means it will be responsive.
  • Callback
    • You can add a callback function to Mason to notify you when the grid has completed building.
  • Gutter
    • Allows you to add spacing between the elements, think of this as a margin.

DESTROY

At times you may want to destroy the mason object and no longer track window changes, to do this assign the mason grid to a variable such as var mason and when you're ready to destroy just call mason.destroy() this will remove all listeners on the mason object and you can remove the grid or elements without any ill effects.

Install

  1. npm install
  2. bower install

Running

cd into project and run gulp

Building

cd into project

  1. run gulp dist
  2. run gulp finish_dist

Examples

Look at the public folder for examples.

Comments
  • Uncaught TypeError: Cannot set property '0' of undefined

    Uncaught TypeError: Cannot set property '0' of undefined

    When columns is declared, like this:

    columns: [
      [0,768,2],
      [768,900,4]
    ]
    

    I get Uncaught TypeError: Cannot set property '0' of undefined when I'm resizing the window at 768px. line 190:

    for (var i = 0; i < a; i++) {
      for (var bh = 0; bh < h; bh++) {
        elements.matrix[t + bh][l] = true;
          for (var bw = 0; bw < w; bw++) {
            elements.matrix[t + bh][l + bw] = true;
          }
      }
    }
    

    Hope it helps :+1:

    opened by ayozebarrera 34
  • How to use mason.js for a perfect grid of images

    How to use mason.js for a perfect grid of images

    Hello there, I was trying to get you by email but it didn't seem to work out so I'll try through here. I would like to use mason.js for creating a perfect grid of images, my question is, how would i do it and if its even possible with mason.js?

    These are the things I need it to do:

    1. Create a perfect grid from inputted images of any size and any amount with no gaps at all.
    2. In order to fill gaps in, the system can resize the images, however ratio should be kept (In case there will be still gaps, the system can also crop certain images).

    What do you think?

    Thanks for your help.

    opened by saharati 13
  • Uncaught TypeError: Cannot set property '2' of undefined

    Uncaught TypeError: Cannot set property '2' of undefined

    I have this issue as well. I see that there are others. I am not using the column parameter, so I can see this can be an issue with the JavaScript's default column parameters.

    I use 2 promoted items. One size is 2 x 3 (big) and one size is 1 x 3 (wide). and the ratio is 1.1 (which i assume means 1 height x 1 width, aka Aspect Ratio aka mine would be square)..

    Anyway, I am not even sure I wanted to get in to the element.matrix[] stuff at all. But I got an error on this error on line 190 of mason.js.

    I use a Jquery .load() ajax to call the next page of a WordPress paginated loop. I use more javascript to form those posts in to the proper html markup as you show in your demo.

    I use PHP to randomize the class to add big and wide classes to promote boxes. I thought that was the idea. Otherwise, does this JS add the promoted classes itself? I only made it have like 10% of being class "big" and 15% of being class "wide" and it usually works out that it has 1-2 big and 2-5 wide.. so I assumed everything was working.

    Load 1 always works without error. All the time. The randomization works great! I can then use CSS to change the appearance of the promoted posts, so its amazing! Load 2 ALSO WORKS!! As in, I have been successfully about to combine this plugin with a .load() AJAX call of a WordPress paginated loop, and have the size randomize.

    Load 1 and load 2 ALWAYS WORK, and I even added loading elements so the loading appearance is okay.. the div that gets clone() (usually the first filler box) now is a loading box. Then I have to use index and eq() to swap out content to get the posts to appear in the correct order. That is working beautifully as well.

    The thing is Load 3. If I press the load more button I created twice, I get:

    "Uncaught TypeError: Cannot set property '2' of undefined"

    I eliminated the change of it being any other error or any other line by using the uncompiled version, and not compiling it myself at all either. I also had spent several hours trouble shooting before I ever got to that point.

    I believe it could be a parse issue that is happening. When I read more about element.matrix[] I found that many people needed to parse the variables inside. Also the error itself is saying "Cannot set property of '2' ", 2 being a number not a variable or attribute or property of any kind, it is a number. Right? I am not sure I am not an expert.

    localnationalnews.com

    I am paying for help on this assignment. I am also needing many other features. But all I want to do is get past this error. I have several other solutions for other features. I guess what I am really saying is I am willing to help and devote resources to making this better.

    opened by UnaOmnia 11
  • Uncaught TypeError: Cannot read property '1' of undefined

    Uncaught TypeError: Cannot read property '1' of undefined

    Hey dude. I'm using this cool plugin but always get this error. My JS:

    $('.hub').mason({
        itemSelector: '.tile',
        ratio: 1,
        sizes: [
            [1,1]
        ],
        promoted: [
            [0.5,0.5,'tile140x140'],
            [0.5,1,'tile140x290'],
            [1,0.5,'tile290x140'],
            [1,1,'tile290x290'],
            [2,2,'tile590x590'],
            [2,2.5,'tile590x740']
        ],
        columns: [
            [0,480,1],
            [480,780,2],
            [780,1080,3],
            [1080,1320,4],
            [1320,1680,5]
        ],
        layout: 'fluid',
        gutter: 5
    });
    
    opened by Mohsen-Khakbiz 10
  • Items repeating unnecessarily

    Items repeating unnecessarily

    Hi!

    This looks like an awesome plugin. I've just cracked it out on an example page, with 4 images as a test.

    $('.images').mason({
      itemSelector: '.image',
      ratio: 1.0,
      sizes: [
        [1, 1]
      ],
      layout: 'fluid'
    });
    

    I'd like them all to render with an equal size, and distribute evenly to fill the container. This the plugin does brilliantly. However, it doesn't just fill one line, it creates a 4x4 grid of images (obviously repeating each image 4 times) meaning the container is 4x the height it should be. I'm sure it's down to user error, but is there any way of disabling the repetition, and limiting the height of the container?

    opened by jonlambert 9
  • TypeError: settings.sizes[s] is undefined

    TypeError: settings.sizes[s] is undefined

    Hi there,

    just created mason instance with following parameters:

    $("#mason-grid").mason({ itemSelector: ".mason-box", ratio: 1.1, sizes: [ [1, 1] ], promoted: [ [1, 3, 'wide'], [3, 1, 'tower'], [2, 3, 'big'], [1, 1, 'default'] ], filler: { itemSelector: '.filler', filler_class: 'custom_filler' }....

    ok...if I give some promoted classes I get this error:

    TypeError: settings.sizes[s] is undefined error source line: var h = settings.sizes[s][1];

    //creality

    opened by CR34L 9
  • Best way to append new content for infinity scrolling page

    Best way to append new content for infinity scrolling page

    I was just wondering if anyone has looked at Mason for an infinity scrolling style page.

    Is there a recommended way to append content to the main content div without causing everything to redraw?

    Thanks,

    Jamie

    opened by Jamie452 8
  • Bad positioning

    Bad positioning

    Hi, nice work with Mason.

    I'm having a little issue. I think there is appearing a filler that we don't really need..

    captura de pantalla 2014-12-10 a las 10 45 27

    Check the next jsfiddle: http://jsfiddle.net/fr93465n/2/

    If you go to the bottom of the result, you will see a small square (light green) with a filler over it. If we move that square to the place of the filler. We we'll have a perfect grid, so...

    How avoid that? I don't really need fillers at this time..

    thankyou~

    opened by ayozebarrera 8
  • Uncaught TypeError: Cannot set property '4' of undefined

    Uncaught TypeError: Cannot set property '4' of undefined

    Hi there -

    First of all, love the plugin! Thank you.

    Although everything still works on the frontend, I am receiving a JS error in my console: "Uncaught TypeError: Cannot set property '4' of undefined"

    Seems to be the same as this issue: https://github.com/DrewDahlman/Mason/issues/10

    If I use the minified version vs. non minified version I get the same error but instead of property '4', it's property '1'

    This error is causing other issues with jQuery plugins, so I'm hoping to get it fixed.

    I am using the most recent version of the script here: https://github.com/DrewDahlman/Mason/blob/master/mason.min.js

    Thanks in advance!

    opened by amandagarceau 8
  • Changed randomness to be passed as a variable.

    Changed randomness to be passed as a variable.

    Added randomSizes and randomFillers options if randomSizes are false then sizes need to be in data-layout attribute as in:

    2

    the datalayout corresponds to the size index (can be improved be passing the exact layout in the html

    $("#grid").mason({ itemSelector: ".box", ratio: 1.5, sizes: [ [1,1], [1,2], [2,1], [2,2], ], columns: [ [0,480,1], [480,780,2], [780,1080,3],

    ], filler: { itemSelector: '.fillerBox', filler_class: 'custom_filler' }, randomSizes:false, randomFillers:false,

    layout: 'fluid', gutter: 5 },function(){ console.log("COMPLETE!") });

    opened by tomeralmog 7
  • Filler items are slightly misaligned

    Filler items are slightly misaligned

    Firstly, awesome work with Mason, it's exactly what I've been looking for.

    I've got a relatively large page of items, using a few different sizes and it needs to be fluid. One issue I'm having is that when I don't include a gutter, some of the filler items appear misaligned, either causing small gaps between them, or overlapping one another. It's slight, but noticeable.

    It looks like some of the filler item properties are calculated as fractions (e.g. left: 810.68px) so I'm not sure if this has anything to do with it. Any ideas? Thanks!

    mason-filler-alignment

    bug 
    opened by apresern 6
  • Is there a way to refresh Mason?

    Is there a way to refresh Mason?

    I'm having a problem with this script. I want to add dinamically new elements to the grid, but then they wont appear, because fillers who have position absolute appear on page in front of them. If i resize everything it works fine. Is there a way to do the same resize does? This way new items can be added dinamically

    opened by Nmaster88 1
  • Fixed height of container

    Fixed height of container

    Hi, thanks for great plugin. Is it possible somehow to force container height to some value, or to define number of rows somehow? I know it is difficult, but I'm just wondering, because with fixed container height there will be less "jumping" of the pagination and the other content bellow the container. Cheers.

    opened by MambotCodeLab 0
  • Getting typeError

    Getting typeError

    Hello, I'm getting the error:

    TypeError: a.matrix[(d + o)] is undefined

    in console. I noticed it after adding CSS preloading .

    Could this have something to do with the error? And why did it occur? Because grid wrapper width and height are unknown at the time masonjs tries to build the grid?

    opened by BrainFeeder 0
  • May I suggest

    May I suggest

    Since your elements matrix is dependent on element calculations, may I suggest to automatically set the container of the elements to relative/absolute and also the item selected to be set as a block. This way here you get proper calculations and the element matrix array won't be dynamically created with improper size.

    opened by joshbarber89 0
  • Space around the grid

    Space around the grid

    grid_mason.pdf

    Hello Drew Dahlman

    I hope you're very well.

    Mason seems incredible and I started to study it, I am a graphic design student but I do not understand how to solve a problem with my grid.

    It would be amazing if you could help me with this.

    I sent you an image with a grid that I have and with a grid that I have to do.

    I know very little about Javascript, but I have no idea how to write a code to do disappear the space around my grid and adjust it to body.

    Could you help me,

    please?

    Thanks a lot!!!

    Regards

    Tânia

    opened by taniacvq 0
Owner
Drew Dahlman
Maker of things. Full Snack Developer. Dogs. Design. Code. Community. Co-Founder / Director of Technology @OddCommon. Previously @Firstborn @legworkstudio.
Drew Dahlman
JavaScript data grid with a spreadsheet look & feel. Works for React, Angular, and Vue. Supported by the Handsontable team ⚡

Handsontable is a JavaScript component that combines data grid features with spreadsheet-like UX. It provides data binding, data validation, filtering

Handsontable 17.4k Jan 1, 2023
A draggable and resizable grid layout with responsive breakpoints, for React.

React-Grid-Layout React-Grid-Layout is a grid layout system much like Packery or Gridster, for React. Unlike those systems, it is responsive and suppo

RGL 16.9k Jan 4, 2023
Testimonials grid section main

Frontend Mentor - Testimonials grid section Welcome! ?? Thanks for checking out this front-end coding challenge. Frontend Mentor challenges help you i

AhmedKamal199 2 Feb 25, 2022
An interactive CLI automation tool 🛠️ for creating react.js and next.js projects most fast and efficiently. ⚛️

An interactive CLI automation tool ??️ for creating react.js and next.js projects most fast and efficiently. ⚛️ About ℹ️ ReexJs CLI is an interactive

Alexis Guzman 27 Apr 12, 2022
Creating this clone to learn the fundamentals of Next Js and Tailwind CSS.

Next.js + Tailwind CSS Example This example shows how to use Tailwind CSS (v3.0) with Next.js. It follows the steps outlined in the official Tailwind

Premveer Tomar 2 Feb 2, 2022
React components and hooks for creating VR/AR applications with @react-three/fiber

@react-three/xr React components and hooks for creating VR/AR applications with @react-three/fiber npm install @react-three/xr These demos are real,

Poimandres 1.4k Jan 4, 2023
A zero-config toolkit for creating fine Forge apps

A zero-config toolkit for creating fine Forge apps. Background The Forge Dev Kit (FDK) is a preconfigured set of libraries and frameworks for developi

Fine Software 17 Aug 10, 2022
tb-grid is a super simple and lightweight 12 column responsive grid system utilizing css grid.

tb-grid Lightweight (<1kb gzipped) 12 column grid system, built with css grid. ?? Demos & Playground Have a look at those examples: Main Demo: https:/

Taskbase 26 Dec 28, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Grid-tool - Small tool that allows you to integrate a predefined or generated grid into your front-end development environment.

Grid tool Small tool that allows you to integrate a predefined or generated grid into your front-end development environment. Tool installation includ

hmongouachon 2 Jan 4, 2022
🌀 The Javacript framework for creating a portal to your data. Perfect for a single dataset or a full catalog.

?? Portal.JS The javascript framework for data portals ?? portal is a framework for rapidly building rich data portal frontends using a modern fronten

Datopian 2k Dec 30, 2022
Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries.

Image Zoom (jQuery) Plugin Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries that is less th

Mario Duarte 8 Aug 3, 2022
An npm package with Tailwind CSS utility classes for creating responsive grid columns without media queries using auto fit.

Grid Auto Fit for Tailwind CSS A plugin that helps you create a responsive grid layout in Tailwind CSS without using media queries. It uses the auto-f

Thirus 80 Dec 28, 2022
A jQuery plugin that displays a thumbnail grid expanding preview similar to the effect seen on Google Images.

jQuery GRIDDER 1.4.2 ======= A jQuery plugin that displays a thumbnail grid expanding preview similar to the effect seen on Google Images. We have all

Orion Gunning 455 Nov 6, 2022
jQuery grid plugin

jqGrid jQuery grid plugin jqGrid is an Ajax-enabled JavaScript control that provides solutions for representing and manipulating tabular data on the w

Tony Tomov 2.8k Jan 7, 2023
Lightweight Grid jQuery Plugin

jsGrid Lightweight Grid jQuery Plugin Project site js-grid.com jsGrid is a lightweight client-side data grid control based on jQuery. It supports basi

Artem Tabalin 1.5k Dec 31, 2022
A jQuery plugin that displays a thumbnail grid expanding preview similar to the effect seen on Google Images.

jQuery GRIDDER 1.4.2 ======= A jQuery plugin that displays a thumbnail grid expanding preview similar to the effect seen on Google Images. We have all

Orion Gunning 455 Nov 6, 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