Skip to content

Instantly share code, notes, and snippets.

@dggodfrey
dggodfrey / smooth scroll
Last active December 23, 2015 22:59
Simple Javascript for smooth scrolling to anchors
//http://css-tricks.com/snippets/jquery/smooth-scrolling/
$(function() {
$('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) {
@dggodfrey
dggodfrey / Clearfix
Created September 25, 2013 22:14
Clearfix
/* http://css-tricks.com/snippets/css/clear-fix/ */
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
@dggodfrey
dggodfrey / in viewport
Created September 26, 2013 15:17
Returns whether the element is visible in the viewport
function isElementInViewport(el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document. documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document. documentElement.clientWidth) /*or $(window).width() */
);
}
@dggodfrey
dggodfrey / image_dimension.js
Last active December 26, 2015 19:19
Simple way to determine the correct dimensions of an image taking into account a max height/width
/**
* Conserve aspect ratio of the orignal region. Useful when shrinking/enlarging
* images to fit into a certain area.
*
* @param {Number} srcWidth Source area width
* @param {Number} srcHeight Source area height
* @param {Number} maxWidth Fittable area maximum available width
* @param {Number} maxHeight Fittable area maximum available height
* @return {Object} { width, heigth }
*/
@dggodfrey
dggodfrey / indexOf Array Prototype
Last active December 27, 2015 19:19
Add indexOf to the Array prototype for browsers that don't support indexOf
//ADD indexof for browsers that don't support it
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
@dggodfrey
dggodfrey / RanDom
Created December 4, 2013 23:54
Randomize items in the dom
//http://james.padolsey.com/javascript/shuffling-the-dom/
(function($){
$.fn.shuffle = function() {
var allElems = this.get(),
getRandom = function(max) {
return Math.floor(Math.random() * max);
},
shuffled = $.map(allElems, function(){
var random = getRandom(allElems.length),
/* modernizr-test.js
* Daniel Ott
* 3 March 2011
* Custom Tests using Modernizr's addTest API
*/
/* iOS
* There may be times when we need a quick way to reference whether iOS is in play or not.
* While a primative means, will be helpful for that.
*/
@dggodfrey
dggodfrey / Get Parameter Value from URL
Created March 13, 2014 19:09
Returns the value of a url parameter
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
@dggodfrey
dggodfrey / ReaderFriendlyNumbers.js
Last active August 29, 2015 14:00
Round Numbers to Reader Friendly format
function abbrNum(number, decPlaces){
//http://stackoverflow.com/questions/2685911/is-there-a-way-to-round-numbers-into-a-reader-friendly-format-e-g-1-1k
// 2 decimal places => 100, 3 => 1000, etc
decPlaces = Math.pow(10,decPlaces);
// Enumerate number abbreviations
var abbrev = [ "k", "m", "b", "t" ];
// Go through the array backwards, so we do the largest first
for (var i=abbrev.length-1; i>=0; i--) {
@dggodfrey
dggodfrey / markdown_base
Last active August 29, 2015 14:07
A base markdown file that can be used for styling elements when rendered to html
#This is an H1
Vivamus auctor condimentum porttitor. Morbi turpis ante, commodo eu aliquet at, posuere non enim. Pellentesque aliquet ornare velit, id mattis lectus finibus quis. Proin non dapibus felis. Etiam id lacus hendrerit, posuere ligula eget, maximus nisl.
##This is an H2
Vivamus auctor condimentum porttitor. Morbi turpis ante, commodo eu aliquet at, posuere non enim. Pellentesque aliquet ornare velit, id mattis lectus finibus quis. Proin non dapibus felis. Etiam id lacus hendrerit, posuere ligula eget, maximus nisl.
###This is an H3
Vivamus auctor condimentum porttitor. Morbi turpis ante, commodo eu aliquet at, posuere non enim. Pellentesque aliquet ornare velit, id mattis lectus finibus quis. Proin non dapibus felis. Etiam id lacus hendrerit, posuere ligula eget, maximus nisl.
####This is and H4