Skip to content

Commit 186686f

Browse files
committed
13-20
1 parent 74b30a8 commit 186686f

File tree

10 files changed

+805
-17
lines changed

10 files changed

+805
-17
lines changed

13 - Slide in on Scroll/index-START.html

+67-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,70 @@ <h1>Slide in on Scroll</h1>
5858
};
5959
}
6060

61+
class slideIn {
62+
constructor(className) {
63+
this.images = document.getElementsByClassName(className);
64+
this.events = 'bind';
65+
}
66+
67+
set events(type) {
68+
const handlers = this.handlers = this.handlers || {},
69+
eventHandlers = [
70+
{
71+
context: window,
72+
type : 'scroll',
73+
callback : 'checkSlide',
74+
option : true,
75+
deBounce: debounce
76+
}
77+
],
78+
isFrozen = Object.isFrozen(handlers);
79+
80+
eventHandlers.forEach( h => {
81+
const func = this[h.callback].bind(this),
82+
callback = 'function' == typeof h.deBounce ? h.deBounce( func ) : func;
83+
84+
h.callback = isFrozen ? handlers[h.callback] : handlers[h.callback] = callback;
85+
return this[type](h);
86+
});
87+
88+
Object.freeze(handlers)
89+
90+
}
91+
92+
bind(listen) {
93+
listen.context.addEventListener( listen.type, listen.callback, listen.option )
94+
}
95+
96+
unbind(listen) {
97+
listen.context.removeEventListener( listen.type, listen.callback, listen.option )
98+
}
99+
100+
showImage(image) {
101+
image.classList.add('active');
102+
}
103+
104+
hideImage(image) {
105+
image.classList.remove('active');
106+
}
107+
108+
checkSlide(e){
109+
Array.prototype.forEach.call(this.images, (image, index) => {
110+
const domRect = image.getBoundingClientRect(),
111+
halfWayToTop = domRect.bottom - domRect.height / 2;
112+
113+
if ( halfWayToTop <= window.innerHeight && domRect.bottom > 0) {
114+
this.showImage(image)
115+
} else {
116+
this.hideImage(image)
117+
}
118+
})
119+
120+
}
121+
}
122+
123+
new slideIn('slide-in');
124+
61125
</script>
62126

63127
<style>
@@ -68,11 +132,11 @@ <h1>Slide in on Scroll</h1>
68132
font-size: 20px;
69133
font-weight: 200;
70134
}
71-
135+
72136
body {
73137
margin: 0;
74138
}
75-
139+
76140
*, *:before, *:after {
77141
box-sizing: inherit;
78142
}
@@ -107,7 +171,7 @@ <h1>Slide in on Scroll</h1>
107171
.align-left.slide-in {
108172
transform: translateX(-30%) scale(0.95);
109173
}
110-
174+
111175
.align-right.slide-in {
112176
transform: translateX(30%) scale(0.95);
113177
}

14 - JavaScript References VS Copying/index-START.html

+41-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@
1616

1717
// You might think we can just do something like this:
1818

19+
const playersTwo = players;
20+
1921
// however what happens when we update that array?
2022

23+
playersTwo.push('Seed');
24+
2125
// now here is the problem!
2226

27+
console.log( 'players: ', players );
28+
29+
players.splice(-1,1);
30+
31+
console.log( 'restored players: ', players );
32+
2333
// oh no - we have edited the original array too!
2434

2535
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
@@ -28,10 +38,30 @@
2838

2939
// one way
3040

31-
// or create a new array and concat the old one in
41+
const playerSlice = players.slice();
42+
43+
playerSlice.push('Seed Slice');
44+
45+
console.log( 'playerSlice: ', playerSlice );
3246

47+
// or create a new array and concat the old one in
48+
const playerConcat = [].concat(players);
49+
playerConcat.push('Seed concat');
50+
console.log( 'playerConcat: ', playerConcat );
3351
// or use the new ES6 Spread
3452

53+
const playersSpread = [...players];
54+
55+
playersSpread.push('seed')
56+
57+
console.log( 'playersSpread: ', playersSpread );
58+
59+
console.log( 'players: ', players );
60+
61+
playerFrom = Array.from(players);
62+
63+
console.log( 'playerFrom: ', playerFrom );
64+
3565
// now when we update it, the original one isn't changed
3666

3767
// The same thing goes for objects, let's say we have a person object
@@ -44,8 +74,18 @@
4474

4575
// and think we make a copy:
4676

77+
personRef = person;
78+
79+
personRef.age = 88
80+
81+
console.log( 'person: ', person );
82+
4783
// how do we take a copy instead?
4884

85+
personCopy = Object.assign({},person, {age : 80})
86+
console.log( 'personCopy: ', personCopy );
87+
console.log( 'person: ', person );
88+
4989
// We will hopefully soon see the object ...spread
5090

5191
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.

0 commit comments

Comments
 (0)