This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* window.requestAnimationFrame polyfill | |
*/ | |
(function() { | |
var lastTime = 0; | |
var vendors = ['ms', 'moz', 'webkit', 'o']; | |
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { | |
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; | |
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] | |
|| window[vendors[x]+'CancelRequestAnimationFrame']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $.onScreen jQuery plugin | |
* @uses $("#eample").onScreen() // return true if element is in current view port | |
*/ | |
$.fn.onScreen = function(x, y){ | |
if(x == null || typeof x == 'undefined') x = 1; | |
if(y == null || typeof y == 'undefined') y = 1; | |
var win = $(window); | |
var viewport = { | |
top : win.scrollTop(), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $.swap - jQuery plugin to swap two elements | |
* Swap the current element with other element | |
* @uses $("#eample").swap($("#element")) | |
*/ | |
$.fn.swap = function(to) { | |
return this.each(function(){ | |
var ct = $(to).clone(true); | |
var cf = $(this).clone(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* replaceAll - JavaScript string function | |
* @uses replace all matching substring to its corresponding replace string | |
* Worked similarly as str_replace in PHP | |
*/ | |
String.prototype.replaceAll = function(find, replace) { | |
var replaceString = this; | |
var regex; | |
for (var i = 0; i < find.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* serializeObject - jQuery plugin to serialize form elements as an object | |
* @uses $("#form").serializeObject(); // return {input:"value", input2:"value" .. } | |
*/ | |
$.fn.serializeObject = function(){ | |
var o = {}; | |
var a = this.serializeArray(); | |
$.each(a, function() { | |
if (o[this.name] !== undefined) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* isPalindrome - function to check if a number or string is palindrome | |
* @uses isPalindrome(141) // true | |
*/ | |
function isPalindrome(str){ | |
return (str + "").split("").reverse().join("") == (str + ""); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* remove all spaces (" ") from string | |
* @uses removeSpaces("sample string with s p a c e s") // samplestringwithspaces | |
*/ | |
function removeSpaces(str){ | |
return (str + "").replace(/ /g,''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* unique - Array function for unique elements | |
* @use [2, 2, 3, 4, 3, 2].unqiue() // [2, 3, 4] | |
*/ | |
Array.prototype.unique = function() { | |
var unique = []; | |
for (var i = 0; i < this.length; i++) { | |
if (unique.indexOf(this[i]) == -1) { | |
unique.push(this[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* size - Object size in javascript | |
* @uses {a:1, b:2}.size() // 2 | |
*/ | |
Object.prototype.size = function(obj) { | |
var size = 0, key; | |
for (key in obj) { | |
if (obj.hasOwnProperty(key)) size++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* flat / merge all sub array into one array | |
* @uses [['1', '2'], ['3', '4], '5', '6'].flat(); // ['1', '2', '3', '4', '5', '6'] | |
*/ | |
Array.prototype.flat = function(){ | |
return [].concat.apply([], this); | |
} |
OlderNewer