Coder Social home page Coder Social logo

isnan909 / flexmasonry Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gilbitron/flexmasonry

0.0 2.0 0.0 43 KB

A lightweight masonry (cascading grid layout) library powered by flexbox.

Home Page: https://flexmasonry.now.sh

License: MIT License

HTML 41.52% CSS 34.31% JavaScript 24.17%

flexmasonry's Introduction

npm

FlexMasonry

FlexMasonry

FlexMasonry is a lightweight, zero-dependency, masonry (cascading grid layout) library powered by CSS flexbox. The library itself is inspired by this article by Tobias Ahlin on using flex, :nth-child(), and order to create a pure CSS masonry layout (as opposed to the hugely popular Masonry library by David DeSandro that is powered by Javascript). I've taken this concept and sprinkled in some Javascript to tie it all together and make it easy to use.

Features

  • Lightweight - Just 6KB of JS and CSS
  • Fast - Uses CSS flexbox for layout
  • Responsive - Show different number of columns at different breakpoints
  • Simple - Just 3 options

See a demo

Install

  • Download the latest release.
  • Clone this repo.
  • Install using NPM/Yarn:
npm install flexmasonry
yarn add flexmasonry

Then, include the flexmasonry.js and flexmasonry.css files from the dist folder in your HTML. Or you can use the files directly from a CDN:

<link rel="stylesheet" href="https://unpkg.com/flexmasonry/dist/flexmasonry.css">
<script src="https://unpkg.com/flexmasonry/dist/flexmasonry.js"></script>

Usage

Set up your HTML. For example:

<div class="grid">
    <div><img src="https://source.unsplash.com/t3DHojIo-08" alt=""></div>
    <div><img src="https://source.unsplash.com/Imc-IoZDMXc" alt=""></div>
    <div><img src="https://source.unsplash.com/SOZWHqeXcPQ" alt=""></div>
    <div><img src="https://source.unsplash.com/bkdzvgBB7rQ" alt=""></div>
    <div><img src="https://source.unsplash.com/Aruugw_rJCM" alt=""></div>
</div>

Then hook up the script, passing in the selector target:

FlexMasonry.init('.grid');

FlexMasonry will then convert all .grid elements to masonry grids with multiple columns.

Options

The second, optional, parameter of the init method is an object containing options. The default options are as follows:

{
    /*
     * If `responsive` is `true`, `breakpointCols` will be used to determine
     * how many columns a grid should have at a given responsive breakpoint.
     */
    responsive: true,
    /*
     * A list of how many columns should be shown at different responsive
     * breakpoints, defined by media queries.
     */
    breakpointCols: {
        'min-width: 1500px': 6,
        'min-width: 1200px': 5,
        'min-width: 992px': 4,
        'min-width: 768px': 3,
        'min-width: 576px': 2,
    },
    /*
     * If `responsive` is `false`, this number of columns will always be shown,
     * no matter the width of the screen.
     */
    numCols: 4,
}

For example, to always shown 6 columns in your grid:

FlexMasonry.init('.grid', {
    responsive: false,
    numCols: 6
});

Methods

The FlexMasonry variable has several methods:

init(targets, options = {})

Initialises the FlexMasonry library and sets up the targets as masonry grids.

  • targets can be a string, an array of elements or a Node​List.
  • options see above.

refresh(target, options = {})

Refreshes the target grid layout.

  • targets can be a string, an array of elements or a Node​List.
  • options see above.

refreshAll(options = {})

Refreshes the grid layouts of all targets passed to init().

destroyAll()

Removes the event listeners for all targets passed to init().

Development

Run yarn to install the dependencies and use demo/index.html to test things. To watch/build the library:

yarn watch
yarn build

FAQ

Why not just use pure CSS?

A good question! You can use pure CSS to achieve the same outcome. However, there are several aspects of this setup that require a bit of "dynamic" updating to make it flexible and easy to use (hence the use of Javascript). The main one being that the masonry container requires a fixed height (which FlexMasonry calculates on the fly). Also the masonry container needs a certain number of "break" elements to work properly depending on the number of columns. To enable this, and to support having a different number of columns at different responsive breakpoints, we need Javascript.

Credits

FlexMasonry was created by Gilbert Pellegrom from Dev7studios. Released under the MIT license.

flexmasonry's People

Contributors

gilbitron avatar

Watchers

James Cloos avatar ishan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.