Breakpoints.js

Breakpoints.js is a lightweight, pure javascript library (with **no dependencies**) for attaching callbacks to breakpoints.

Usage

You can init the script easily by code Breakpoints();.

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:

<script type="text/javascript">
    Breakpoints({
        mobile: {
            min: 0,
            max: 767
        },
        tablet: {
            min: 768,
            max: 991
        },
        destop: {
            min: 992,
            max: Infinity
        }
    });
</script>

API

is

Check if the current screen is a specific size.

Breakpoints.is('xs'); // return true or false

get

Return the size object that you can operate it handily.


// get size object
var sm = Breakpoints.get('sm');
// attach events
sm.on('enter', function(){
    // do something
});
// remove event handler
sm.off('enter');
// get min width
sm.min // 768
// get max width
sm.max // 991
// get media query
sm.media // "(min-width: 768px) and (max-width: 991px)"
// check if it's current size
sm.isMatched(); // true or false
// you also do in a chain
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.

Enable
Disable
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

Enable
Disable

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);
});

Unite sizes

Enable
Disable
Breakpoints.on('md lg', {
    enter: function() {
        console.info('enter '+ this.name);
    },
    leave: function() {
        console.info('leave '+ this.name);
    }
});

one

The handler attached to the size will executed at most once.

Enable
Disable

Breakpoints.one('md', 'enter', function(){
    console.info('this only appear once when enter md');
});

off

Remove event handlers attached to size.

// remove all events bind to sm size
Breakpoints.off('sm');
// remove all enter type events bind to md size
Breakpoints.off('md', 'enter');
// remove specific event handler
var enterHandler = function(){};
Breakpoints.on('lg', 'enter', enterHandler);
Breakpoints.off('lg', {
    enter: enterHandler
})
// alternative way
Breakpoints.off('lg', 'enter', enterHandler);

change

Attach an event handler to the size change event.

On Change
Off Change

// attach handler to change event
Breakpoints.on('change', function(){
    console.info('enter ' + this.current.name);
    console.info('leave ' + this.previous.name);
});
// altrnative example
var changeHandler = function(){
    // do something
};
Breakpoints.on('change', changeHandler);
// remove the handler
Breakpoints.off('change', changeHandler);
// remove all change handlers
Breakpoints.off('change');