Skip to content

Instantly share code, notes, and snippets.

@elricco
Created June 20, 2018 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elricco/ca06b641740bdfd64f4299f4c95747c8 to your computer and use it in GitHub Desktop.
Save elricco/ca06b641740bdfd64f4299f4c95747c8 to your computer and use it in GitHub Desktop.
//this is specific for sonata admin - but can be tweaked for other purposes
//was to lazy to write a class
$(function () {
window.setTimeout(function () {
var elementExists = !!document.getElementsByClassName('.tablesaw-bar');
if(elementExists) {
makeTablesawBarSticky();
}
}, 100);
function makeTablesawBarSticky() {
var distance = $('.tablesaw-overflow').offset().top - 122, $window = $(window);
$window.scroll(function() {
if ( $window.scrollTop() >= distance ) {
//make bar sticky when tablehead reaches offset
$('.tablesaw-bar').css({
'position': 'fixed',
'top': '102px',
'background': '#f5f5f5',
'width': $('.content-wrapper').outerWidth() + 'px',
'margin': '0 -15px',
'padding': '0 15px',
'border' : '1px solid #CCC',
'z-index': '999'
});
}
else {
//reset css and unstick bar
$('.tablesaw-bar').css({
'position': '',
'top': '',
'background': '',
'width': '',
'margin': '',
'padding': '',
'border' : '',
'z-index': ''
});
}
});
window.addEventListener("resize",function() {
if ( $window.scrollTop() >= distance ) {
window.setTimeout(function () {
//resize bar when window resizes
$('.tablesaw-bar').css('width', $('.content-wrapper').outerWidth() + 'px');
}, 360);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment