Skip to content

Instantly share code, notes, and snippets.

View anselmh's full-sized avatar

Anselm Hannemann anselmh

View GitHub Profile
@anselmh
anselmh / dabblet.css
Created April 17, 2013 12:20
Untitled
.box {
width: 400px;
height: 300px;
background: #999;
}
.box:after {
background: orange;
border: 1px solid white;
color: white;
@anselmh
anselmh / dabblet.css
Created May 23, 2013 05:38
Pure CSS Toggle Page Layout
/**
* Pure CSS Toggle Page Layout
*/
.content > article {
min-height: 0;
height: 0;
overflow: hidden;
opacity: 0;
@anselmh
anselmh / dabblet.css
Created May 24, 2013 13:44
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
.sticker {
display: inline-block;
width: 3rem;
height: 3rem;
@anselmh
anselmh / target_blank--external.js
Created July 24, 2013 12:12
Open all external links (different hostname than current page) in new tab/window with plain vanilla JavaScript.
/**
* Open external links in new tab/window
*/
// All http:// links should open in new tab/window. Internal links are relative.
var anchors = document.querySelectorAll('a');
for (var i = 0; i < anchors.length; i++) {
if (anchors[i].host !== window.location.hostname) {
anchors[i].setAttribute('target', '_blank');
@anselmh
anselmh / resimg-syntax-test.html
Last active December 25, 2015 20:49
It's a quick demo of the three candidates of #respimg syntax. I simply want to compare verbosity of all those.
<!--
A quick demo of the three current candidates of Responsive Images syntax on a real world example for one image.
This on purpose does not include any of the proposed viewport syntaxes because this IMO adds confucion and
is stylistic only (therefore should go in CSS IMO).
-->
<!-- This is the src-{N} way: http://tabatkins.github.io/specs/respimg/Overview.html -->
/* Wildcard selector searches through any part of the string */
html[data-useragent*='Chrome/13.0'] .nav{
background:url(img/radial_grad.png) center bottom no-repeat;
}
@anselmh
anselmh / faster-for-loops.js
Created December 12, 2013 12:06
# Cached and faster `for` loops
/*
* The trouble with collections is that they are live queries against the underlying document (the HTML page). This means that every time you access any collection’s length, you’re querying the live DOM, and DOM operations are expensive in general.
*
* It's only good if you don't update the length by f.e. adding DOM nodes
*
* That’s why a better pattern for for loops is to cache the length of the array (or collection) you’re iterating over, as shown in the following example:
*/
for (var i=0, max = myarray.length; i < max; i++) {
// do sth.
var inspect_me = 0,
result = '';
switch (inspect_me) {
case 0:
result = "zero";
break;
case 1:
result = "one";
break;
default:
@anselmh
anselmh / timer.js
Created December 19, 2013 12:15
Better timers with closures and throttling through debouncing methods. By Remy Sharp: http://remysharp.com/2010/07/21/throttling-function-calls/
// Debounce without jQuery
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
(function (global) {
'use strict';
window.matchMedia = window.matchMedia || function (media) {
if (!Modernizr) {
throw new Error('Reference Error: Modernizr is undefined');
}
return {
matches: Modernizr.mq(media)