Skip to content

Instantly share code, notes, and snippets.

View gucheen's full-sized avatar

Cheng Gu gucheen

View GitHub Profile
@gucheen
gucheen / sticky-menu.js
Created August 19, 2014 02:31
Creating a sticky menu
var menu = document.querySelector('.menu');
var menuPosition = menu.getBoundingClientRect();
var placeholder = document.createElement('div');
placeholder.style.width = menuPosition.width + 'px';
placeholder.style.height = menuPosition.height + 'px';
var isAdded = false;
window.addEventListener('scroll', function() {
if (window.pageYOffset >= menuPosition.top && !isAdded) {
menu.classList.add('sticky');
development:
closure-library/closure/bin/build/closurebuilder.py --root=closure-library/ --root=js/ --namespace="YourRootNameSpace" --output_mode=compiled --compiler_jar=compiler.jar > compiled.js
production:
closure-library/closure/bin/build/closurebuilder.py --root=closure-library/ --root=js/ --namespace="YourRootNameSpace" --output_mode=compiled --compiler_jar=compiler.jar --compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" > compiled.js
all:
development
@gucheen
gucheen / scrollToBottom.js
Created January 3, 2015 02:33
detect page scroll to bottom
window.innerHeight+(window.scrollY || document.documentElement.scrollTop) > document.documentElement.scrollHeight
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style id="jsbin-css">
body {
height: 3000px;
}
.scroll {
@gucheen
gucheen / mouse-idle.js
Created March 9, 2015 11:12
detect mouse idle
var timeoutID;
function setup() {
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("mousewheel", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);
@gucheen
gucheen / debounce.js
Created June 4, 2015 02:31
JavaScript 防抖
function debounce(func, wait, options) {
var args,
maxTimeoutId,
result,
stamp,
thisArg,
timeoutId,
trailingCall,
lastCalled = 0,
maxWait = false,
.ui-select-bootstrap.ng-invalid span.btn.ui-select-toggle {
border-color: #D44950 !important;
}
@gucheen
gucheen / Animated Weather Icons.markdown
Created June 17, 2015 03:37
Animated Weather Icons
@gucheen
gucheen / isDescendant.js
Created June 17, 2015 09:40
isDescendant.js
function isDescendant(parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
}
@gucheen
gucheen / parseLodash.js
Created August 25, 2015 05:51
Safe JSON.parse with lodash.js
function parseLodash(str){
return _.attempt(JSON.parse.bind(null, str));
}