Breakpoints
![npm package](https://img.shields.io/npm/v/@pluginjs/breakpoints.svg)
breakpoints
is a utility JavaScript library for breakpoints.
Samples
Introduction
Installation
Yarn
yarn add @pluginjs/breakpoints
NPM
npm i @pluginjs/breakpoints
Getting Started
CDN:
Development:
<script src="https://unpkg.com/@pluginjs/breakpoints/dist/breakpoints.js"></script>
Production:
<script src="https://unpkg.com/@pluginjs/breakpoints/dist/breakpoints.min.js"></script>
Initialize
HTML:
ECMAScript Module:
import Breakpoints from "@pluginjs/breakpoints"
Breakpoints();
CommonJS:
const Breakpoints = require("@pluginjs/breakpoints")
Breakpoints();
Browser:
<script src="https://unpkg.com/@pluginjs/breakpoints/dist/breakpoints.js"></script>
<script>
Breakpoints();
</script>
Defaults
It will use the bootstrap media query breakpoints by default:
Breakpoints.defaults = {
xs: {
min: 0,
max: 575
},
sm: {
min: 576,
max: 767
},
md: {
min: 768,
max: 991
},
lg: {
min: 992,
max: 1199
},
xl: {
min: 1200,
max: Infinity
}
};
You can set up your own breakpoints when initialize it:
<script type="text/javascript">
Breakpoints({
mobile: {
min: 0,
max: 767
},
tablet: {
min: 768,
max: 991
},
destop: {
min: 992,
max: Infinity
}
});
</script>
Methods
is
Check if the current screen is a specific size.
Breakpoints.is('xs');
Breakpoints.is('md+');
Breakpoints.is('sm-');
Breakpoints.is('sm-md');
get
Return the size object that you can operate it handily.
var sm = Breakpoints.get('sm');
sm.on('enter', function(){
});
sm.off('enter');
sm.min
sm.max
sm.media
sm.isMatched();
Breakpoints.get('sm').on({
enter: function(){
},
leave: function(){
}
});
current
Return the current screen size object
Breakpoints.current();
on
Attach an event handler function for one or more events to the size
Breakpoints.on('md', {
enter: function() {
console.info('enter '+ this.name);
},
leave: function() {
console.info('leave '+ this.name);
}
});
Breakpoints.on('lg', 'enter', function(){
console.info('hello lg');
});
Passing data to the callback
Breakpoints.on('sm', 'enter', {
name: "Addy"
}, function(data) {
console.info(data.name + ' enter '+ this.name);
});
Breakpoints.on('sm', 'leave', {
name: "Karl"
}, function(data) {
console.info(data.name + ' leave '+ this.name);
});
one
The handler attached to the size will executed at most once.
Breakpoints.one('md', 'enter', function(){
console.info('this only appear once when enter md');
});
off
Remove event handlers attached to size.
Breakpoints.off('sm');
Breakpoints.off('md', 'enter');
var enterHandler = function(){};
Breakpoints.on('lg', 'enter', enterHandler);
Breakpoints.off('lg', {
enter: enterHandler
})
Breakpoints.off('lg', 'enter', enterHandler);
change
Attach an event handler to the size change event
Breakpoints.on('change', function(){
console.info('enter ' + this.current.name);
});
var changeHandler = function(){
};
Breakpoints.on('change', changeHandler);
Breakpoints.off('change', changeHandler);
Breakpoints.off('change');
At
Attach an event handler function for the screen at a specific size
Breakpoints.at('md', {
enter: function() {
console.info('enter '+ this.name);
},
leave: function() {
console.info('leave '+ this.name);
}
});
Breakpoints.at('md', 'enter', function(){
console.info('enter '+ this.name);
});
Breakpoints.on('md', 'enter', function(){
console.info('enter '+ this.name);
});
Breakpoints.off('md', 'enter');
From
Attach an event handler function for the screen width is inside a specific size or larger
Breakpoints.from('md', {
enter: function() {
console.info('enter md+');
},
leave: function() {
console.info('leave md+');
}
});
Breakpoints.from('md', 'enter', function(){
console.info('enter md+');
});
Breakpoints.on('md+', 'enter', function(){
console.info('enter md+');
});
Breakpoints.off('md+', 'enter');
To
Attach an event handler function for the screen width is inside a specific size or smaller
Breakpoints.to('md', {
enter: function() {
console.info('enter md-');
},
leave: function() {
console.info('leave md-');
}
});
Breakpoints.to('md', 'enter', function(){
console.info('enter md-');
});
Breakpoints.on('md-', 'enter', function(){
console.info('enter md-');
});
Breakpoints.off('md-', 'enter');
Between
Attach an event handler function for the screen width is inside two specific size
Breakpoints.between('md', 'lg', {
enter: function() {
console.info('enter md-lg');
},
leave: function() {
console.info('leave md-lg');
}
});
Breakpoints.between('md', 'lg', 'enter', function(){
console.info('enter md-lg');
});
Breakpoints.on('md-lg', 'enter', function(){
console.info('enter md-');
});
Breakpoints.off('md-lg', 'enter');
Browser support
Tested on all major browsers.
![IE / Edge](https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png) IE / Edge | ![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) Firefox | ![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) Chrome | ![Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png) Safari | ![Opera](https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png) Opera |
---|
IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
License
@pluginjs/breakpoints is Licensed under the GPL-v3 license.
If you want to use @pluginjs/breakpoints project to develop commercial sites, themes, projects, and applications, the Commercial license is the appropriate license. With this option, your source code is kept proprietary.
For purchase an Commercial License, contact us purchase@thecreation.co.
Copyright
Copyright (C) 2018 Creation Studio Limited.