code: function(container, item) {        
              var borderTop = parseFloat(container.css('borderTopWidth')) || 0,
              paddingTop = parseFloat(container.css('paddingTop')) || 0,
              offset = item.offset().top - container.offset().top - borderTop - paddingTop,
              scroll = container.scrollTop(),
              elementHeight = container.height(),
              itemHeight = item.outerHeight(true);
  
              if(offset < 0) {
                  container.scrollTop(scroll + offset);
              }
              else if((offset + itemHeight) > elementHeight) {
                  container.scrollTop(scroll + offset - elementHeight + itemHeight);
              }
          }
docs: Aligns container scrollbar to keep item in container viewport, algorithm copied from jquery-ui menu widget