Skip to content

Instantly share code, notes, and snippets.

View Alex1990's full-sized avatar
🐢
Get Things Done!

Alex Chao Alex1990

🐢
Get Things Done!
View GitHub Profile
/**
* Check if an event is supported.
* Ref: http://perfectionkills.com/detecting-event-support-without-browser-sniffing/
*/
function isEventSupported(event) {
var testEl = document.createElement('div');
var isSupported;
event = 'on' + event;
isSupported = (event in testEl);
// Get the closest matched element of an element. (Include itself)
function closest(elem, className, context) {
context = context || document;
do {
if (hasClass(elem, className)) return elem;
} while ((elem = elem.parentNode) && elem !== context)
return null;
}
@Alex1990
Alex1990 / viewport.js
Last active August 29, 2015 14:17
Get window(aka viewport) width and height.
/**
* Get window/viewport width and height. The window.innerWidth value includes the scrollbar,
* and document.documentElement.clientWidth/document.body.clientWidth doesn't include.
* Compatibility: IE6+ and other modern browsers
*/
function winW() {
return window.innerWidth || document.documentElement.clientWidth;
}
function winH() {
@Alex1990
Alex1990 / delegate.js
Last active August 29, 2015 14:17
A simple event delegation.
/**
* A simple event delegation method.
*/
// closest method: https://gist.github.com/Alex1990/5547956babc593852c3a
function delegate(elem, type, selector, listener, capture) {
listener._delegateWrapper = function(e) {
if (e.delegateTarget = closest(e.target, selector, elem)) {
listener.call(e.delegateTarget, e);
@Alex1990
Alex1990 / hasClass.js
Created March 9, 2015 09:24
Check if an element has the specified class.
// Check if an element has the specified class.
function hasClass(el, cls) {
return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
isIE = /MSIE|Trident/.test(navigator.userAgent);
// isIE = 'ActiveXObject' in window;
/**
* IE 11 has discarded the "MSIE" feature string.
*/
isIE11 = /Trident.*rv:11\.0/.test(navigator.userAgent);
@Alex1990
Alex1990 / rNumber.js
Last active June 1, 2016 18:59
Valid number primitive regular expression.
/**
* Valid number primitive regular expression. The keyword "Infinity" is invalid.
*/
var rNumber = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/;
@Alex1990
Alex1990 / rDecimal.js
Last active August 29, 2015 14:14
Decimal number regular expression.
/**
* Decimal number regular expression.
*
* Valid decimal strings:
* ['1', '1.', '1.1', '.1', '+1', '+1.', '+1.1', '+.1', '-1', '-1.', '-1.1', '-.1']
* Invalid decimal strings:
* ['', '.', '+.', '-.', '1.1.', '1..1', 'Infinity', '-Infinity']
*
* Ref:
* http://stackoverflow.com/questions/12117024/decimal-number-regular-expression
@Alex1990
Alex1990 / benchmark.js
Created February 5, 2015 04:39
A simple function to test performance.
/**
* A simple function to test performance.
*/
function benchmark() {
var tests = Array.prototype.slice.call(arguments);
var length = tests.length;
var startTimes = Array(length);
var result = Array(length);
var times = typeof benchmark.times === 'number' ? benchmark.times/2 : 5000;