Last active
May 17, 2024 04:00
-
-
Save ZeeAgency/5244572 to your computer and use it in GitHub Desktop.
UI Module Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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