Giter Site home page Giter Site logo

res's Introduction

res.js

Res.js is a swiss army knife for responsive sites.

Minified, Res.js comes in at 4kb making it the perfect compliment to your next project. Res.js is not dependant on jQuery or Zepto and is written in vanilla Javascript.

Res.js is simple to include in your project. Res.js takes one argument, a JSON array of breakpoints. This makes it easy to include in an JSON formatted model you may already be using.

Each state has two required keys: name ("state") and breakpoint ("breakpoint"). If you wish to position elements via a Javascript based grid, you can also provide optional number of columns ("cols"), outer margin ("margin") and gutter ("gutter"). Res.js will calculate the position of columns and size of column spans for you. An example of setting multiple breakpoints and supplying the units needed to create a grid for each breakpoint is below.


var r = new res([{
                     "state": "portrait",
                     "breakpoint": 420,
                     "cols": 4,
                     "margin": 10,
                     "gutter": 10
                 },
                 {
                     "state": "landscape",
                     "breakpoint": 640,
                     "cols": 4,
                     "margin": 10,
                     "gutter": 10
                 },
                 {
                     "state": "tablet",
                     "breakpoint": 768,
                     "cols": 12,
                     "margin": 40,
                     "gutter": 10
                 },
                 {
                     "state": "small",
                     "breakpoint": 1024,
                     "cols": 12,
                     "margin": 40,
                     "gutter": 10
                 },
                 {
                     "state": "medium",
                     "breakpoint": 1440,
                     "cols": 16,
                     "margin": 80,
                     "gutter": 20
                 },
                 {
                     "state": "large",
                     "breakpoint": 1920,
                     "cols": 16,
                     "margin": 80,
                     "gutter": 20
                 },
                 {
                     "state": "retina",
                     "breakpoint": 3840,
                     "cols": 16,
                     "margin": 160,
                     "gutter": 40
                 }]);


Res.js doesn't just define breakpoints in Javascript, it also provides easy access to OS and Browser type, detects whether a device is in Portrait or Landscape and uses Mouse or Touch input.

Once instantiated as an object, you can do things based on device type, input, orientation, os, and breakpoint. For example, this statement checks if the device type is Android and the device is oriented in landscape.


if( r.os === 'android' && r.device === 'tablet' && r.orient === 'landscape' ){

	// do something here for android tablets in landscape mode

}

Sometimes you need to specify the width of an element after filling it dynamically with content. With frameworks like famo.us coming out that primarily position and size elements with Javascript, this grid can come in handy.


// in a 12 column layout, give the element a width of 4 columns and position the element on the right side of the page, on column 8.

this.width = r.grid.colSpan[4];
this.x = r.grid.col[8];


In your app, you can listen for the "stateChange" event on the window and resposition and resize elements based on stateChange. This way you can make your apps more performant by not repeating window.onresize events. The stateChange fires after the window has resized and during initialization.


window.addEventListener('stateChange',function(ev){

if(r.state === 'portrait'){
  that.width = r.grid.colSpan[4];
  that.x = r.grid.col[8];
}

if(r.state === 'tablet'){
  that.width = r.grid.colSpan[6];
  that.x = r.grid.col[6];
}

});


Fork the repo and fire up index.html on your local development server. Type "r" in the console to get the current res.js implementation on the page. More examples coming soon.

Sumbit issues in the issue tracker, fork the repo and make a pull request to add a feature.

v 1.3 -----

  • Added support for Edge browser.
  • Fixed an issue that would prevent validating IE11 and Opera User Agent.

v.125 -----

  • Added dynamic state creation.
  • Changed format of model to JSON.
  • Implemented an optional grid system on the window.

v.1.1 -----

  • Dispatched an Event on the window on StateChange.

res's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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