Giter Club home page Giter Club logo

stack-up.js's Introduction

stack-up.js

A simple and fast JavaScript plugin to help you create fixed-width, variable-height grid layout.

Getting started

First, include stack-up.js to your project.

<!-- Example HTML -->
<div id="gridContainer">
  <div class="gridItem">...</div>
  <div class="gridItem">...</div>
  <div class="gridItem">...</div>
</div>

<!-- Scripts -->
<script src="js/stack-up.js"></script>

Minimum CSS requirements

#gridContainer {
  position: relative;
}

.gridItem {
  position: absolute;
}

.gridItem img {
  width: 100%;
}

Make sure all content inside the container are loaded before initializing stack-up. This is to make sure stack-up calculates the right height before plotting.

// One way to make sure everything is loaded is
// to wrap the initializer inside window onload.
window.onload = function() {

  // Create a stackup object.
  var stackup = new StackUp({
    containerSelector: "#gridContainer",
    itemsSelector    : "#gridContainer > .gridItem",
    columnWidth      : 240,
  });
  // Initialize stackup.
  stackup.initialize();

};

Config

Customize stack-up to your needs.

stackup.setConfig({
  columnWidth        : 320,
  gutter             : 18,
  isFluid            : false,
  layout             : "ordinal", // ordinal, optimized
  numberOfColumns    : 3,
  resizeDebounceDelay: 350
});

// This function allows you to modify how each item is moved or animated.
stackup.config.moveItem: function(item, left, top, callback) {
  item.style.left = left + "px";
  item.style.top  = top + "px";
  // The callback function is required to continue operation.
  callback();
};

// This one allows you to modify how the container scales.
stackup.config.scaleContainer: function(container, width, height, callback) {
  container.style.width  = width + "px";
  container.style.height = height + "px";
  // The callback function is required to continue operation.
  callback();
};

Reset and restack

Once the grid is initialized; you will need to call the restack method if you change any of the configurations.

stackup.config.layout = 'optimized';
stackup.restack();

The restack method will not work properly if you change something that affect the dimensions of the grid item. You will have to use reset instead. (This recalculates the grid stacking from top to bottom.)

stackup.config.columnWidth = 220;
stackup.reset();

You will also need to use the reset method if you add or remove any grid item.

Append

The append method allows you to add a new grid item without re-calculating everything.

// Get container element.
var gridContainer = document.getElementById("gridContainer");

// Create a new grid item element.
var gridItem = document.createElement("div");
gridItem.setAttribute("class", "gridItem");
gridItem.innerHTML = "blah blah";

// Append the new grid item element into container element.
gridContainer.appendChild(gridItem);

// Append it to stackup.
stackup.append(gridItem);

There is currently no prepend method.

That's it!

License

StackUp is licensed under the MIT license - http://opensource.org/licenses/MIT

Copyright (C) 2017 Andrew Prasetya

stack-up.js's People

Contributors

tommyzzm 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.