Skip to content

Instantly share code, notes, and snippets.

View b-aleksei's full-sized avatar

Алексей b-aleksei

View GitHub Profile
@b-aleksei
b-aleksei / debounce.js
Created October 21, 2020 16:15
function debounce(func, ms) { let timeout; return () => { clearTimeout(timeout); timeout = setTimeout(() => { func(); }, ms); }; debounce
function debounce(func, ms) {
let timeout;
return () => {
clearTimeout(timeout);
timeout = setTimeout(() => {
func();
}, ms);
};
}
@b-aleksei
b-aleksei / multi-modal.js
Last active December 7, 2021 11:18
multi-modal
const focusableEls = `a[href]:not(:disabled), input:not(:disabled):not([type="hidden"]),
select:not(:disabled), textarea:not(:disabled), button:not(:disabled), [contenteditable],
[tabindex]:not([tabindex="-1"])`;
const isTouchSupported = () => ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
const getHtmlElement = (selector, ctx = document) => {
const node = typeof selector === 'string' ? ctx.querySelector(selector) : selector;
if (node instanceof HTMLElement) {
return node;
}
const ua = window.navigator.userAgent.toLowerCase();
const isIe = (/trident/gi).test(ua) || (/msie/gi).test(ua);
const goToTarget = function (target) { // фолбэк для ie11
const y = target.offsetTop;
const moveTo = function () {
if (window.pageYOffset < y) {
window.scrollBy(0, 60);
setTimeout(moveTo);
@b-aleksei
b-aleksei / phoneMask.js
Last active November 17, 2020 13:24
PhoneMask
const COUNTRY_CODE = 7;
const onInputPhoneInput = ({target}) => {
const matrix = `+${COUNTRY_CODE} (___) ___-__-__`;
let counter = 0;
let val = target.value.replace(/\D/g, '');
if (!val.length) {
val = `${COUNTRY_CODE}`;
}
@b-aleksei
b-aleksei / button.js
Last active August 15, 2020 13:30
button wave
"use strict";
const addElement = function (e) {
const button = e.currentTarget;
const el = e.currentTarget.lastElementChild;
const mValue = Math.max(button.clientWidth, button.clientHeight);
el.style.width = el.style.height = mValue + 'px';
el.classList.remove('pulse');
setTimeout(() => {
@b-aleksei
b-aleksei / navigate-menu.html
Last active March 1, 2021 17:11
navigate menu
<nav class="header__nav nav">
<button class="nav__toggle" type="button" aria-label="Отобразить меню"
aria-controls="main-menu" aria-expanded="false">
<span class="nav__burger"></span>
</button>
<ul id="main-menu" class="nav__list">
<li class="nav__item">
<a class="nav__link" href="#">О нас</a>
</li>
</ul>
// <div id="calendar"></div>
function createCalendar(elem, year, month) {
let mon = month - 1; // месяцы в JS идут от 0 до 11, а не от 1 до 12
let d = new Date(year, mon);
let table = '<table><tr><th>пн</th><th>вт</th><th>ср</th><th>чт</th><th>пт</th><th>сб</th><th>вс</th></tr><tr>';
// пробелы для первого ряда
@b-aleksei
b-aleksei / requestAnimationFrame.js
Last active June 19, 2020 13:07
requestAnimationFrame
// <progress id="elem" style="width: 5%;" onclick="anim.start()"></progress>
class Animation {
constructor({draw, duration}) {
this.draw = draw;
this.duration = duration;
}
animate(time) {
// for IE11
const supportsTemplate = 'content' in document.createElement('template');
// the function takes a template and returns a DOM element
const createElement = supportsTemplate
? function (html) {
const template = document.createElement('template');
template.innerHTML = html;
return template.content.firstElementChild;
}
::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-top: none;
border-bottom: 15px solid red;
border-left: 7px solid transparent;
border-right: 7px solid transparent;