js: introduce ComboScrollY global function (#45040)

Launch callbacks when page scroll below or above a limit
This commit is contained in:
Thomas Jund 2020-07-11 12:48:56 +02:00 committed by Frédéric Péters
parent 3ac4263250
commit 94bd9c0929
1 changed files with 42 additions and 3 deletions

View File

@ -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');
}
});