Created
May 24, 2011 17:55
-
-
Save haggen/989253 to your computer and use it in GitHub Desktop.
Make targeted elements scroll with the window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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