wiggle
Small wrapper around matchMedia
to easily react on changes in page layout. Define your own layout breakpoints and attach listeners to them.
Wiggle.init([{
name:String
minWidth:String|Number
minWidth:String|Number
mediaQuery:String
}])
var screen = Wiggle.init([{
name: 'desktop',
minWidth: 992
}, {
name: 'desktop-menu',
minWidth: '820px'
}, {
name: 'tablet',
minWidth: 768,
maxWidth: 991
}, {
name: 'mobile',
maxWidth: '62em'
}]);
screen.on('mobile', function() {
console.log('Called if screen size is mobile and ever time after screen size changes to mobile');
});
screen.on.change('mobile', function() {
console.log('Called every time after screen size changes to mobile');
});
screen.off('tablet', function() {
console.log('Called if screen size is not tablet and ever time after screen size stops being tablet');
});
if(screen.is('desktop')) {
console.log('Current screen size is desktop');
}
if(screen.is('tablet') && screen.is('desktop-menu')) {
console.log('Based on configuration multiple screens can be active.')
}
var orientation = Wiggle.init([{
name: 'portrait',
mediaQuery: 'orientation: portrait'
}, {
name: 'landscape',
mediaQuery: 'orientation: landscape'
}]);
orientation.on('portrait', function() {
console.log('Called if screen is in portrait mode and every time screen changes to portrait mode');
});
function landscapeListener() {
console.log('Listener will be active until we unsubscribe from it');
}
orientation.on('landscape', landscapeListener);
onSomeEvent(function() {
orientation.unsubscribe(landscapeListener);
});
Supported browsers
Wiggle is using matchMedia to detect layout changes. matchMedia is not supported in IE9 and below.