Skip to content

Instantly share code, notes, and snippets.

@andreyshr
andreyshr / gist:de0f60c0fd32af45d102fff0286abec0
Last active May 13, 2021 12:00
Получение размера localStorage
var getLocalStorageSize = function() {
var total = 0;
for (var x in localStorage) {
// Value is multiplied by 2 due to data being stored in `utf-16` format, which requires twice the space.
var amount = (localStorage[x].length * 2) / 1024 / 1024;
if (!isNaN(amount) && localStorage.hasOwnProperty(x)) {
total += amount;
}
}
return total.toFixed(2);
@andreyshr
andreyshr / MediaQueries
Last active September 21, 2018 09:47
MediaQueries
// media query event handler
if (matchMedia) {
const mq = window.matchMedia("(min-width: 500px)");
mq.addListener(WidthChange);
WidthChange(mq);
}
// media query change
function WidthChange(mq) {
if (mq.matches) {
function declOfNum(number, titles) {
cases = [2, 0, 1, 1, 1, 2];
return titles[ (number%100>4 && number%100<20)? 2 : cases[(number%10<5)?number%10:5] ];
}
use:
declOfNum(count, ['найдена', 'найдено', 'найдены']);
@andreyshr
andreyshr / getSelectedText()
Last active September 21, 2018 09:48
getSelectedText()
// функция для получение выделенного текста
function getSelectedText(){
var text = "";
if (window.getSelection) {
text = window.getSelection();
}else if (document.getSelection) {
text = document.getSelection();
}else if (document.selection) {
text = document.selection.createRange().text;
}
@andreyshr
andreyshr / horizontalWheelScroll()
Last active August 2, 2018 10:44
horizontalWheelScroll()
const container = document.querySelector('.scroll-container');
container.addEventListener('wheel', horizontalWheelScroll);
function horizontalWheelScroll (event) {
let modifier;
if (event.deltaMode === event.DOM_DELTA_PIXEL) {
modifier = 1;
// иные режимы возможны в Firefox
} else if (event.deltaMode === event.DOM_DELTA_LINE) {
modifier = parseInt(getComputedStyle(this).lineHeight);
@andreyshr
andreyshr / formatDigits(str)
Created August 2, 2018 07:43
formatDigits(str)
function formatDigits(str) {
return str.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');
}
@andreyshr
andreyshr / sliceText(textNodes, symbols)
Last active August 2, 2018 07:38
sliceText(textNodes, symbols)
function sliceText(textNodes, symbols) {
textNodes.forEach(cur => {
const text = cur.innerText;
let sliced = text.slice(0,symbols);
if (sliced.length < text.length) {
sliced += '...';
cur.innerText = sliced;
}
});
@andreyshr
andreyshr / randomNumber(min, max)
Last active August 2, 2018 07:38
randomNumber(min, max)
function randomNumber(min, max) {
return min + Math.floor(Math.random() * (max + 1 - min));
}
@andreyshr
andreyshr / bottomButton
Last active September 25, 2017 11:58
bottomButton
$("#bottom").on("click", function (event) {
event.preventDefault();
var id = $(this).attr('href'),
top = $(id).offset().top;
$('body,html').animate({
scrollTop: top
}, 1200);
});
@andreyshr
andreyshr / visually-hidden
Created September 16, 2017 10:12
visually-hidden
.visually-hidden {
position: absolute;
clip: rect(0 0 0 0);
width: 1px;
height: 1px;
margin: -1px;
}