(function ($) {
  
  // http://davidwalsh.name/ways-css-javascript-interact
  function addCSSRule (sheet, selector, rules, index) {
   if (sheet.insertRule) {
      sheet.insertRule(selector + '{ ' + rules + ' }', index)
    } else {
      sheet.addRule(selector, rules, index)
    }   
  }
  
  function heightOffset (elem) {
    var height = 0
    
    elem.prevAll(':visible').each(function (_, sibling) {
      height += $(sibling).outerHeight()
    })
    
    return height || elem.outerHeight()
  }
  
  $.fn.sticky = function () {
    var elem = $(this),
        nextVisible = elem.siblings().first(':visible'),
        stuck = false,
        styleSheet = document.styleSheets[0]
        
    addCSSRule(styleSheet, '.stuck', 'position: fixed !important; top: 0 !important; z-index: 10')
  
    $(window).on('scroll', function () {
      if ($(this).scrollTop() > heightOffset(elem)) {
        
        if (stuck) return;
        
        elem.addClass('stuck')
        nextVisible.css('margin-top', elem.outerHeight())
        stuck = true
        
      } else {
        elem.removeClass('stuck')
        nextVisible.css('margin-top', "")
        stuck = false
      }
    })
  }
})(jQuery)

@@ -328,156 +328,32 @@
     
-var height = 0%0A    %0A    elem.prevAll(':visible').each(function (_, sibling) %7B%0A      height += $(sibling).outerHeight()%0A    %7D)%0A    %0A    return height
+return elem.offset.top()
  %7C%7C 

Use offset rather than calculating position by hand