Skip to content

thelevicole/breakpoints.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Javascript breakpoints for Bootstrap

Basic usage

var bp = new Breakpoints();

bp.up( 'md', function() {
	console.log( 'This will run if the screen size is wider than 768px' );
} );

bp.down( 'sm', function() {
	console.log( 'This will run if the screen size is smaller than 576px' );
} );

var button = document.querySelector( 'button' );

button.onclick = function() {
	if ( bp.isUp( 'lg' ) ) {
		alert( 'Screen is large and up' );
	} else {
		alert( 'Screen is smaller than large' );
	}
};

window.onresize = function() {
	if ( window.innerWidth < bp.get( 'md' ) ) {
		// Custom resize handling
	}
};

Custom breakpoints

If you have customised the Bootstrap grid and have non standard breakpoints (see here), you can defined these like so:

var bp = new Breakpoints( [ 'small', 'medium', 'large' ] );

bp.up( 'medium', function() {
	...
} );

bp.down( 'large', function() {
	...
} );