Skip to content

Instantly share code, notes, and snippets.

View dustintheweb's full-sized avatar
🎯
Focusing

Dustin Hoffmann dustintheweb

🎯
Focusing
View GitHub Profile
@dustintheweb
dustintheweb / clean-nav-bubble-promise-trigger.js
Created April 1, 2014 22:53
Event Bubbling, Promises & Triggers - for a clean navigation structure
// in something.html
// <div>
// <a href="" data-page-state="isAbout">lorem ipsum</a>
// </div>
//promises
$('body').on('isAbout', function(){
// do something
});
$('body').on('isNews', function(){
@dustintheweb
dustintheweb / javascript-outside-click-examples.js
Last active August 29, 2015 13:57
If you click outside of a specific div, do something
$('body').on('touchstart click', $staticSection, function(e) {
var $target = $(e.target);
if(!$target.closest($someSection).length) {
// do something... ex: $someSection.removeClass('open');
}
});
// conditional example
$('body').on('touchstart click', $staticSection, function(e) {
@dustintheweb
dustintheweb / switch-statement-example-syntax.js
Last active August 29, 2015 13:57
Switch Statement Example / Syntax
var yourFunction = function(id) {
var _somePath,
_img,
conditionA = !! (window.someGlobalVar === 5), // if you need internal ternary conditions (path switching for example)
conditionB = !! (window.someGlobalVar === 10),
conditionC = !! (window.someGlobalVar === 15);
switch (id) {
case 'some-id-name-1':
// do something
@dustintheweb
dustintheweb / force-safari-to-redraw.js
Created March 24, 2014 22:33
Force Safari to redraw when it's being a slow idiot
var someFn = function() {
$('<style></style>').appendTo($(document.body)).remove(); // force safari redraw
};
// Released under MIT license: http://www.opensource.org/licenses/mit-license.php
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() === input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
@dustintheweb
dustintheweb / js-jquery-var-function-syntax.js
Last active December 23, 2015 14:49
Various var & function syntax used in JS & jQuery
// Var & Function Syntax // WIP
var someName; // a variable on the local scope
someName; // a variable on the global scope
window.someName; // a production friendly variable on the global scope
// note: when making a reference to any var with a specific namespace,
// make sure you specify it in the call
@dustintheweb
dustintheweb / loop-through-array.js
Created August 28, 2013 11:55
Parse through an array with a loop & print out its contents
var cities = ["Melbourne", "Amman", "Helsinki", "NYC", "Tulsa", "Nome"];
for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
}
@dustintheweb
dustintheweb / for-loop-syntax.js
Created August 28, 2013 11:22
various examples of for loops & syntax
for (var i=1; i<26; i = i++) { // counting from 1 to 25
console.log(i);
}
for (var i = 5; i < 51; i+=5) { // counting from 5 to 50 in increments of 5
console.log(i);
}
for (var i=8; i<120; i+=12) { // counting from 8 to 119 in increments of 12
console.log(i);
@dustintheweb
dustintheweb / inject-url-suffix-to-object.js
Created August 22, 2013 17:47
Inject the last part of the current page url into an object (class, id, etc)
var url = document.referrer,
urlsplit = url.split("/").pop();
$('body').addClass(urlsplit);
@dustintheweb
dustintheweb / jquery-toggle-replacement-syntax.js
Last active December 21, 2015 10:09
jQuery Replacement Syntax for Depreciated Toggle Function
$('#element').click(function() {
if ($(this).data('clicked')) {
$(this).css({'display': 'block'});
$(this).data('clicked', false);
} else {
$(this).css({'display': 'none'});
$(this).data('clicked', true);
}
});