Skip to content

Instantly share code, notes, and snippets.

@lutzissler
lutzissler / jquery.partition.js
Created October 28, 2014 16:23
Little jQuery plugin for spreading, or “partitioning“, elements across several parent elements. Called to create `n` partitions, the plugin wraps the selected elements at positions `c`, `n+c`, `2n+c`, `3n+c`, … into “wrapper” element for all `c` within `1`…`n`.
(function ($) {
$.fn.partition = function(options) {
options = $.extend({
count: false,
wrapper : '<div>'
}, options || {});
var elems = $(this),
len = pos = 0;
for (var i = 0; i < options.count; i++) {
var sel = elems.filter(":nth-child(" + (options.count - i) + "n)");
@lutzissler
lutzissler / func.getMediaInfo.php
Last active August 29, 2015 14:08
Get media info object based on media URL. Currently support YouTube, Vimeo and Soundcloud.
function getMediaInfo($mediaUrl) {
$mediaUrl = trim($mediaUrl);
// Check for YouTube
$matchFound = preg_match('|^\s*https?://www.youtube.com/watch\?v=([a-zA-Z0-9\.-]+)(#.*)?\s*$|', $mediaUrl, $matches);
if ($matchFound) {
return (object)array(
'mediaUrl' => $mediaUrl,
'mediaSource' => 'youtube',
'mediaID' => $matches[1],
@lutzissler
lutzissler / func.first.php
Created November 28, 2014 12:16
Get the first element of an array without throwing a warning for non-variable arrays, like reset() does. In PHP 5.4, this could be reduced to a one-liner by removing $first.
function first($arr) {
$first = array_slice($arr, 0, 1);
return $first[0];
}
@lutzissler
lutzissler / func.extend.php
Created December 18, 2014 09:41
Extend $object1 with the properties of $object2 and return the resulting object.
function extend($object1, $object2) {
return (object)array_merge((array)$object1, (array)$object2);
}
@lutzissler
lutzissler / func.flushDOM.js
Created August 4, 2015 11:23
Workaround for forcing the browser to write any cached changes to the DOM.
function flushDOM() {
$('body').height();
}
@lutzissler
lutzissler / jquery.hiddenscroll.js
Created September 6, 2014 06:33
Feature test for hidden scrollbars (e. g. on Mac OS). Adopted from http://davidwalsh.name/detect-scrollbar-width and https://github.com/Modernizr/Modernizr/blob/d6e1d6f09e1f8250eca9116bcffe2f846616afa7/feature-detects/hiddenscroll.js. Usage: Plug it in and see the class "hiddenscroll" being added to the html element if scrollbars are hidden.
(function ($) {
$(function () {
var el = $('<div style="width:100px;height:100px;overflow:scroll;position:absolute;top:-9999px;"/>'),
elDom = el.get(0);
el.appendTo("body");
if (elDom.offsetWidth === elDom.clientWidth) {
$("html").addClass("hiddenscroll");
}
el.remove();
});
@lutzissler
lutzissler / countries.inc.php
Last active September 21, 2015 08:48
Alphabetical list of all countries, in English and German.
<?php
$countries = array(
'en' => array(
'Afghanistan',
'Albania',
'Algeria',
'American Samoa',
'Andorra',
'Angola',
@lutzissler
lutzissler / function.tidy_office_html.php
Created July 31, 2012 07:32
Tidy HTML inserted by copy/pasting from Microsoft office (PHP)
// Regexps courtesy of 1st class media
// http://www.1stclassmedia.co.uk/developers/clean-ms-word-formatting.php
function tidy_office_html($str) {
$replacements = array(
'/<!--.*?-->/s' => '',
'/<o:p>\s*<\/o:p>/s' => '',
'/<o:p>.*?<\/o:p>/s' => "&nbsp;",
'/\s*mso-[^:]+:[^;"]+;?/i' => '',
'/\s*MARGIN: 0cm 0cm 0pt\s*;/i' => '',
'/\s*MARGIN: 0cm 0cm 0pt\s*"/i' => '',
@lutzissler
lutzissler / jquery.wrapgroups.js
Last active October 12, 2015 18:58
Wrap groups of matched elements (jQuery)
(function ($) {
// From http://forloop.co.uk/blog/wrap-child-elements-in-groups-in-jquery
// Modified to work much simpler
$.fn.wrapGroups = function(options) {
options = $.extend({
groupSize : false,
groupCount: false,
wrapper : '<div>'
}, options || {});
var elems = $(this),
@lutzissler
lutzissler / jquery.flash.js
Created June 3, 2013 09:58
Flash/blink elements by fading in/out. Use it with care!
$.fn.flash = function (options) {
options = $.extend({
animation: 200,
off: 0,
on: 1000
}, options || {});
var elems = $(this),
f = function () {
elems
.animate({opacity: 0}, options.animation)