Skip to content

Instantly share code, notes, and snippets.

@koohq
Last active January 8, 2018 11:55
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 koohq/5ddf924faca89bcfca26b28397ef5d0c to your computer and use it in GitHub Desktop.
Save koohq/5ddf924faca89bcfca26b28397ef5d0c to your computer and use it in GitHub Desktop.
Freezing of the SharePoint List/Library header
/**
* jquery.sp-viewheaderfreezer v1.0.0
* jQuery plugin to freeze the list-view-header on SharePoint pages.
*
* (c) 2017 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
(function($, cn) {
$.fn.freezeSPViewHeader = function(h, w) {
var $t = this;
if (!$t.hasClass('ms-listviewtable') || $t.parent().hasClass(cn)) { return this; }
var $d = $('<div>').addClass(cn).css('overflow', 'scroll');
if (typeof h === 'number') { $d.height(h); }
if (typeof w === 'number') { $d.width(w); }
this.wrap($d);
var $h = this.find('.ms-viewheadertr > th').css('background-color', 'white');
this.parent('.' + cn).on('scroll', function(e) { $h.css('transform', 'translateY(' + e.target.scrollTop + 'px)'); });
return this;
};
})(jQuery, 'sp-frozen-wrapper');
/**
* sp-viewheaderfreezer v1.0.0
* Provides a function to freeze the list-view-header on SharePoint pages(without jQuery dependency).
*
* (c) 2017 koohq. Licensed under CC0.
* https://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
var SPViewHeaderFreezer = (function(cn) {
function freeze(s, h, w) {
var t = document.querySelector(s);
if (typeof t === 'undefined' || t.className.indexOf('ms-listviewtable') === -1 || t.parentNode.className.indexOf(cn) !== -1) { return; }
var d = document.createElement('div');
d.className = cn;
d.style.overflow = 'scroll';
if (typeof h === 'number') { d.style.height = h + 'px'; }
if (typeof w === 'number') { d.style.width = w + 'px'; }
d.innerHTML = t.outerHTML;
t.outerHTML = d.outerHTML;
var p = document.querySelector(s).parentNode;
var hs = p.querySelectorAll('.ms-viewheadertr > th');
for (var i = 0; i < hs.length; i++) { hs[i].style.backgroundColor = 'white'; }
p.addEventListener('scroll', function(e) {
for (var i = 0; i < hs.length; i++) { hs[i].style.transform = 'translateY(' + e.target.scrollTop + 'px)'; }
});
}
return Object.freeze({
freeze: freeze
});
})('sp-frozen-wrapper');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment