Skip to content

Instantly share code, notes, and snippets.

@xat
xat / gist:8882979
Last active August 29, 2015 13:56
scrollTo
var scrollTo = function($target) {
var $body = $('body,html');
var currentOffset = $(window).scrollTop();
var targetOffset = $target.offset().top;
var distance = Math.abs(currentOffset - targetOffset);
if (!$target.length) {
return;
}
@xat
xat / gist:8898538
Created February 9, 2014 12:41
automatic answerscript for speedsums.com: Just open the page and execute this script in your JS console.
(function($) {
var $question = $('#question');
var $answer = $('#answer');
var checkInterval = 250;
var cheat = function() {
var rawVal = $question.html();
rawVal = rawVal.replace(' =', '')
.replace('x', '*')
.replace('÷', '/');
@xat
xat / gist:9978542
Last active August 29, 2015 13:58
An untested node-port of https://github.com/foertel/pihp
var Gpio = require('onoff').Gpio,
async = require('async'),
_ = require('underscore');
var measureDistance = function(opts) {
var options = _.defaults(opts, {
trigger: 24,
echo: 25,
leds: [4, 17, 18, 27, 22, 23],
timeout: 2000
var engine = SearchEngine({
url: 'http://localhost:3333/'
});
engine.use(searchbox({
el: '#box',
autocomplete: true
}));
engine.use(results({
var MyCollection = Backbone.Collection.extend({
reset: function(models, options) {
return Backbone.Collection.reset.call(this, models.results, options);
}
});
<?php
function mailer($smtpHost) {
return function($from, $to, $subject, $content) use ($smtpHost) {
mail(...);
};
}
$myMailer = mailer('smtp.google.com');
@xat
xat / jquery-fork.js
Last active August 29, 2015 14:03
jQuery fork
$('li', 'ul')
.fork(
$.branch('.active').show(),
$.branch(':not(.active)').hide()
);
var speak = function(text, cb) {
var u = new SpeechSynthesisUtterance();
u.text = text;
u.lang = 'de-DE';
u.rate = 1;
if (cb) u.onend = cb;
speechSynthesis.speak(u);
};
@xat
xat / gist:76ab2e78806c33f1d2e9
Last active August 29, 2015 14:04
js lookup
var data {
id_1: true,
id_2: true,
id_3: true
};
var lookup = function(dict) {
return function(key) {
return dict[key];
};
@xat
xat / gist:4f05f3e0d1f54c2daeda
Created September 12, 2014 13:21
jquery sort elements
$.fn.sorty = function(fn) {
return this.each(function() {
var $el = $(this);
$el.html($el.children().sort(fn));
});
};