Created
May 26, 2011 01:52
-
-
Save blankyao/992399 to your computer and use it in GitHub Desktop.
jQuery Floating Widget in javascript
This file contains hidden or 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
(function ($) { | |
$.fn.floatingWidget = function () { | |
return this.each(function () { | |
var $this = $(this), | |
$parent = $this.offsetParent(), | |
$window = $(window), | |
top = $this.offset().top - parseFloat($this.css('marginTop').replace(/auto/, 0)), | |
bottom = $parent.offset().top + $parent.height() - $this.outerHeight(true), | |
floatingClass = 'floating', | |
pinnedBottomClass = 'pinned-bottom'; | |
if ($parent.height() > $this.outerHeight(true)) { | |
$window.scroll(function () { | |
var y = $window.scrollTop(); | |
if (y > top) { | |
$this.addClass(floatingClass); | |
if (y > bottom) { | |
$this.removeClass(floatingClass).addClass(pinnedBottomClass); | |
} else { | |
$this.removeClass(pinnedBottomClass); | |
} | |
} else { | |
$this.removeClass(floatingClass); | |
} | |
}); | |
} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment