Skip to content

Instantly share code, notes, and snippets.

@alizhdanov
alizhdanov / Content.js
Created November 8, 2017 09:18
Render vue components from string
// IF you need render from string in vue
// The beset example is a blog post, where your content are recieving from a server and you don't have control of it.
import Vue from 'vue';
import CustomComponent1 from './CustomComponent1.vue'
import CustomComponent2 from './CustomComponent1.vue'
export default {
props: ['res'],
@alizhdanov
alizhdanov / addEventListener.js
Last active October 23, 2017 11:48
addEventListener once - cross browser
// Regular version - unfortunately supported only by very lates browsers (chrome 60+, ff 55+, edge 16+, safari 10.1+)
el.addEventListener('eventName', fn, {once: true})
// decision - working everywhere
el.addEventListener('eventName', function cb(event) {
// function code here
event.currentTarget.removeEventListener(event.type, cb)
})
import Util from './src/util'
const Default = {
toggle : true,
parent : ''
}
class Collapse {
constructor(element, config) {
this._isTransitioning = false
@alizhdanov
alizhdanov / toArray.js
Created October 19, 2017 15:55
Nodelist (HTMLCollection) to Array
// All credits and inspiration from this article https://hackernoon.com/htmlcollection-nodelist-and-array-of-objects-da42737181f9
const nodelist = document.querySelectorAll('.articles');
// Array.from method
// not supporting IE, Safari >= 9, probably will require pollyfill in your project
const array = Array.from(nodelist)
// Array.prototype.slice
// if i'm right browser support for this method even better than for HTMLColections, no pollyfils bro
@alizhdanov
alizhdanov / sequence.js
Last active October 6, 2017 11:10
Generate array sequence (ES6)
[...Array(N).keys()] // will generate array sequence with N length
// example
[...Array(5).keys()] // [0,1,2,3,4]
// Don't hesitate to use map if you need array started from some specific index
[...Array(5).keys()].map(i => i+1) // [1,2,3,4,5]
// Also You can create some fancy util function
const getSequnce = (n, offset=0) => {

Regexp - шпаргалка

Синтаксис для создания

var regexp = new RegExp("шаблон", "флаги");
// or
var regexp = /шаблон/; // без флагов
var regexp = /шаблон/gmi; // с флагами gmi
@alizhdanov
alizhdanov / uuid.js
Last active October 3, 2017 15:28
Fast and easy way to make unique id
Math.random().toString(36).slice(2)
gulp.task('images-webp', function () {
return gulp.src('./public/assets/img/**/*.{jpg,png}')
.pipe(webp())
.pipe(gulp.dest('./public/assets/img/'))
.pipe(gulp.dest('./public/assets/img/'));
});
gulp.task('images-webp-posts', function () {
return gulp.src('./public/files/post/**/*.{jpg,png}')
.pipe(webp())
@alizhdanov
alizhdanov / app.js
Last active November 8, 2016 09:13
Moving body background
// single-line solution
$('body').css('background-position', 'center ' + (-window.pageYOffset / 8) + 'px');
// more advanced solution
$('.paralax').each(function() {
var position = (-window.pageYOffset) + $(this).offset().top
position = -(position / 8)
if (position >= ($(this).height() + $(this).height() / 2)) {
@alizhdanov
alizhdanov / scrollTop.js
Created August 21, 2016 20:03
pure javascript scrollTop function
function scrollTo(element, to, duration) {
if (duration < 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 2;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
scrollTo(element, to, duration - 2);
}, 10);
}