Giter Site home page Giter Site logo

jrespond's Introduction

#jRespond

####jRespond is a simple way to globally manage JavaScript on responsive websites.

Responsive websites that require JavaScript functionality for some breakpoints and not for others need some type of system for triggering the correct funcitons at the correct breakpoint and to also be aware of when a browser is resized across breakpoints. Although switching between breakpoints could be seen as an edge case, a few applications for jRespond are:

  • Managing functionality for initial page load: Even if the browser is never resized, jRespond can help manage what JavaScript functions are triggered when the page loads for a given breakpoint.
  • Development testing: jRespond makes it much easier to test in-browser.
  • Borderline device widths: Real user browser resizing and device rotation that crosses breakpoints.

If your project only needs to support modern browsers I highly recommend checking out Rob Tarr's mediaCheck which uses the matchMedia method. But if you're using respond.js as a polyfill to ensure that your site responds on older browsers, jRespond is worth checking out.

##How does it work?

jRespond is a script that holds a list of user-defined functions that are fired based on the browser's width compared to a list of customizable breakpoints. Entry and exit functions can be defined so transitions between breakpoints can be managed by removing and unbinding some page elements while creating and binding others. jRespond was built to be independent and browser agnostic. It does NOT sniff for media queries in the stylesheets.

After including jRespond.js, call jRespond and define as many or as few media breakpoints as you need for your project. Labels can be any single-word string:

// call jRespond and add breakpoints
var jRes = jRespond([
	{
		label: 'handheld',
		enter: 0,
		exit: 767
	},{
		label: 'tablet',
		enter: 768,
		exit: 979
	},{
		label: 'laptop',
		enter: 980,
		exit: 1199
	},{
		label: 'desktop',
		enter: 1200,
		exit: 10000
	}
]);

Once running, functions can be registered with jRespond along with a desired breakpoint:

// register enter and exit functions for a single breakpoint
jRes.addFunc({
	breakpoint: 'desktop',
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Or an array of breakpoints:

// register enter and exit functions for multiple breakpoints
jRes.addFunc({
	breakpoint: ['desktop','laptop'],
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Use '*' to run a function at every breakpoint:

// register enter and exit functions for every breakpoint
jRes.addFunc({
	breakpoint: '*',
	enter: function() {
		myInitFunc();
	},
	exit: function() {
		myUnInitFunc();
	}
});

Ask jRespond what the current breakpoint is at any time:

// get the current breakpoint
jRes.getBreakpoint();

The breakpoint parameter is required but the enter and exit parameters are optional (of course, at least one is required for something to happen).

##Performance

jRespond is 1.3kb minified and only polls for the browser width every 500ms. If it detects a change the polling speed is increased to 100ms only until the browser width stops changing.

##Browser Support

IE 6+, Safari 5+, Firefox 3+, Chrome 1+

##Dependencies

None.

##Credits

Thanks to Rob Tarr for inspiring the function registration pattern and Markup Boy for helping me with my JavaScript failings.

jrespond's People

Contributors

herschel666 avatar ten1seven avatar

Watchers

 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.