Skip to content

Instantly share code, notes, and snippets.

@randgroupmrkt
Created July 21, 2016 15:41
Show Gist options
  • Select an option

  • Save randgroupmrkt/e06467f5373adb6094ca5c13eea22c20 to your computer and use it in GitHub Desktop.

Select an option

Save randgroupmrkt/e06467f5373adb6094ca5c13eea22c20 to your computer and use it in GitHub Desktop.
jquery-fixed-header.js : Fixed header for tables on scroll
////////////////////////////////
/// FIXED HEADER
/// github @ chrisbautista 2016-07-20
///////////////////////////////
(function($,window) {
'use strict';
var fh = {}; // fixed header
fh.init = function init(_select, _select_parent) {
this.config = {
'class' : _select || '.fixed-header',
'container' : _select_parent || 'table'
}
}; //fh init
fh.handlers = function handlers(){
var $select = $(this.config.class);
var $select_parent = $select.closest(this.config.container);
var $pinned = false;
var $clone;
function scroll_style() {
var window_top = $(window).scrollTop();
var div_top = $select_parent.offset().top;// + parseInt($select.css('height'));
var div_bottom =$select_parent.offset().top + parseInt($select_parent.css('height'));
//console.log(window_top, div_top, $pinned);
if((window_top > div_bottom)&&($pinned)){
$pinned = false;
$clone.remove();
} else if ((window_top > div_top)&&(window_top < div_bottom)&&(!$pinned)){
$clone = $select.clone();
$select_parent.prepend($clone);
$clone.attr('style','width:'+ $('.table').css('width') +';display:inline-block; position:fixed; top:0;');
$pinned = true;
}else if((window_top <= div_top)&&($pinned)){
$pinned = false;
$clone.remove()
}
}
$(window).scroll(scroll_style);
}; //handlers
fh.run = function run(_select, _select_parent) {
if($(_select).length>0){
this.init(_select,_select_parent);
this.handlers();
}
}; ///fhrun
//
// Run Plugin
//
window.fh = fh;
}(jQuery,window));
// sample usage
// fh.run('.erp-comparison .table tbody tr.info');
fh.run(); // check for .fixed-header classes
////////////////////////////////
/// END FIXED HEADER
///////////////////////////////
@randgroupmrkt
Copy link
Copy Markdown
Author

tested on Chrome Version 51+
tested on IE Edge windows 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment