Skip to content

Instantly share code, notes, and snippets.

var $win = $(window)
var $progress = $('.progress-indicator')
var wh = $win.height()
var h = $('body').height()
var scrollHeight = h - wh
$win.on('scroll', function () {
var perc = Math.max(0, Math.min(1, $win.scrollTop() / scrollHeight))
$progress.css({
@gaowhen
gaowhen / isInViewport
Created June 15, 2015 08:55
is element in viewport
function isElementInViewport (el) {
//special bonus for those using jQuery
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0]
}
var rect = el.getBoundingClientRect()
return (
@gaowhen
gaowhen / random_int_between
Created April 7, 2015 10:16
random_int_between
function random_int_between(min, max) {
return Math.floor(Math.random() * (max -min + 1) + min);
}
@gaowhen
gaowhen / url_parse
Created April 1, 2015 10:08
parse url
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
@gaowhen
gaowhen / safe_string
Created April 1, 2015 10:07
safe string
function safeString(string) {
if(!string) {
return "";
}
var escape = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
function prefix_integer(num, length) {
var len = length || 2;
return (num / Math.pow(10, len)).toFixed(len).substr(2);
}