Skip to content

Instantly share code, notes, and snippets.

@haggen
Created May 24, 2011 17: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 haggen/989253 to your computer and use it in GitHub Desktop.
Save haggen/989253 to your computer and use it in GitHub Desktop.
Make targeted elements scroll with the window
/**
* Squire v1.0
* Make targeted elements scroll with the window
* by Arthur Corenzan <arthur@corenzan.com>
* Creative Commons-Attribution-Share Alike
* http://creativecommons.org/licenses/by-sa/3.0
*
* Under development...
*/
(function($) {
$.fn.squire = function(o) {
o = $.extend({
classname: "fixed"
}, o);
var squires = [],
$win = $(window);
if($.browser.msie && $.browser.version == "8.0") {
alert("IE8");
$(document).add("html, body");
}
$win.scroll(function(e) {
var i;
for(i in squires) {
if(squires.hasOwnProperty(i)) {
//if it scrolled past the threshold and is not fixed
if($win.scrollTop() > squires[i].offsetTop &&
!squires[i].hasClass(o.classname)) {
squires[i].addClass(o.classname);
//if it still over the threshold and is fixed
} else if($win.scrollTop() < squires[i].offsetTop &&
squires[i].hasClass(o.classname)) {
squires[i].removeClass(o.classname);
}
}
}
});
return this.each(function(i, that) {
var $that = $(that);
$that.offsetTop = $that.offset().top;
squires.push($that);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment