Skip to content

Instantly share code, notes, and snippets.

@seangaffney
Created January 20, 2012 16:58
Show Gist options
  • Save seangaffney/1648386 to your computer and use it in GitHub Desktop.
Save seangaffney/1648386 to your computer and use it in GitHub Desktop.
Fix That Ish - A plugin for fixing things (one thing in particular) in the browser window. No support for IE8 and below.
/*
* jQuery sgFixThatIsh v0.1 :)
* http://seangaffney.cc
* Copyright 2012, Sean Gaffney
*/
(function( $ ){
var opts = { bottomPadding: 8 };
var $header, $content, $win;
var contWidth, currentContWidth, headHeight, headWidth,
footHeight, viewHeight, viewWidth, docHeight;
var methods = {
init : function( options ) {
return this.each(function(){
$header = $(this),
$content = $('#content'),
$win = $(window);
contWidth = $('body > .container').width(),
headHeight = $header.outerHeight(true),
headWidth = $header.outerWidth(true),
footHeight = $('footer').outerHeight(true),
viewHeight = $win.height(),
viewWidth = $win.width(),
docHeight = $(document).height();
$win.bind('resize.fixthatish', function(event) {
currentContWidth = $('body > .container').width(),
headHeight = $header.outerHeight(true),
headWidth = $header.outerWidth(true),
viewHeight = $win.height(),
viewWidth = $win.width();
if ( (currentContWidth != contWidth) && (currentContWidth >= 768) ) {
$header.sgFixThatIsh();
contWidth = currentContWidth;
$win.scroll();
} else if (($header.data('fixthatish') == true) && currentContWidth < 768) {
$header.sgFixThatIsh('unfix');
}
if ( ($header.data('fixthatish') == true) && headHeight > viewHeight ) {
$header.sgFixThatIsh('unfix');
} else if ( ($header.data('fixthatish') == false) && headHeight < viewHeight && currentContWidth >= 768 ) {
$header.sgFixThatIsh();
$win.scroll();
}
});
if ( headHeight < viewHeight && viewWidth >= 768 ) {
$win.bind('scroll.fixthatish', function(event) {
if ( $win.scrollTop() > docHeight - (headHeight + footHeight + opts.bottomPadding) ) {
$header.css({
'position': 'absolute',
'left': 0,
'top': docHeight - ( headHeight + footHeight + opts.bottomPadding )
});
} else {
$header.css({
'position': 'fixed',
'top': 'auto',
'left': 'auto',
'display': 'block',
'float': 'none'
});
}
$content.css({
'position': 'relative',
'left': headWidth
});
});
$header.data('fixthatish', true);
} else {
$header.data('fixthatish', false);
}
});
},
unfix : function( ) {
$win.unbind('scroll.fixthatish');
$header.css({
'position': '',
'top': '',
'left': '',
'display': '',
'float': ''
});
$content.css({
'position': '',
'left': ''
});
$header.data('fixthatish', false);
},
destroy : function( ) {
return this.each(function(){
$header.sgFixThatIsh('unfix');
$win.unbind('.fixthatish');
$header.removeData('fixthatish');
});
}
};
$.fn.sgFixThatIsh = function( method ) {
if((!jQuery.browser.msie) || (jQuery.browser.msie && jQuery.browser.version.substring(0, 1) >= 9)){
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.sgFixThatIsh' );
}
}
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment