How to add back to top using drupal behaviors?
Try wrapping it:

(function ($) {
    Drupal.behaviors.scrollToTop = {
      attach: function (context, setting) {
        $('#back-to-top', context).on('click', function(){
            $('body,html').animate({scrollTop : 0}, 500);
        });
      }
    };
}(jQuery));


Had the same issue, AFAIK the wrapper is necessary to actually fire the behavior.