Permalink
Browse files

Refactored lib.

  • Loading branch information...
1 parent d242ea2 commit e9a0eba716c0303b597c6fa123b1cba235580069 @polarblau committed Oct 9, 2011
Showing with 60 additions and 58 deletions.
  1. +0 −58 jquery.stickysectionheaders.js
  2. +60 −0 src/jquery.stickysectionheaders.js
@@ -1,58 +0,0 @@
-/*!
- * Sticky Section Headers
- *
- * Copyright (c) 2010 Florian Plank (http://www.polarblau.com/)
- * Dual licensed under the MIT (MIT-LICENSE.txt)
- * and GPL (GPL-LICENSE.txt) licenses.
- *
- * USAGE:
- *
- *
- * $('#container').stickySectionHeaders({
- * stickyClass : 'sticky',
- * headlineSelector : 'strong'
- * });
- */
-
-(function($){
- $.fn.stickySectionHeaders = function(options) {
-
- var settings = $.extend({
- stickyClass: 'sticky',
- headlineSelector: 'strong'
- }, options);
-
- return $(this).each(function() {
- var $this = $(this);
- $this.find('ul:first').scroll(function(e) {
- $this.find('ul:first > li').each(function() {
- var t = $(this).position().top;
- var h = $(this).outerHeight();
- var $s = $(this).find(settings.headlineSelector);
- var sh = $s.outerHeight();
- if (t < 0) {
- $(this).addClass(settings.stickyClass).css('paddingTop', sh);
- $s.css({
- 'width': $(this).outerWidth() - $s.cssSum('paddingLeft', 'paddingRight'),
- 'top': (h + t < sh) ? (sh - (t + h)) * -1 : ''
- });
- } else $(this).removeClass(settings.stickyClass).css('paddingTop', '');
- });
- });
- });
- };
-
- /* A little helper to calculate the sum of different
- * CSS properties
- *
- * EXAMPLE:
- * $('#my-div').cssSum('paddingLeft', 'paddingRight');
- */
- $.fn.cssSum = function() {
- var $self = $(this), sum = 0;
- $(arguments).each(function(i, e) {
- sum += parseInt($self.css(e), 10);
- });
- return sum;
- };
-})(jQuery);
@@ -0,0 +1,60 @@
+/*!
+ * Sticky Section Headers
+ *
+ * Copyright (c) 2010 Florian Plank (http://www.polarblau.com/)
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
+ * and GPL (GPL-LICENSE.txt) licenses.
+ *
+ * USAGE:
+ *
+ * $('#container').stickySectionHeaders({
+ * stickyClass : 'sticky',
+ * headlineSelector : 'strong'
+ * });
+ *
+ */
+
+(function($){
+ $.fn.stickySectionHeaders = function(options) {
+
+ var settings = $.extend({
+ stickyClass : 'sticky',
+ headlineSelector: 'strong'
+ }, options);
+
+ return $(this).each(function() {
+ var $this = $(this);
+ $(this).find('ul:first').bind('scroll.sticky', function(e) {
+ $(this).find('> li').each(function() {
+ var $this = $(this),
+ top = $this.position().top,
+ height = $this.outerHeight(),
+ $head = $this.find(settings.headlineSelector),
+ headHeight = $head.outerHeight();
+
+ if (top < 0) {
+ $this.addClass(settings.stickyClass).css('paddingTop', headHeight);
+ $head.css('top', (height + top < headHeight) ? (headHeight - (top + height)) * -1 : '' );
+ } else {
+ $this.removeClass(settings.stickyClass).css('paddingTop', '');
+ }
+ });
+ });
+ });
+ };
+
+ /* A little helper to calculate the sum of different
+ * CSS properties
+ *
+ * EXAMPLE:
+ * $('#my-div').cssSum('paddingLeft', 'paddingRight');
+ */
+ $.fn.cssSum = function() {
+ var $self = $(this), sum = 0;
+ $(arguments).each(function(i, e) {
+ sum += parseInt($self.css(e) || 0, 10);
+ });
+ return sum;
+ };
+
+})(jQuery);

0 comments on commit e9a0eba

Please sign in to comment.