breakpoints-jsis a lightweight, pure javascript library for attaching callbacks to breakpoints.
- Main files
- Quick start
- Usage
- Examples
- Defaults
- Methods
- Browser support
- Contributing
- Development
- Changelog
- Other Projects
- Copyright and license
dist/
├── breakpoints.js
├── breakpoints.es.js
└── breakpoints.min.js
Download the production version or the development version.
bower install breakpoints.js --savenpm install breakpoints-js --saveyarn add breakpoints-js###Build From Source
If you want build from source:
git clone git@github.com:amazingSurge/breakpoints-js.git
cd breakpoints-js
npm install
npm install -g gulp-cli babel-cli
gulp buildDone!
Before you try anything, you need to include breakpoints.js in your page.
<scriptsrc="breakpoints.min.js"></script>You may need provide a matchMedia polyfill if you wish to support old/incapable browsers.
Then you can init the script easily by code
<scripttype="text/javascript">
Breakpoints();
</script>There are some example usages that you can look at to get started. They can be found in the examples folder.
It will use the bootstrap media query breakpoints by default:
Breakpoints.defaults={// Extra small devices (phones)xs: {min: 0,max: 767},// Small devices (tablets)sm: {min: 768,max: 991},// Medium devices (desktops)md: {min: 992,max: 1199,},// Large devices (large desktops)lg: {min: 1200,max: Infinity}};You can set up your own breakpoints when initialize it:
<scripttype="text/javascript">
Breakpoints({mobile: {min: 0,max: 767},tablet: {min: 768,max: 991},destop: {min: 992,max: Infinity}});
</script>Check if the current screen is a specific size.
Breakpoints.is('xs');// return true or falseReturn the size object that you can operate it handily.
// get size objectvarsm=Breakpoints.get('sm');// attach eventssm.on('enter',function(){// do something});// remove event handlersm.off('enter');// get min widthsm.min// 768// get max widthsm.max// 991// get media querysm.media// "(min-width: 768px) and (max-width: 991px)"// check if it's current sizesm.isMatched();// true or false// you can do in a chainBreakpoints.get('sm').on({enter: function(){},leave: function(){}});Return the current screen size object
Breakpoints.current();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');});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);});Breakpoints.on('md lg',{enter: function(){console.info('enter '+this.name);},leave: function(){console.info('leave '+this.name);}});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');});Remove event handlers attached to size.
// remove all events attached to sm sizeBreakpoints.off('sm');// remove all enter type events attached to md sizeBreakpoints.off('md','enter');// remove specific event handlervarenterHandler=function(){};Breakpoints.on('lg','enter',enterHandler);Breakpoints.off('lg',{enter: enterHandler})// alternative wayBreakpoints.off('lg','enter',enterHandler);Attach an event handler to the size change event
// attach handler to change eventBreakpoints.on('change',function(){console.info('enter '+this.current.name);});// altrnative examplevarchangeHandler=function(){// do something };Breakpoints.on('change',changeHandler);// remove the handlerBreakpoints.off('change',changeHandler);// remove all change handlersBreakpoints.off('change');Tested on all major browsers.
![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|---|---|---|---|---|---|
| Latest ✓ | Latest ✓ | Latest ✓ | Latest ✓ | 9-11 ✓ | Latest ✓ |
matchMediabrowser support work perfect on all modern browsers (IE10+, firefox, chrome, android and safari).- With some polyfills (like the ones included in matchMedia.js) Breakpoints works perfect in IE6-9 as well.
Anyone and everyone is welcome to contribute. Please take a moment to
review the guidelines for contributing. Make sure you're using the latest version of jquery-scrollTo before submitting an issue. There are several ways to help out:
- Bug reports
- Feature requests
- Pull requests
- Write test cases for open bug issues
- Contribute to the documentation
jquery-scrollTo is built modularly and uses Gulp as a build system to build its distributable files. To install the necessary dependencies for the build system, please run:
npm install -g gulp
npm install -g babel-cli
npm installThen you can generate new distributable files from the sources, using:
gulp build
More gulp tasks can be found here.
To see the list of recent changes, see Releases section.
If you like this project then I encourage you to check out a few of my other hand-selected projects.
- enquire.js - A lightweight, pure JavaScript library for responding to CSS media queries.
- mediaquery - A jQuery plugin for responsive media query events.
- strapPoint - A small jQuery utility plugin to make working with Bootstrap breakpoints easier.
- pointbreak.js - It provides a friendly interface to matchMedia with named media queries and easy to create callbacks.
Copyright (C) 2016 amazingSurge.
Licensed under the LGPL license.





