Skip to content

Instantly share code, notes, and snippets.

@fiinix
Last active January 3, 2016 20:35
Show Gist options
  • Save fiinix/1cb6daa23da25ecfe23a to your computer and use it in GitHub Desktop.
Save fiinix/1cb6daa23da25ecfe23a to your computer and use it in GitHub Desktop.
Take jQuery and ruin it. An attempt to create a small subset of jQuery's functionality that removes the jQuery dependency. Goal: Element selector, addClass, removeClass, toggleClass, click.eventListener https://docs.google.com/document/d/1LPaPA30bLUB_publLIMF0RlhdnPx_ePXm7oW02iiT6o/preview
// Put in <head>
if ( 'querySelector' in document && 'addEventListener' in window ) {
document.documentElement.className += 'js';
}
// Does it dijon?
if ( 'querySelector' in document && 'addEventListener' in window && 'classList' in document.createElement('_') ) {
//Map $ to querySelector
function $(target){
return document.querySelector(target);
}
//ready()
document.addEventListener('DOMContentLoaded', function() {
// Traverse DOM and create variables
var html = $('html'),
nav = $('.js-navigation'),
navToggler = $('.js-nav-toggler');
// Replace the default `.no-js` with `.js`
html.className = html.className.replace( /(?:^|\s)no-js(?!\S)/g , 'js' );
// Listen for click event, toggle class names
navToggler.addEventListener('click', function(e) {
nav.classList.toggle('is-active');
navToggler.classList.toggle('is-active');
}, false);
});
//End of ready()
}
// End of dijon
var arrTimes = [];
var i = 0; // start
var timesToTest = 5;
var tThreshold = 150; //ms
var testImage = "http://www.google.com/images/phd/px.gif"; // small image in your server
var dummyImage = new Image();
var isConnectedFast = false;
testLatency(function(avg){
isConnectedFast = (avg <= tThreshold);
/** output */
document.body.appendChild(
document.createTextNode("Time: " + (avg.toFixed(2)) + "ms - isConnectedFast? " + isConnectedFast)
);
});
/** test and average time took to download image from server, called recursively timesToTest times */
function testLatency(cb) {
var tStart = new Date().getTime();
if (i<timesToTest-1) {
dummyImage.src = testImage + '?t=' + tStart;
dummyImage.onload = function() {
var tEnd = new Date().getTime();
var tTimeTook = tEnd-tStart;
arrTimes[i] = tTimeTook;
testLatency(cb);
i++;
};
} else {
/** calculate average of array items then callback */
var sum = arrTimes.reduce(function(a, b) { return a + b; });
var avg = sum / arrTimes.length;
cb(avg);
}
}
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
var scrollEventFired = false;
// Set throttle to true when a scroll event fires
viewport.scroll(function() {
// scrollEventFired = true;
});
// Allow scroll event to run its course every 250ms
setInterval(function() {
if (scrollEventFired) {
scrollEventFired = false;
// Check distance from top
if (viewport.scrollTop() > distance ) {
header.addClass('is-fixed');
}
else {
header.removeClass('is-fixed');
}
}
}, 250);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment