Skip to content

Instantly share code, notes, and snippets.

View ericwwsun's full-sized avatar

Eric Sun ericwwsun

  • IBM iX
  • New York City
View GitHub Profile
@ericwwsun
ericwwsun / gist:2998980
Created June 26, 2012 21:01
Javascript: Keyboard navigation control, Vertically
// --------- keyboard navigation control --------- //
var isinmotion = false;
$j(document).keydown(function(e){
switch (e.keyCode) {
case 38:
//console.log("up");
if (isinmotion) {
return false;
} else {
var curr = $j("li.viewing:last");
@ericwwsun
ericwwsun / gist:2998979
Created June 26, 2012 21:00
Javascript: Keyboard navigation control, Vertically
// --------- keyboard navigation control --------- //
var isinmotion = false;
$j(document).keydown(function(e){
switch (e.keyCode) {
case 38:
//console.log("up");
if (isinmotion) {
return false;
} else {
var curr = $j("li.viewing:last");
@ericwwsun
ericwwsun / gist:2998950
Created June 26, 2012 20:57
Javascript: Delay fire window resize handler
window.onresize = function(e) {
delay(function(){
// do something.....
}, 250);
}
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
@ericwwsun
ericwwsun / gist:2998882
Created June 26, 2012 20:53
Javascript: Background Position control by js, IE8 included
var backgroundPos,
xPos,
yPos;
if( $j("html").hasClass("ie8")) { // html class... you know... it's useful
xPos = $j(this).css("background-position-x");
yPos = Number($j(this).css("background-position-y").replace(/[^0-9-]/g, ''));
} else {
backgroundPos = $j(this).css("backgroundPosition").split(" ");
xPos = backgroundPos[0],
@ericwwsun
ericwwsun / gist:2998847
Created June 26, 2012 20:45
Javascript: if is Mobile detection
isMobile: function() {
// detect user agent
var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
var result = false;
if (mobile) {
result = true;
var userAgent = navigator.userAgent.toLowerCase();
if ((userAgent.search("android") > -1) && (userAgent.search("mobile") > -1))
result = true; // android mobile
@ericwwsun
ericwwsun / gist:2998840
Created June 26, 2012 20:43
Javascript: Cookies
setCookie: function(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
},
@ericwwsun
ericwwsun / gist:2998826
Created June 26, 2012 20:42
Javascript: iPad Orientation and scale, ref: http://adactio.com/journal/4470/
// iPad Orientation and scale , ref: http://adactio.com/journal/4470/
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelector('meta[name="viewport"]');
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
document.body.addEventListener('gesturestart', function () {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}
@ericwwsun
ericwwsun / namespace.html
Created June 21, 2012 03:06
Javascript: Namespace Revealing Module Pattern
Namespace
@ericwwsun
ericwwsun / gist:2950681
Created June 18, 2012 21:03
Javascript: Key/Value Array Example
var dropdowns = {
className1: 'classname1',
className2: 'classname2',
className3: 'classname3'
};
for(index in dropdowns) {
if( dropdowns[index] != currentClass) {
$j('.' + dropdowns[index]).dosomething();
}
@ericwwsun
ericwwsun / gist:2891905
Created June 7, 2012 22:05
Javascript: Placeholder in IE
// Form - Placeholder
if(!Modernizr.input.placeholder) {
//alert("no placeholder!!!!!");
$j("*:text:not([type='password'])").each(function(){
//$j(this).css("border","solid 1px red");
if( $j(this).attr("placeholder") && $j(this).attr("placeholder").length > 1 ) {
var placeholderTxt = $j(this).attr("placeholder");
$j(this).val(placeholderTxt);