Skip to content

Instantly share code, notes, and snippets.

@chrisdigital
Created July 16, 2012 16:39
Show Gist options
  • Save chrisdigital/3123675 to your computer and use it in GitHub Desktop.
Save chrisdigital/3123675 to your computer and use it in GitHub Desktop.
This is modified code to work with mootools to use variables in the sticky element code and give flexibility to the selector being targeted.
// Scrolling sidebar for your website
// Downloaded from Marcofolio.net
// Read the full article: http://www.marcofolio.net/webdesign/create_a_sticky_sidebar_or_box.html
// This has a mootools.js dependency
// Modified by Chris Carvey Eyetrackshop, Inc.
window.onscroll = function()
{
///////////// Variables ///////////////
//Change selector and these numbers....
//Insert target selector
var targetID = 'content';
//Insert left position
var leftPos = 767;
//Insert top position (start)
var topPos = 476;
//Insert top position (when sticky fires)
var topPosSticky = 476;
///////////// End Variables ///////////////
//Leave the rest of this alone....
if ( window.XMLHttpRequest ) {
if (document.documentElement.scrollTop > topPos || self.pageYOffset > topPos) {
$(targetID).style.position = 'fixed';
$(targetID).style.top = topPos + 'px';
$(targetID).style.left = leftPos + 'px';
} else if (document.documentElement.scrollTop < topPos || self.pageYOffset < topPos) {
$(targetID).style.position = 'absolute';
$(targetID).style.top = topPosSticky + 'px';
$(targetID).style.left = leftPos + 'px';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment