Skip to content

Instantly share code, notes, and snippets.

View vldvel's full-sized avatar

Vlad vldvel

  • Amsterdam
View GitHub Profile
@vldvel
vldvel / MutationObserver-MutationRecord.js
Created March 22, 2018 15:34
MutationObserver MutationRecord
MutationRecord = {
addedNodes : [], // NodeList
attributeName : null, // attribute name - string or null
attributeNamespace : null, // attribute namespace
nextSibling : null, // next sibling in DOM
oldValue : null, // old value
previousSibling : null, // previous sibling in DOM
removedNodes : [], // NodeList of removed nodes
target : Element, // target element
type : "childList" // mutation type one of childList, attributes or characterData
@vldvel
vldvel / MutationObserver-target.js
Created March 22, 2018 08:26
MutationObserver target
const target1 = document; // correct
const target2 = document.body; // correct
const target3 = document.getElementById('blockId'); // correct
const target4 = window; // wrong
@vldvel
vldvel / MutationObserver-constructor.js
Created March 22, 2018 07:53
MutationObserver constructor
// create subscriber
function subscriber (mutations) {
mutations.forEach((mutation) => {
console.log(mutation);
});
}
// create observer
const observer = new MutationObserver(subscriber);
@vldvel
vldvel / MutationObserver-subscriber-simple.js
Last active October 19, 2019 03:12
MutationObserver subscriber simple
function subscriber(mutations) {
console.log(mutations);// [...]
console.log(mutations.constructor);// Array
mutations.forEach((mutation) => {
console.log(mutation); // {...}
console.log(mutation.constructor); // MutationRecord
});
@vldvel
vldvel / MutationObserver-config.js
Created March 21, 2018 15:04
MutationObserver config
// by default all false
// you can pick as many as you want, but at least one of - attributes, characterData, or childList
const observerConfig = {
attributes: true, // attribute changes will be observed | on add/remove/change attributes
attributeOldValue: true, // will show oldValue of attribute | on add/remove/change attributes | default: null
characterData: true, // data changes will be observed | on add/remove/change characterData
characterDataOldValue: true, // will show OldValue of characterData | on add/remove/change characterData | default: null
@vldvel
vldvel / MutationObserver-simple.js
Last active March 22, 2018 10:20
MutationObserver simple
// target element that we will observe
const target = document.body;
// config object
const config = {
attributes: true,
attributeOldValue: true,
characterData: true,
characterDataOldValue: true,
childList: true,
@vldvel
vldvel / Box-shadow.js
Created March 15, 2018 15:24
Box shadows in JavaScript
const offsetX = 10; // horizontal distance - any number
const offsetY = 10; // vertical distance - any number
const blurRadius = 20; // shadow blur - positive number - default = 0
const spreadRadius = 40; // shadow size - any number - deafult = 0
const color = 'rgba(255, 255, 199, 0.7)'; // color - rgb/rgba/hsl/hsla
// basic options
element0.style.boxShadow = `${offsetX}px ${offsetY}px ${color}`;
// full options
@vldvel
vldvel / Notification.js
Last active March 15, 2018 12:34
Notification API
function createNotification () {
// first let's check browser support
if (!('Notification' in window)) {
throw new Error('Browser doesn\'t support notifications');
}
// now let's look whether permission is already granted or not
switch (Notification.permission) {
case 'granted': {
return innerCreateNotification;
}
@vldvel
vldvel / Notification-close-simple.js
Created March 15, 2018 12:15
Notification close simple
const notification = new Notification('Hello world!');
setTimeout(() => {
notification.close();
}, 600);
@vldvel
vldvel / Notification-permission-request.js
Created March 15, 2018 06:46
Notification request permission
Notification.requestPermission();