Skip to content

Instantly share code, notes, and snippets.

@cristianobecker
Created June 15, 2015 00:42
Show Gist options
  • Save cristianobecker/b76e42115004469dee0c to your computer and use it in GitHub Desktop.
Save cristianobecker/b76e42115004469dee0c to your computer and use it in GitHub Desktop.
Main.js file when using jQuery
/*global jQuery */
/*jslint browser: true */
(function (win, $) {
'use strict';
// helpers
var Util, Globals;
Util = {
safeEvent: function (callback) {
var a = false;
return function () {
var args = arguments;
if (!a) {
a = true;
win.requestAnimationFrame(function () {
callback.apply(null, args);
a = false;
});
}
};
},
timeoutEvent: function (timeout, callback) {
var interval = null;
return function () {
var args = arguments;
clearTimeout(interval);
setTimeout(function () {
callback.apply(null, args);
}, timeout);
};
}
};
Globals = {
$win: $(win),
$body: $('body'),
$html: $('html')
};
// events
Globals.$win.on('scroll', Util.safeEvent(function () {
Globals.$win.trigger('safescroll', {
top: Globals.$win.scrollTop(),
viewport: Globals.$win.height(),
height: Globals.$html.height()
});
}));
// configuration
win.Util = Util;
win.Globals = Globals;
}(window, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment