Skip to content

Instantly share code, notes, and snippets.

@Rycochet
Rycochet / jQuery.pushState.js
Created February 23, 2015 13:39
Use window.history easily via jQuery
/**
* Levarage the wnidow.history object for AJAX website navigation
* @param {(Function|String|Number|null)} arg Use a function(url,isPop) to set the callback, a string for a url, and a number for a relative history url, finally "null" returns the current url
*/
$.pushState = function(arg) {
var state = $.pushState;
if (state.index === undefined) {
var url = window.location.href;
@Rycochet
Rycochet / jquery.lazyload.js
Last active August 29, 2015 14:15
Sinple jQuery lazy-loading
/**
* Shortcut to getBoundingClientRect
* @param {Element} element
*/
function getRect(element) {
if (element === window) {
return {
top: 0,
right: element.innerWidth,
bottom: element.innerHeight,
@Rycochet
Rycochet / convertCSS
Created September 16, 2014 09:37
Convert CSS values to a usable number, parses calc() for a single depth only
/**
* Convert a css number type string into pixel units
* @param {jQuerySelector} $el
* @param {String} value
* @param {?Boolean} horiz
* @returns {Number}
*/
function convertCSS($el, value, horiz) {
var val = parseFloat(value);
// console.log("convertCSS", value, value.replace(/[^a-z%]/g, ""), horiz)
@Rycochet
Rycochet / Popover
Created June 19, 2014 09:33
Bootstrap popover - directly under the line clicked on for multi-line texts
/*Popover functionality*/
$(window).on('resize', function(event) {
$('.show-text').not(this).popover('hide');
});
$(window).on('click', function(event) {
if (!$(event.target).hasClass("show-text")) {
$('.show-text').popover('hide');
}
});
$('.show-text').popover({
@Rycochet
Rycochet / $.fn.drawLine.js
Last active August 29, 2015 13:58
Draw a line from one element to another, use $(sel).drawLine("update") to refresh positions etc
/**
* Draw a line between two elements
* @param {(jQuerySelector|string)} target
* @param {?Object} options
* @returns {jQuerySelector}
*/
$fn.drawLine = function(target, options) {
var opts,
_uiLine = "ui-line",
get_relative = function(from_pos, from_size, to_pos, to_size, small, big, corners) {
@Rycochet
Rycochet / $.fn.imageLoad
Created February 17, 2014 09:32
Perform a callback when an image has loaded, optionally on every load
/**
* Perform a callback when an image has loaded, optionally on every load
* @param {function} callback
* @param {?boolean} repeat
* @returns {jQuerySelector}
*/
$.fn.imageLoad = function(callback, repeat) {
return this.each(function() {
var $el = $(this), fn = function() {
callback.call(this, $el);
@Rycochet
Rycochet / Array.prototype.grow
Created February 13, 2014 16:11
Grow (or shrink) an array - alters the array
/**
* Grow (or shrink) an array - alters the array
* @param {number} length
* @param {?*...} add these entries and repeat
* @returns this
*/
Array.prototype.grow = function(length, add) {
var i = 0, len = this.length, args = Array.prototype.slice.call(arguments, 1);
if (this.length < length) {
args = args.length ? args : [0];
@Rycochet
Rycochet / Math.range
Created January 24, 2014 13:39
Make sure a number is within a minimum and maximum
/**
* Make sure a number is within a minimum and maximum
* @param {number} min
* @param {number} num
* @param {number} max
* @returns {number}
*/
Math.range = function(min, num, max) {
return Math.max(min, Math.min(num, max));
};
@Rycochet
Rycochet / String.prototype.regex
Last active January 4, 2016 08:48
Split a string into an array of regex matches, or a single result if only a single match
/**
* Split a string into an array of regex matches, or a single result if only a single match
* @param {RegExp} r The pattern to check against
* @return {Array|string|number} The matches
*/
String.prototype.regex = function(r) {
var a = this.match(r), i, rx;
if (a) {
if (r.global) {
if (/(^|[^\\]|[^\\](\\\\)*)\([^?]/.test(r.source)) { // Try to match "(blah" but not "\(blah" or "(?:blah" - ignore invalid regexp
@Rycochet
Rycochet / rgba()
Last active January 24, 2018 14:32
Javascript hex to rgb()/rgba()
// Please note this uses my Math.range and String.regex methods
/**
* Convert a hex colour to a rgb/rgba value
* @param {string} hex
* @param {?number} opacity
* @returns {string}
*/
function rgba(hex, opacity) {
var colours = hex.regex(/#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/i).map(function(val) {
return Math.range(0, parseInt(val, 16), 255);