Skip to content

Instantly share code, notes, and snippets.

@LucaRosaldi
Created August 5, 2016 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucaRosaldi/88ee68dfbe5e2fe44b63228e7e16a4e9 to your computer and use it in GitHub Desktop.
Save LucaRosaldi/88ee68dfbe5e2fe44b63228e7e16a4e9 to your computer and use it in GitHub Desktop.
Javascript: App boilerplate (ES2015)
/**
* App JS
*
* This is the main scripts file.
*
* Author: PubliOne S.r.l.
* Author URI: http://publione.it
*
* @version 0.1.0
*/
require( './libs/modernizr.js' );
const debounce = require( 'lodash/debounce' );
( function( window, document, undefined ) {
'use strict';
const win = window;
const doc = document;
const htmlEl = doc.getElementsByTagName( 'html' )[0];
const headEl = doc.getElementsByTagName( 'head' )[0];
const bodyEl = doc.getElementsByTagName( 'body' )[0];
const transEndEventNames = {
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'transitionend',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd',
'transition': 'transitionend'
}
const transitionEnd = transEndEventNames[ Modernizr.prefixed( 'transition' ) ];
/**
* Website config.
*/
const config = win.CONFIG || { domain: '', lang: 'it' };
/**
* Track window size.
*/
let winSize = {
width: win.innerWidth,
height: win.innerHeight
}
/**
* Merge two objects.
*
* @param {object} a First object
* @param {object} b Second object
* @return {object} Merged object
*/
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[ key ] = b[ key ];
}
}
return a;
}
/**
* Load asynchronous script.
*
* @param {string} src The script uri
*/
function loadScript( src ) {
let script = doc.createElement('script');
script.type = 'text/javascript';
script.async = 'true';
script.src = src;
headEl.appendChild( script );
}
/**
* Update var which holds window dimensions.
*
* @param {integer} width New width of window
* @param {integer} height New height of window
* @return {object} Updated window size
*/
function updateWinSize( width, height ) {
return winSize = {
width: width,
height: height
}
}
/**
* Add a scrolled class to the body when page has scrolled.
*
* @param {Integer} scrollOffset Number of pixels scrolled before class is applied
* @param {String} scrolledClass Class applied to body element
*/
function toggleScrolledClass( scrollOffset = 0, scrolledClass = 'is-scrolled' ) {
let bodyClassList = bodyEl.classList;
let scrollTop = win.pageYOffset;
( scrollTop >= scrollOffset )
? bodyClassList.add( scrolledClass )
: bodyClassList.remove( scrolledClass );
}
/**
* Initiate all the events.
*/
function init() {
// Update size vars on window resize
win.addEventListener( 'resize', debounce( function() {
updateWinSize( win.innerWidth, win.innerHeight );
}, 250, { trailing: true } ) );
// Add a scrolled class to the body when page has scrolled
win.addEventListener( 'scroll', debounce( function() {
toggleScrolledClass( 150 );
}, 250, { trailing: true } ) );
win.addEventListener( 'load', function() {
// remove loader, ect.
});
}
/**
* Constructor.
*/
return { init: init() };
} )( window, window.document );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment