Skip to content

Instantly share code, notes, and snippets.

View taimursaeed's full-sized avatar

Taimur Saeed taimursaeed

View GitHub Profile
document.documentElement //<HTML> tag
document.body //<Body> tag
document.documentElement.scrollHeight //Total page height
document.documentElement.clientHeight || window.innerHeight //Viewport height
window.scrollY || document.documentElement.scrollTop //How much the document is scrolled
/*
Thanks to: https://gist.github.com/davidtheclark/5515733#gistcomment-2113205
*/
function isAnyPartOfElementInViewport(el) {
const rect = el.getBoundingClientRect();
// DOMRect { x: 8, y: 8, width: 100, height: 100, top: 8, right: 108, bottom: 108, left: 8 }
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
@taimursaeed
taimursaeed / package.json
Created April 7, 2018 04:42
Boilerplate Package.json
{
"name": "gulp-boilerplate",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Taimur",
"license": "ISC",
@taimursaeed
taimursaeed / gulpfile.js
Last active April 7, 2018 04:42
Boilerplate gulpfile
var gulp = require("gulp");
var clean = require('gulp-clean');
var concat = require("gulp-concat");
var sourcemaps = require('gulp-sourcemaps');
var minifyHtml = require("gulp-minify-html");
var critical = require('critical').stream;
var cleanCSS = require('gulp-clean-css');
@taimursaeed
taimursaeed / readJSON.js
Created February 9, 2018 10:12
Read JSON key value
$.each(myJSON, function(key, value) {
//read key and value here
});
@taimursaeed
taimursaeed / readQueryString.js
Created February 9, 2018 10:10
Reads Query String
function getUrlVars() {
var vars = [],
hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
@taimursaeed
taimursaeed / capitalizeFirstLetter.js
Created February 9, 2018 10:03
Capitalize First Letter JS
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// use it like capitalizeFirstLetter('hello')
// returns Hello
@taimursaeed
taimursaeed / localStorage.js
Created February 9, 2018 10:01
Storing and Retrieving JSON from Local Storage
function storeItem(itemName, data) {
localStorage.setItem(itemName, btoa(JSON.stringify(data)));
}
function retrieveItem(itemName) {
var x = localStorage.getItem(itemName);
if (!x) { console.log("Element not found in local storage"); return false; }
localStorage.removeItem('x');
x = atob(x);
return $.parseJSON(x);