Skip to content

Instantly share code, notes, and snippets.

@davidcostadev
Created August 21, 2016 18:47
Show Gist options
  • Save davidcostadev/041dca0e7f9b7a70614a55ebb9af9729 to your computer and use it in GitHub Desktop.
Save davidcostadev/041dca0e7f9b7a70614a55ebb9af9729 to your computer and use it in GitHub Desktop.
Fixed sidebar on scrooll with little jquery
function FixedSidebar(sidebarName) {
var self = this;
this.elSidebar = document.getElementById(sidebarName);
this.elContent = this.elSidebar.parentNode;
this.elBody = document.getElementsByTagName('body')[0];
this.lastBodyHeight = this.elBody.clientHeight;
this.lastSidebarHeight = this.elSidebar.clientHeight;
this.lastContentHeight = this.elContent.clientHeight;
}
FixedSidebar.prototype = {
verifyChange : function (self, callback) {
if(self.elBody.clientHeight != self.lastBodyHeight) {
self.lastBodyHeight = self.elBody.clientHeight;
callback(self);
} else if(self.elSidebar.clientHeight != self.lastSidebarHeight) {
self.lastSidebarHeight = self.elSidebar.clientHeight;
callback(self);
// } else if(self.elContent.clientHeight != self.lastContentHeight) {
// self.lastContentHeight = self.elContent.clientHeight;
// callback(self);
}
},
listenHeightChange : function () {
var self = this;
setInterval(function () {
self.verifyChange(self, self.updateVar);
}, 100);
},
onScroll : function () {
var self = this;
window.addEventListener('scroll', function () {
self.run(self);
});
},
onHeightChange : function () {
window.addEventListener('heightChange', this.updateVar);
},
updateVar : function (self) {
$(self.elSidebar).removeClass('fixed');
$(self.elSidebar).css('marginTop', '');
$(self.elSidebar).css('width', '');
self.parent = {
top : self.elContent.offsetTop,
bottom : self.elContent.offsetHeight + self.elContent.offsetTop,
height : self.elContent.offsetHeight
};
self.child = {
top : self.elSidebar.offsetTop,
bottom : self.elSidebar.offsetHeight + self.elSidebar.offsetTop,
width : self.elSidebar.offsetWidth,
height : self.elSidebar.offsetHeight
};
self.run(self);
},
run : function (self) {
var cursor = (window.scrollY - self.parent.top) > 0 ? window.scrollY - self.parent.top : 0;
cursorBottom = window.scrollY + window.innerHeight;
if (cursorBottom < self.parent.top) { // se não tiver visivel na parte de cima
var log = 'nao visivel';
$(self.elSidebar).removeClass('fixed');
$(self.elSidebar).css('width', '');
$(self.elSidebar).css('marginTop', '');
} else if (window.scrollY + window.innerHeight > self.child.bottom) {
var limitMargin = self.parent.height - self.child.height;
var marginAtual = (cursor + window.innerHeight) - self.child.height - 10;
if(marginAtual < limitMargin) {
var log = 'ja é para esta fixo';
$(self.elSidebar).addClass('fixed');
$(self.elSidebar).css('width', self.child.width + 'px');
} else {
$(self.elSidebar).removeClass('fixed');
$(self.elSidebar).css('marginTop', limitMargin + 'px');
$(self.elSidebar).css('width', '');
var log = 'pare em baixo';
}
} else {
$(self.elSidebar).removeClass('fixed');
$(self.elSidebar).css('marginTop', '');
$(self.elSidebar).css('width', '');
var log = 'normal';
}
},
doMagic : function () {
var self = this;
this.updateVar(this);
this.onScroll();
this.onHeightChange();
this.listenHeightChange();
},
}
var magic = new FixedSidebar('sidebar').doMagic();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment