Skip to content

Instantly share code, notes, and snippets.

@craigmdennis
craigmdennis / keyboard.css
Created June 14, 2011 16:28
Keyboard Navigation Widget
#key-tip {
background:#EEE;
position:absolute;
top:50%;
left:50%;
margin-top:-62.5px;
margin-left:-119px;
height:125px;
width:138px;
}
@craigmdennis
craigmdennis / descendentOf.js
Created July 1, 2011 13:58
Descendent of - jQuery Plugin
(function($) {
$.fn.descendantOf = function(parentId) {
return this.closest(parentId).length != 0;
}
})(jQuery)
/*
* USAGE
* $('#element').descendantOf('#another-element'); if #element is within #another-element then this will return true
*
@craigmdennis
craigmdennis / horizontal.masonry.js
Created July 5, 2011 10:48
Simple Horizontal Masonry
/**
* Builds a horizontal masonry in whats possbily (i haven't researched
* the techniques) a crude manner.
* Fits elements into columns if there is room and sets the width of
* the container element to contain all the columns.
*
* Known Issues:
* - Does not do anything clever for elements where height exceeds
* window height (probably just gets chopped off if overflow: hidden)
* - All elements are expected to be the column width
@craigmdennis
craigmdennis / gist:1064807
Created July 5, 2011 13:07
Detect if an anchor is linking to the current page
(function( $ ){
$.fn.isCurrentPage = function() {
if (window.location.href.indexOf(this.attr('href')) >= 0) {
return true;
}
else {
@craigmdennis
craigmdennis / gist:1113572
Created July 29, 2011 10:17
Simple random array object
var myArray = ["Good!", "Great!", "Awesome!", "Super!", "Nice!"];
function random(array) {
return array[Math.floor(Math.random() * array.length)];
}
random(myArray);
@craigmdennis
craigmdennis / verticalCenter.js
Created September 8, 2011 18:47
Vertical Center jQuery Plugin
(function ($) {
$.fn.verticalCenter = function () {
var obj = this;
obj.parent().css({
'position': 'relative'
});
obj.css({
'margin-top': -this.outerHeight() / 2,
'margin-left': -this.outerWidth() / 2,
'position': 'absolute',
@craigmdennis
craigmdennis / currentScrollPos.js
Created September 8, 2011 23:33
Get the current position of an element relative to the top of the viewport
(function ($) {
$.fn.currentScrollPos = function () {
var obj = this.offset().top;
obj = obj - window.pageYOffset;
return obj;
}
})(jQuery);
$(window).scroll(function () {
console.log($('#your-element').currentScrollPos());
@craigmdennis
craigmdennis / labelSlider.js
Created September 15, 2011 22:42
Slide labels away from input on focus
// Requires jQuery easing
function moveLabels() {
// Get the original position values
var pos = $('#contact label').css('left').replace('px', '');
$('input[type=text],textarea').focusin(function () {
var label = $(this).prev('label');
label.stop(true, false).animate({
'left': -label.width() - pos
@craigmdennis
craigmdennis / background_image.php
Created September 16, 2011 13:20
Create a tileable background image from the left-most pixel of an image
<?php
function create_background($filename) {
// Get the file extension
$file_pieces = explode('.', $filename);
$file_name = $file_pieces[0];
$file_ext = $file_pieces[1];
$new_filename = $file_name.'_bg.'.$file_ext;
@craigmdennis
craigmdennis / removeWildcardClass.js
Created October 14, 2011 09:43
Remove a class on a wildcard basis
function removeWildcardClass(string, element) {
$(element).each(function () {
var aryClasses = $(this).attr('class').split(' ');
for (var i = 0; i < aryClasses.length; i++) {
if (aryClasses[i].indexOf(string) != -1) {
$(this).removeClass(aryClasses[i]);
}
}
});