diff --git a/combo/public/static/js/combo.public.js b/combo/public/static/js/combo.public.js index 4d1139e2..eb3ec073 100644 --- a/combo/public/static/js/combo.public.js +++ b/combo/public/static/js/combo.public.js @@ -76,6 +76,44 @@ function combo_modify_query_string(name, value) { return search; } +/* Launch callbacks when scroll move above and below a limit + * new ComboScrollY({ + * limit: int, + * below: callback function, + * above: callback function + * }) + */ +function ComboScrollY(options) { + this.defaults = { + limit: 0 + }; + this.options = options; + this.init(); +}; +ComboScrollY.prototype.init = function(){ + this.options = $.extend({}, this.defaults, this.options); + this.callbacks(); + window.addEventListener('scroll', this.callbacks.bind(this)); +}; +ComboScrollY.prototype.position = function(){ + return (window.pageYOffset <= this.options.limit) ? "above" : "below"; +}; +ComboScrollY.prototype.update = function(){ + return (this.position() === this.last_callback_position) ? false : true; +}; +ComboScrollY.prototype.callbacks = function() { + if (this.update()) { + if (this.position() === "below") { + this.options.below(); + this.last_callback_position = "below"; + } else { + this.options.above(); + this.last_callback_position = "above"; + } + } +}; + + $(function() { $('[data-ajax-cell-refresh]').each(function(idx, elem) { var $elem = $(elem); @@ -247,10 +285,11 @@ $(function() { $(document).on('combo:cell-loaded', function(ev, cell) { prepare_foldable(cell); }); /* add a scrolled class to body once the user scrolled the page a bit */ - $(window).on('scroll', function() { - if ($(window).scrollTop() == 0) { + var body_is_scrolled = new ComboScrollY({ + above: function(){ $('body').removeClass('scrolled'); - } else { + }, + below: function(){ $('body').addClass('scrolled'); } });