Skip to content

Commit 1b6e42f

Browse files
committed
mutation observer
1 parent 57c0b1a commit 1b6e42f

6 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# JavaScript Mutation Observer
2+
3+
> [https://youtu.be/Hn2zzi_lquA](https://youtu.be/Hn2zzi_lquA)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `npm start` to run the app viewable on `http://localhost:9966`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const theOnlyBearsIKnow = ['Polar', 'Brown', 'Grizzly']
2+
3+
setTimeout(function addBear () {
4+
const bears = document.querySelector('ul.bears')
5+
const bear = document.createElement('li')
6+
bear.textContent = theOnlyBearsIKnow[parseInt(Math.random() * theOnlyBearsIKnow.length, 10)]
7+
bears.appendChild(bear)
8+
}, Math.random() * 2000)
9+
10+
setTimeout(function removeBear () {
11+
const bears = document.querySelector('ul.bears')
12+
bears.removeChild(bears.querySelector('li'))
13+
}, Math.random() * 2000 + 2000)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>MutationObserver</title>
6+
</head>
7+
<body>
8+
<ul class="bears">
9+
</ul>
10+
11+
<script src="bundle.js" charset="utf-8"></script>
12+
</body>
13+
</html>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require('./bears.js')
2+
require('./sizer.js')
3+
4+
const observer = new MutationObserver(function (mutations) {
5+
mutations.forEach(function (mutation) {
6+
if (mutation.attributeName === 'style') {
7+
centerModal()
8+
}
9+
if (mutation.addedNodes.length) {
10+
console.log('Added', mutation.addedNodes[0])
11+
}
12+
if (mutation.removedNodes.length) {
13+
console.log('Removed', mutation.removedNodes[0])
14+
}
15+
})
16+
})
17+
const bears = document.querySelector('ul.bears')
18+
observer.observe(bears, {
19+
childList: false,
20+
attributes: true
21+
})
22+
23+
function centerModal () {
24+
const width = parseInt(bears.offsetWidth, 10)
25+
const height = parseInt(bears.offsetHeight, 10)
26+
Object.assign(bears.style, {
27+
top: '50%',
28+
left: '50%',
29+
marginTop: -(height / 2) + 'px',
30+
marginLeft: -(width / 2) + 'px',
31+
})
32+
}
33+
34+
// const poller = setInterval(function () {
35+
// const bear = document.querySelector('ul.bears li')
36+
// if (bear) {
37+
// console.log(bear)
38+
// clearInterval(poller)
39+
// }
40+
// }, 1000)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "javascript-mutation-observer",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js:bundle.js"
8+
},
9+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"budo": "^11.2.0"
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
setInterval(function sizer () {
2+
const bears = document.querySelector('ul.bears')
3+
Object.assign(bears.style, {
4+
position: 'absolute',
5+
border: '1px solid #ddd',
6+
width: (Math.random() * 200) + 'px',
7+
height: (Math.random() * 200) + 'px',
8+
boxShadow: '0px 0px 5px 0px rgba(0,0,0,.3)',
9+
overflow: 'hidden',
10+
padding: '20px',
11+
listStyle: 'none'
12+
})
13+
}, 1000)

0 commit comments

Comments
 (0)