Skip to content

Instantly share code, notes, and snippets.

@mjpitz
Last active November 19, 2015 13:49
Show Gist options
  • Save mjpitz/ad86956f9a8363e25131 to your computer and use it in GitHub Desktop.
Save mjpitz/ad86956f9a8363e25131 to your computer and use it in GitHub Desktop.
Make the project banner on a repository page sticky as you scroll down (new ui only)
(function() {
var $ = function(str) {
return document.querySelector(str);
};
var win = window;
var doc = document.documentElement;
var mast = $('#js-repo-pjax-container .pagehead');
var mastTop = mast.offsetTop;
var adjustedMargin = mast.offsetHeight;
var currentMastAttributes = mast.getAttribute('style');
var newMastAttributes = [
currentMastAttributes,
'position: fixed;',
'z-index: 10;',
'top: 0;',
'left: 0;',
'right: 0;'
].join('');
var content = $('#js-repo-pjax-container .repo-container');
var currentContentAttributes = content.getAttribute('style');
var newContentAttributes = [
currentContentAttributes,
'margin-top: ' + adjustedMargin + 'px;'
].join('');
var toggle = false;
document.addEventListener('scroll', function() {
var scrollTop = (win.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
if (scrollTop > mastTop) {
content.setAttribute('style', newContentAttributes);
mast.setAttribute('style', newMastAttributes);
toggle = true;
setTimeout(function() {toggle = false}, 100);
} else if (!toggle) {
content.setAttribute('style', currentContentAttributes);
mast.setAttribute('style', currentMastAttributes);
}
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment