Skip to content

Instantly share code, notes, and snippets.

View lean8086's full-sized avatar

Leandro Linares lean8086

View GitHub Profile
@lean8086
lean8086 / navigation-menu.js
Last active August 29, 2015 13:57
Improve the #navigation-menu behavior
(function (doc) {
var input = doc.getElementById('ml-header-user');
// Only works when a user menu exists (for logged users)
if (!input) return;
// Listen for every click on <html>
// The support for IE8+ was dropped because of lack of support of :checked pseudo-selector
doc.documentElement.addEventListener('click', function (ev) {
// Avoid to close if I click on the trigger. In that case, it will use CSS
if (input.checked && ev.target !== input && ev.target !== input.previousElementSibling) input.checked = false;
});
@lean8086
lean8086 / namespace.js
Created May 10, 2012 15:47
Namespace template
(function (window) {
"use strict";
/**
* Helpers
*/
var document = window.document,
WebSocket = window.WebSocket,
Worker = window.Worker,
localStorage = window.localStorage,
@lean8086
lean8086 / vendor_prefix.js
Created May 10, 2012 15:22
Grab the vendor prefix of the current browser.
// Based on: http://lea.verou.me/2009/02/find-the-vendor-prefix-of-the-current-browser/
var VENDOR_PREFIX = (function () {
var regex = /^(Webkit|Khtml|Moz|ms|O)(?=[A-Z])/,
styleDeclaration = document.getElementsByTagName("script")[0].style;
for (var prop in styleDeclaration) {
if (regex.test(prop)) {
return prop.match(regex)[0].toLowerCase();
@lean8086
lean8086 / friendly.js
Created May 11, 2012 16:07
Convert a normal string into a friendly string
function friendly(str) {
"use strict";
// Turn string into lower cases
str = str.toLowerCase();
// Normalize vocals
str = str.replace(/\á|\à|\ä|\â|\ã|\ª/g, "a");
str = str.replace(/\é|\è|\ë|\ê/g, "e");
str = str.replace(/\í|\ì|\ï|\î/g, "i");
@lean8086
lean8086 / Database.php
Created May 15, 2012 17:29
Allows to connect to a database from PHP
<?php
class Database {
private $HOST = "...Your hostname or IP here...";
private $USER = "...Username to log into database...";
private $PASS = "...Password to log into database...";
private $DDBB = "...Name of database...";
private function connect() {
$c = mysql_connect($this->HOST, $this->USER, $this->PASS);
@lean8086
lean8086 / canvasContext.js
Created May 22, 2012 19:27
Set the 2D context of a canvas
var context = (function () {
"use strict";
// Get the HTML Canvas element
var canvas = document.querySelector("canvas");
// Set canvas size
canvas.width = 200;
canvas.height = 200;
@lean8086
lean8086 / bind.js
Created May 23, 2012 13:47
Binds multiple events into one listener
function bind(listener, events, callback, useCapture) {
events.split(" ").forEach(function (ev) {
listener.addEventListener(ev, callback, useCapture);
});
}
// Example:
// bind(document, "mousedown mouseup keydown keyup", proccess);
@lean8086
lean8086 / rAF.js
Created May 25, 2012 19:56
Request Animation Frame polyfill
var fps = 1000 / 60,
rAF = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, fps);
};
@lean8086
lean8086 / sniffing.jade
Created June 4, 2012 18:30
Browser sniffing for Jade
!!! 5
//if lt IE 7
html.no-js.lt-ie10.lt-ie9.lt-ie8.lt-ie7.ie6(lang="en")
//if IE 7
html.no-js.lt-ie10.lt-ie9.lt-ie8.ie7(lang="en")
//if IE 8
html.no-js.lt-ie10.lt-ie9.ie8(lang="en")
//if IE 9
html.no-js.lt-ie10.ie9(lang="en")
//[if gt IE 9] <!
@lean8086
lean8086 / customEvent.js
Created July 20, 2012 00:23
Creating, triggering and listening a custom event
// Defining the custom event
var customEvent = (function () {
"use strict";
// Create a generic event
var e = document.createEvent("Event");
// Define the custom event name
e.initEvent("custom", true, true);
// Grab event to trigger
return e;
}());