Skip to content

Instantly share code, notes, and snippets.

@janbaykara
Last active August 29, 2015 14:13
Show Gist options
  • Save janbaykara/51c017a91471fc951c98 to your computer and use it in GitHub Desktop.
Save janbaykara/51c017a91471fc951c98 to your computer and use it in GitHub Desktop.
LIB / JQUERY STICKY_ELEMENT
/* {
$element: $("selector"),
sidebarInitY: 0,
parentH: 0,
containerH: 0,
containerY: 0,
headerHeight: 0,
newHeight: 0
} */
var stickyScroller = function (config) {
var stickyScroller = this
// Sidebar sticky
$(document).ready(function() {
$(window).scroll(function(e) {
config.$element.css(
stickyScroller.newY({
offsetY: config.containerY,
elH: config.parentH,
minY: config.containerY,
maxY: config.containerY + config.containerH,
winMinY: 0,
winMaxY: 0
})
)
});
});
}
/* {
offsetY: '.',
elH: '.',
minY: '.',
maxY: '.',
winMinY: '.',
winMaxY: '.',
} */
stickyScroller.prototype.newY = function(config) {
var windowTop = $(window).scrollTop(),
newY = posTop = windowTop + config.winMinY,
property = 'top',
style = {};
if( (windowTop + config.winMinY) > config.minY // Below container top
&& (windowTop + config.elH + config.winMaxY) < config.maxY) // Above container bottom
{
console.log("Passed MINIMUM height");
style = {
bottom: 'auto',
top: windowTop + config.winMinY - config.offsetY
}
}
else
if(windowTop + config.winMinY < config.minY) { // Before container top
console.log("NOT past MINIMUM height");
style = {
bottom: 'auto',
top: 0
}
}
else
if(windowTop + config.elH + config.winMinY > config.maxY - config.winMaxY) { // Beyond container bottom
console.log("Past MAX height");
style = {
top: config.maxY - config.minY - config.elH,
bottom: 'auto'
}
}
return style;
}
// SiteNotice sticky scrolling
// var siteNotice = new stickyScroller({
// $element: $(".js-snap"),
// sidebarInitY: 0,
// parentH: $(document).height(),
// containerH: $(document).height(),
// containerY: $(document).height(),
// headerHeight: $(".global-header").height(),
// newHeight: 0
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment