Skip to content

Instantly share code, notes, and snippets.

(async () => {
let nodes = document.getElementsByTagName('img');
for (let node of nodes) {
await new Promise(res => {
node.src = node.dataset.src;
node.onload = () => res();
})
}
})()
// When the modal is shown, we want a fixed body
document.body.style.position = 'fixed';
document.body.style.top = `-${window.scrollY}px`;
// When the modal is hidden, we want to remain at the top of the scroll position
const scrollY = document.body.style.top;
document.body.style.position = '';
document.body.style.top = '';
window.scrollTo(0, parseInt(scrollY || '0') * -1);
@mohnatus
mohnatus / lazyload.js
Created July 24, 2019 03:31
lazy load images
document.addEventListener("DOMContentLoaded", function() {
var lazyloadImages;
if ("IntersectionObserver" in window) {
lazyloadImages = document.querySelectorAll(".lazy");
var imageObserver = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
var image = entry.target;
image.src = image.dataset.src;
@mohnatus
mohnatus / memoizer
Created July 22, 2019 15:19
memoization wrapper
function memoizer(fun){
let cache = {}
return function (n){
if (cache[n] != undefined ) {
return cache[n]
} else {
let result = fun(n)
cache[n] = result
return result
}
@mohnatus
mohnatus / getScrollTop.js
Created May 20, 2019 16:16
get scroll top
function getScrollTop() {
self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}
let printText = (phrases, el, cb) => {
let printPhrase = (phraseIndex) => {
if (phraseIndex >= phrases.length) {
cb();
return;
}
let phrase = texts[phraseIndex];
let phraseLength = phrase.length;
let div = document.createElement('div');
function declOfNum(number, titles) {
let cases = [2, 0, 1, 1, 1, 2];
return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
}
// собираем все якоря; устанавливаем время анимации и количество кадров
const anchors = [].slice.call(document.querySelectorAll('a[href*="#"]')),
animationTime = 300,
framesCount = 20;
anchors.forEach(function(item) {
// каждому якорю присваиваем обработчик события
item.addEventListener('click', function(e) {
// убираем стандартное поведение
e.preventDefault();
@mohnatus
mohnatus / twig-install.php
Created April 13, 2019 13:32
twig installation
<?php
require_once '../vendor/autoload.php';
use \Twig\Environment;
use \Twig\Loader\Filesystem;
use \Twig\Extension\Debug;
$loader = new Twig_Loader_Filesystem('./templates/');
$twig = new Twig_Environment($loader);
@mohnatus
mohnatus / random.js
Created April 12, 2019 10:36
get random integer
// Возвращает случайное целое число между min (включительно) и max (не включая max)
// Использование метода Math.round() даст вам неравномерное распределение!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}