Skip to content

Instantly share code, notes, and snippets.

View daihuaye's full-sized avatar

Daihua Ye daihuaye

View GitHub Profile
@daihuaye
daihuaye / getCursorPosition.js
Last active August 29, 2015 14:07
Calculate the caret(cursor) position
function getCursorPosition (inp) {
if ("selectionStart" in inp) {
return inp.selectionStart
} else if (document.selection) {
inp.focus();
var sel = document.selection.createRange();
var selLen = document.selection.createRange().text.length;
sel.moveStart("character", -inp.value.length);
return sel.text.length - selLen;
}
@daihuaye
daihuaye / setCursorPosition.js
Last active August 29, 2015 14:07
Move cursor to position in input text box
function setCursorPosition(e, pos) {
$(e).each(function(index, elem) {
if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
var range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
@daihuaye
daihuaye / module.js
Created October 17, 2014 15:54
JS best practice - module
module = function(){
var current = null;
var labels = {
'home':'home',
'articles':'articles',
'contact':'contact'
};
var init = function(){
};
var show = function(){
@daihuaye
daihuaye / carousel.js
Created October 17, 2014 15:56
Allow for Configuration and Translation
@daihuaye
daihuaye / optimizeLoop.js
Created October 17, 2014 15:57
Loops can get terribly slow in JavaScript.
var names = ['George',
'Ringo',
'Paul',
'John'];
for(var i=0,j=names.length;i<j;i++){
doSomethingWith(names[i]);
}
@daihuaye
daihuaye / process.sh
Created October 18, 2014 03:18
Port 35729 is already in use by another process and check which program is using this port .
lsof | grep 35729
@daihuaye
daihuaye / hiddenPorperty.css
Last active August 29, 2015 14:07
Some tips in CSS
.element {
/*apply: display: inline-block;*/
float: left;
}
.element {
/*apply: width: inherit; height: inherit;*/
display: inline;
}
.element {
/*apply: height: 100px; vertical-align: middle;*/
@daihuaye
daihuaye / resource.md
Last active August 29, 2015 14:07
Good libraries to solve difficulty questions
@daihuaye
daihuaye / getScrollTop.js
Last active August 29, 2015 14:08
Get height to the document top after scroll and set set to top
var getScrollTop = function() {
if(_isMobile) {
return _mobileOffset;
} else {
return window.pageYOffset || documentElement.scrollTop || body.scrollTop || 0;
}
};
var setScrollTop = function(top, force) {
_forceRender = (force === true);
@daihuaye
daihuaye / alertSize.js
Created October 22, 2014 15:47
Get the inner browser's actual size
function alertSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;