Skip to content

Instantly share code, notes, and snippets.

@ZeeAgency
Last active May 17, 2024 04:00
Show Gist options
  • Save ZeeAgency/5244572 to your computer and use it in GitHub Desktop.
Save ZeeAgency/5244572 to your computer and use it in GitHub Desktop.
UI Module Snippet
define(function() {
var onscrollCallbacks = [],
onresizeCallbacks = [],
didScroll = false,
didResize = false;
var UI = {
init: function() {
// Caching but nothing too specific here
UI.$window = $(window);
UI.$body = $(document.body);
UI.$htmlBody = $(document.documentElement).add(document.body);
UI.$form = $('form');
UI.$fields = $('input, textarea');
UI.$medias = $('video, audio');
UI.$primaryNav = $('.primary-nav');
UI.$main = $('.main');
// Better scroll & resize events
UI.$window
.scroll(function() { didScroll = true; })
.resize(function() { didResize = true; });
setInterval(function() {
if(didScroll) {
didScroll = false;
UI.scroll();
}
if(didResize) {
didResize = false;
UI.resize();
}
}, 250);
},
onscroll: function(callback) {
onscrollCallbacks.push(callback);
return UI;
},
onresize: function(callback) {
onresizeCallbacks.push(callback);
return UI;
},
scroll: function() {
for(var i = 0, l = onscrollCallbacks.length; i < l; i++) {
onscrollCallbacks[i]();
}
},
resize: function() {
for(var i = 0, l = onscrollCallbacks.length; i < l; i++) {
onscrollCallbacks[i]();
}
}
};
return UI;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment