Created
June 22, 2012 15:50
-
-
Save shanejdonnelly/2973629 to your computer and use it in GitHub Desktop.
React to responsive events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Plugin Name : responsiveEvents | |
* Author : Shane Donnelly | |
*/ | |
(function($){ | |
$.fn.responsiveEvents = function(options){ | |
var defaults = { | |
'phone_portrait' : true, | |
'pp_func' : false, | |
'phone_landscape' : true, | |
'pl_func' : false, | |
'tablet_portrait' : true, | |
'tp_func' : false, | |
'tablet_landscape' : true, | |
'tl_func' : false, | |
'large_desktop' : true, | |
'ld_func' : false, | |
'all_func' : false | |
}; | |
//call in the default options | |
var options = $.extend(defaults, options); | |
return this.each(function() { | |
var $element = $(this); | |
function initEvents(){ | |
//phone portrait | |
if($element.width() < 320 && options.phone_portrait){ | |
$('html').alterClass('response*', 'response_pp'); | |
if(options.pp_func || options.all_func){ | |
options.all_func ? options.all_func() : options.pp_func(); | |
} | |
} | |
//phone landscape | |
else if($element.width() < 480 && options.phone_landscape){ | |
$('html').alterClass('response*', 'response_pl'); | |
console.log('pl'); | |
if(options.pl_func || options.all_func){ | |
options.all_func ? options.all_func() : options.pl_func(); | |
} | |
} | |
//tablet portrait | |
else if($element.width() < 768 && options.tablet_portrait){ | |
$('html').alterClass('response*', 'response_tp'); | |
if(options.tp_func || options.all_func){ | |
options.all_func ? options.all_func() : options.tp_func(); | |
} | |
} | |
//tablet landscape | |
else if($element.width() < 1024 && options.tablet_landscape){ | |
$('html').alterClass('response*', 'response_tl'); | |
if(options.tl_func || options.all_func){ | |
options.all_func ? options.all_func() : options.tl_func(); | |
} | |
} | |
//large desktop | |
else if(options.large_desktop){ | |
$('html').alterClass('response*', 'response_ld'); | |
if(options.ld_func || options.all_func){ | |
options.all_func ? options.all_func() : options.ld_func(); | |
} | |
} | |
} | |
initEvents(); | |
$(window).smartresize(function(){initEvents();}); | |
}); | |
} | |
})(jQuery); | |
/** | |
* jQuery alterClass plugin | |
* | |
* Remove element classes with wildcard matching. Optionally add classes: | |
* $( '#foo' ).alterClass( 'foo-* bar-*', 'foobar' ) | |
* | |
* Copyright (c) 2011 Pete Boere (the-echoplex.net) | |
* Free under terms of the MIT license: http://www.opensource.org/licenses/mit-license.php | |
* | |
*/ | |
(function ( $ ) { | |
$.fn.alterClass = function ( removals, additions ) { | |
var self = this; | |
if ( removals.indexOf( '*' ) === -1 ) { | |
// Use native jQuery methods if there is no wildcard matching | |
self.removeClass( removals ); | |
return !additions ? self : self.addClass( additions ); | |
} | |
var patt = new RegExp( '\\s' + | |
removals. | |
replace( /\*/g, '[A-Za-z0-9-_]+' ). | |
split( ' ' ). | |
join( '\\s|\\s' ) + | |
'\\s', 'g' ); | |
self.each( function ( i, it ) { | |
var cn = ' ' + it.className + ' '; | |
while ( patt.test( cn ) ) { | |
cn = cn.replace( patt, ' ' ); | |
} | |
it.className = $.trim( cn ); | |
}); | |
return !additions ? self : self.addClass( additions ); | |
}; | |
})( jQuery ); | |
// debouncing function from John Hann | |
// http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/ | |
(function($,sr){ | |
var debounce = function (func, threshold, execAsap) { | |
var timeout; | |
return function debounced () { | |
var obj = this, args = arguments; | |
function delayed () { | |
if (!execAsap) | |
func.apply(obj, args); | |
timeout = null; | |
}; | |
if (timeout) | |
clearTimeout(timeout); | |
else if (execAsap) | |
func.apply(obj, args); | |
timeout = setTimeout(delayed, threshold || 100); | |
}; | |
} | |
// smartresize | |
jQuery.fn[sr] = function(fn){ return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); }; | |
})(jQuery,'smartresize'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment