Code Monkey home page Code Monkey logo

breakpoint's Introduction

jQuery Breakpoint

A simple way to use media queries in your jQuery javascripts!

The structure of a breakpoint

A breakpoint, in this case, is a javascript object consisting of four methods: condition, first_enter, enter and exit.

condition() should return true when the breakpoint should be activated and false when it should be deactivated. Most likely, you'll want to use a media query as your condition, but this is not by any means required by the breakpoint plugin.

	$.breakpoint({
		condition: function () {
			return window.matchMedia('only screen and (min-width:500px)').matches;
		}
	});

Whenever the condition returns true, be it on page load or when the viewport changes, the enter method is executed. The first time this happens, the optional first_enter method will execute before the enter method. When condition becomes false, the exit method will execute. Be aware however that the exit method will only run provided that the condition previously has been true!

	$.breakpoint({
		condition: function () {
			return window.matchMedia('only screen and (min-width:500px)').matches;
		},
		first_enter: function () {
			// Code will run the first time condition() is true.
			// Here, you might create elements to use in
			// your enter and exit methods.
		},
		enter: function () {
			// Code will run whenever condition() becomes true.
		},
		exit: function () {
			// Code will run whenever condition() becomes false
			// (if it was previously true).
			// This is where you revert the things you do in the
			// enter method.
		}
	});

Debugging

The plugin provides some basic debugging which you can activate by setting $.breakpoint.debug = true. This will cause the plugin to log information to the browser console whenever a breakpoint is entered or exited. Add a toString method to your breakpoint object to distinguish between breakpoints; otherwise you'll see [object Object] as the name of the breakpoint.

Tips and tricks

MatchMedia support in older browsers

To use media queries via window.matchMedia in older browser you can use something like Paul Irish's matchMedia() polyfill. If you're using Modernizr, the same polyfill can be included by checking the Media queries checkbox in the Extra section.

Self invoking anonymous function

It's generally a good idea to use a self invoking anonymous function to return the breakpoint object. This way, you can define private variables which can be used in all your breakpoint methods.

	$.breakpoint((function () {
		var element;

		return {
			condition: function () {
				return window.matchMedia('only screen and (min-width:500px)').matches;
			},
			first_enter: function () {
				// Create element.
				element = $('<a>');
			},
			enter: function () {
				// Append element to DOM when condition is true.
				$('body').append(element);
			},
			exit: function () {
				// Detach element when condition is false.
				element.detach();
			}
		}
	}()));

breakpoint's People

Watchers

Greg Leroux 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.