Skip to content

Instantly share code, notes, and snippets.

Avatar
😶
boop

andreas andreasvirkus

😶
boop
View GitHub Profile
View rand.js
function rand(min, max, integer) {
var r = Math.random() * (max - min) + min;
return integer ? r|0 : r;
}
// Usage
console.log(rand(2,5)); // float random between 2 and 5 inclusive
console.log(rand(1,100,true)); // integer random between 1 and 100
View strip.js
function stripTags(htmlstr) {
var div = document.createElement('div');
div.innerHTML = htmlstr;
return div.textContent;
}
View once.js
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
View progjpeg.sh
convert -strip -quality 80% original.jpg step1.jpg
jpegtran -optimize -progressive -copy none -outfile output.jpg step1.jpg
View appear.js
// Meant to find out if a DOM element is scrolled into view
// aka if it is visible on the screen.
function appear(elem) {
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
View defer.js
// add this as the only script before your closing body tag
// and let it load your external scripts once your DOM has finished loading.
function addJSOnload() {
var element = document.createElement("script");
element.src = "defer.js"; // replace your script with "defer.js"
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", addJSOnload, false);
else if (window.attachEvent)
View clearfix.css
/* A simple Bootstrap clearfix mod */
/* Tablet */
@media (min-width:767px){
/* Column clear fix */
.col-lg-1:nth-child(12n+1),
.col-lg-2:nth-child(6n+1),
.col-lg-3:nth-child(4n+1),
.col-lg-4:nth-child(3n+1),
View debounce.js
export default function debounce(fn, wait = 0) {
let called = false;
return function debounceReturn(...args) {
if (!called) {
const context = this;
called = true;
setTimeout(() => {
View carousel.css
View getDistanceFromTop.js
//For the times that I need to know how far an element is from the top of the DOM,
//not the top of it’s parent, I use this helper function.
//Loops through all parent nodes of an element to get it's distance from the top of the document
function getDistanceFromTop(element) {
var yPos = 0;
while(element) {
yPos += (element.offsetTop);
element = element.offsetParent;
You can’t perform that action at this time.