Skip to content

Instantly share code, notes, and snippets.

View willbroderick's full-sized avatar

Will Broderick willbroderick

  • Clean Canvas Ltd
  • Nottingham, UK
View GitHub Profile
@willbroderick
willbroderick / minicomp.js
Created March 31, 2014 12:33
For compressing strings, just a little bit, when you already know good keywords to use for the compression. I use it to trim cookies.
var compList = ['products', 'collections', '_thumb.jpg', '.myshopify.com/', '//cdn.shopify.com/s/files/', 'http://', 'shopify.com'];
function miniComp(str) {
for(var i=0; i<compList.length; i++) {
str = str.split(compList[i]).join('#['+i+']'); //Split/join vs regex speed = surprising
}
return str;
}
function miniDecomp(str) {
@willbroderick
willbroderick / jsonIsEqual
Created April 16, 2014 10:43
Check if two 1-level-deep json arrays contain the same values
// A shallow scan of a JSON object to test for equality
function getNumKeysInJsonObject(obj) {
var i = 0;
for (var x in obj)
if (obj.hasOwnProperty(x)) i++;
return i;
}
function jsonIsEqual(obj1, obj2) {
var isEqual = true; //Equal until proven otherwise
if(typeof obj1 != 'undefined' && typeof obj2 != 'undefined') {
@willbroderick
willbroderick / jquery.staggeredEvent.js
Created June 17, 2014 10:19
Stagger events across a collection of elements
/*
Stagger an event across a collection of elements. Useful for making the pretties.
Example - for loading in ajax results with a class-based css3 transition, where results are initially hidden with hide():
$('#ajax-results').on('loaded', function(){
$(this).children('.result').addClass('pre-transition').slideDown(250).staggerEvent(function(el){
$(el).removeClass('pre-transition');
}, 150, 250);
});
*/
@willbroderick
willbroderick / gist:dbb4064ba345790b39f9
Last active August 29, 2015 14:19 — forked from carolineschnapp/currencies.liquid
Keeps the 'You have chosen GBP, but the shop is AUS' message hidden, if you're using the native currency
{% if settings.show_multiple_currencies %}
{{ "//cdn.shopify.com/s/javascripts/currencies.js" | script_tag }}
{{ "jquery.currencies.min.js" | asset_url | script_tag }}
<script>
{% if settings.currency_format %}
Currency.format = '{{ settings.currency_format }}';
{% endif %}
@willbroderick
willbroderick / normheights.js
Last active November 17, 2015 09:29
Help normalise the heights of items laid out in responsive rows - relies on presence of an inner div that will keep natural height of objects inside. Useful for keeping prices aligned under product images.
$(window).on('debouncedresize load', function(){
$('[data-normheights]').each(function(){
var $items = $(this).find($(this).data('normheights')),
childFilter = $(this).data('normheights-inner'),
tallest = 0,
lastYOffset = 0,
row = [];
$items.each(function(index){
var $img = $(this).find(childFilter);
var yOffset = $(this).offset().top;
@willbroderick
willbroderick / scrollinternallinks.js
Last active December 14, 2015 16:58
Javascript to scroll to in-page # hrefs, and permit external links
@willbroderick
willbroderick / jquery.cropImageToRatio.js
Last active December 21, 2015 16:37
Wraps an image with a fixed-ratio div and scales/translates the image to fill. Uses % CSS so no resize events needed.
/// Crop and scale images to a specific ratio
$.fn.cropImageToRatio = function(params){
//params
var params = $.extend({
ratio: 4/3,
wrapper: '<div/>'
}, params);
//loop all images
return $(this).each(function(){
//only allow init once
@willbroderick
willbroderick / remaining_chars.html
Last active December 28, 2015 20:49
Show the number of remaining characters beside any text input (requires jQuery)
<script>
$(function(){
$('textarea[maxlength], input[maxlength]').bind('keyup change paste', function(){
var maxlength = parseInt($(this).attr('maxlength'));
var val = $(this).val();
var currLen = val.length;
var newLines = val.match(/(\r\n|\n|\r)/g);
if(newLines != null) {
currLen += newLines.length;
}
@willbroderick
willbroderick / scale_text_with_container.html
Last active December 28, 2015 20:49
Scale size of text up/down with % to sit nicely inside parent container. Great for live-type over images/slides. - CURRENTLY DEPENDENT ON A BASE SIZE FROM: {{ settings.adv_page_width }} - how should this be remedied? What's the best way to get a 'default' size? Maybe just a px val in data attribute?
<script>
$(function(){
//Text that scales up/down based on container width
function resizeScalingTextFromColumn() {
$('.scaled-text').each(function(){
var $base = $(this).closest('.scaled-text-base');
var scale = $base.width() / {{ settings.adv_page_width }};
$(this).css('font-size', (scale * 100) + '%');
});
}
@willbroderick
willbroderick / gist:7806634
Last active December 30, 2015 08:59
Hide orphaned elements - the last unfull row of a grid, call on parent with child selector
//Ignore if you already have it:
$.fn.reverse = [].reverse;
//Hide orphaned thumbs, call on parent with child selector
$.fn.hideOrphans = function(selector, totalCountSelector){
var selector = selector;
$(this).each(function(){
var $children = $(this).find(selector);
$children.filter('.hidden-orphan').show().removeClass('hidden-orphan');
if($(this).hasClass('show-orphans')) {