forked from gdi-curriculum-review/gdi-jquery-intro
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass4.html
357 lines (324 loc) · 16.3 KB
/
class4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Intro to JavaScript : Class 4 ~ Girl Develop It</title>
<meta name="description" content="This is an introduction to JavaScript curriculum, developed by Sylvia Richardson for the Raleigh/Durham chapter, modified by Julie Cameron and Sara Gibbons for the Ann Arbor chapter. The course is meant to be taught in 4 two-hour sections. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/gdiaa.css" id="theme">
<link rel="stylesheet" href="lib/css/light.css">
<link rel="stylesheet" href="css/print/pdf.css" media="print">
<script src="lib/js/head.min.js"></script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to JavaScript</span></h3>
<h4><span class="blue">Class 4</span></h4>
</section>
<section>
<h3>Review</h3>
<ul class="copy--small box--small list--tall">
<li>Objects</li>
<li>Manipulating the DOM</li>
</ul>
</section>
</section>
<section>
<section>
<h3>Events</h3>
<p class="copy--small box--small">An 'event' is a type of object that is created when the user interacts with a web page.</p>
<p class="copy--small box--small">For example, JS creates an event when a user clicks an element.</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
element.onclick = function () {
//code to execute when the click happens
};
</code></pre>
</section>
<section>
<h3>Types of Events</h3>
<p class="copy--small box--small">There are a <a href="http://www.w3schools.com/jsref/dom_obj_event.asp">variety of events</a>. Some of the most common:</p>
<ul class="copy--small box--small list--tall">
<li>onclick: The event occurs when the user clicks on an element</li>
<li>onmouseover: The event occurs when the pointer is moved onto an element</li>
<li>onkeyup: The event occurs when the user releases a key</li>
<li>onload: The event occurs when a document has been loaded</li>
<li>onfocus: The event occurs when an element gets focus</li>
</ul>
</section>
<section>
<h3>Events and Code</h3>
<p class="copy--small box--small">How do you make an event trigger some code?</p>
<ol class="copy--small box--small list--tall">
<li>Break your code into functions</li>
<li>Associate a function with a JavaScript event</li>
</ol>
</section>
<section>
<h3>Calling Functions from HTML</h3>
<p class="copy--small box--small">You can call a function directly from your HTML code:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
<button id="clickMe" onclick="sayHi()">Click Me!</button>
</code></pre>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
function sayHi() {
alert('Hi!');
}
</code></pre>
</section>
<section>
<h3>Listening Functions</h3>
<p class="copy--small box--small">Listening functions work like many of the other things we have done. First, find the object:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var myTarget = document.getElementById('clickMe');
</code></pre>
<p class="copy--small box--small">Then add an event to that object:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
myTarget.onclick = function(){
alert('Hi!');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Download <a href="files/class4.zip">this week's sample code</a>.</p>
<p class="copy--small box--small">Make some JavaScript code fire after an <code contenteditable>onmouseover</code> event. Try adding the event to the HTML and adding a listening function.</p>
</section>
</section>
<section>
<section>
<h3>Preventing Defaults</h3>
<p class="copy--small box--small">Elements like buttons and links have default behaviors. However, the <code contenteditable>event</code> objects has a built-in method to handle that:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
element.onclick = function (event) {
event.preventDefault();
};
</code></pre>
</section>
<section>
<h3>This</h3>
<p class="copy--small box--small">The keyword <code contenteditable>this</code> references the element that the user has just interacted with:</p>
<pre style="height: 200px; overflow: hidden;"><code contenteditable>
element.onmouseover = function (event) {
console.log(this);
};
element.onclick = function (event) {
this.style.backgroundColor = 'red';
this.innerHTML = 'I've been clicked!';
};
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Write code that targets this link:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
<a href="http://girldevelopit.com/" id="gdiLink">Girl Develop It</a>
</code></pre>
<p class="copy--small box--small">When a user clicks the link, the page should display an error message instead of going to the Girl Develop It homepage.</p>
</section>
</section>
<section>
<section>
<h3>Forms</h3>
<p class="copy--small box--small">You can collect information from users to use in functions. The most common method is an HTML form:</p>
<pre style="height: 200px; overflow: hidden;"><code contenteditable>
<form id="temperatureForm">
<label for="temp">Temperature:</label>
<input type="text" id="temp" size="3"/> degrees in
<input type="radio" name="tempFormat" value="F" checked />Fahrenheit
<input type="radio" name="tempFormat" value="C" />Celsius <br />
<input type="submit" id="tempSubmitButton" value="Submit" />
</form>
</code></pre>
</section>
<section>
<h3>Retrieving Form Data</h3>
<p class="copy--small box--small">You retrieve the values of form elements using the <code contenteditable>value</code> method:</p>
<pre style="height: 160px; overflow: hidden;"><code contenteditable>
var temperature = document.getElementById('temp').value;
console.log(temperature);
</code></pre>
<p class="copy--small box--small">You can retrieve the value of a form at any time.<br/>Try <code contenteditable>onblur</code> (when a form element loses focus).</p>
</section>
<section>
<h3>Radio Buttons</h3>
<p class="copy--small box--small">Radio buttons usually do not have IDs,<br/>so you will need to use an array:</p>
<pre style="height: 240px; overflow: hidden;"><code contenteditable>
var radios = document.getElementsByName('tempFormat');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
var radioButtonValue = radios[i].value;
// only one radio can be checked, so stop now
break;
}
}
</code></pre>
</section>
<section>
<h3>Submit buttons</h3>
<p class="copy--small box--small">If you are going to retrieve form values with the submit button, be sure to prevent the default action!</p>
<pre style="height: 200px; overflow: hidden;"><code contenteditable>
var submitButton = document.getElementById('tempSubmitButton');
submitButton.onclick = function (event) {
event.preventDefault();
var temperature = document.getElementById('temp').value;
console.log(temperature);
}
<!--line 84 has a purple { opening bracket, but line 188 has a white } closing bracket. should they be the same color?-->
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Collect a value from the input box on the page.</p>
<p class="copy--small box--small">Use it inside a function of some kind. For example, collect a number and multiply it by five or collect a name and display a greeting.</p>
</section>
</section>
<section>
<section>
<h3>Timing Events</h3>
<p class="copy--small box--small">In JavaScript, it is possible to execute some code<br/>at specified time intervals.</p>
<ul class="copy--xsmall box--small list--tall">
<li><code contenteditable>setInterval()</code> executes a function over and over again, at specified time intervals</li>
<li><code contenteditable>setTimeout()</code> executes a function, once, after waiting a specified number of milliseconds</li>
</ul>
</section>
<section>
<h3>setInterval</h3>
<p class="copy--small box--small">The syntax for <code contenteditable>setInterval()</code> is:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
setInterval(function, milliseconds);
</code></pre>
<p class="copy--small box--small">For example, this is a simple clock:</p>
<pre style="height: 200px; overflow: hidden;"><code contenteditable>
var simpleClock = setInterval(myClock, 1000);
function myClock() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById('resultsBox').innerHTML = t;
}
<!--line 214 has a purple { opening bracket, but line 218 has a white closing bracket, should it also be purple?} -->
</code></pre>
</section>
<section>
<h3>clearInterval</h3>
<p class="copy--small box--small">You can use <code contenteditable>clearInterval()</code> to stop a setInterval loop:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
clearInterval(intervalVariable);
</code></pre>
<p class="copy--small box--small">To stop our clock:</p>
<pre style="height: 100px; overflow: hidden;"><code contenteditable>
function myStopFunction() {
clearInterval(simpleClock);
}
<!--line 230 has a purple opening bracket { but line 232 has a white } closing bracket, should they be the same color? -->
</code></pre>
</section>
<section>
<h3>setTimeout</h3>
<p class="copy--small box--small">The method <code contenteditable>setTimeout()</code> is similar, but it will only run once:</p>
<pre style="height: 80px; overflow: hidden;"><code contenteditable>
setTimeout(function, milliseconds);
</code></pre>
<p class="copy--small box--small">For example, this code will wait 3 seconds, then create a popup box, unless the user triggers the <code contenteditable>clearTimeout():</code></p>
<pre style="height: 220px; overflow: hidden;"><code contenteditable>
var helloBox;
function sayHello() {
helloBox = setTimeout(function(){alert('Hello')},3000);
}
function dontSayIt() {
clearTimeout(helloBox);
}
<!--line 245 and line 248 have purple opening brackets, but lines 247 and 250 have white closing brackets, should they all be the same colors? -->
</code></pre>
</section>
<section>
<h3>Animations</h3>
<p class="copy--small box--small">By changing values slowly over time,<br/>we can create simple animations:</p>
<pre><code contenteditable>
var targetPic = document.getElementById('turtlePic');
targetPic.onclick = function () {
var leftMarginValue = 0;
function increaseMargin() {
leftMarginValue++; // update parameter each time
targetPic.style.marginLeft = leftMarginValue + 'px'; //set left margin
if (leftMarginValue === 200) { // check finish condition
clearInterval(movePic);
}
}
var movePic = setInterval(function(){increaseMargin()}, 10); // update every 10ms
}
<!--lines 259, and 261, have purple opening { brackets, lines 266, and 268} have white closing brackets. line 267 has a purple opening bracket { and a white closing bracket }, should these all match? -->
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p class="copy--small box--small">Using the function from the previous slide as a base, try changing some animation parameters. What happens?</p>
</section>
</section>
<section>
<section>
<h3>Resources</h3>
<ul class="copy--small box--small list--tall">
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide" target="_blank">JavaScript Guide</a>, from the Mozilla Developers Network.</li>
<li><a href="http://www.codecademy.com/tracks/javascript" target="_blank">Codecademy</a>, with interactive JavaScript lessons to help you review.</li>
<li><a href="https://www.khanacademy.org/computing/cs/programming">Khan Academy</a> has a lot more information abut drawing and animations.</li>
</ul>
</section>
<section>
<h3>Questions?</h3>
<p class="box"><a href="https://gdiaa.slack.com/messages/javascript/details/" target="_blank" class="roll"><span>#javascript</span></a></p>
<p><small>Not on Slack? <a target="_blank" href="http://bit.ly/gdiaa-slack" class="roll"><span data-title="bit.ly/gdiaa-slack">bit.ly/gdiaa-slack</span></a></small></p>
</section>
<section>
<img src="images/gdi_logo_badge.png" class="img--bare" height="450px" />
<div class="box--small">
<h3><span class="green">Intro to JavaScript</span></h3>
<h4><span class="blue">Thank you for joining us!</span></h4>
</div>
</section>
</section>
</div><!-- Close .slides -->
<footer>
<div class="copyright">
Intro to JavaScript ~ Girl Develop It ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div><!-- Close .copyright -->
</footer>
</div><!-- Close .reveal -->
<script src="js/jquery.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
rollingLinks: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>