forked from girldevelopit/gdi-featured-js-intro
-
Notifications
You must be signed in to change notification settings - Fork 100
/
class4.html
321 lines (288 loc) · 12.6 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 4 ~ JavaScript for Beginners ~ Girl Develop It</title>
<meta name="description" content="This is an introduction to JavaScript curriculum, developed by Sylvia Richardson for the Raleigh/Durham 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="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdidefault.css" id="theme">
<link rel="stylesheet" href="css/custom.css">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor--><link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[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">
<!-- Opening slide -->
<section>
<img src="images/gdi_logo_badge.png" alt="GDI logo">
<h3>JavaScript for Beginners</h3>
<h4>Class 4</h4>
</section>
<section>
<h3>Events</h3>
<p>An 'event' is a type of object that is created when the user interacts with a web page.</p>
<p>For example, JS creates an event when a user clicks an element.</p>
<pre><code>
element.onclick = function () {
//code to execute when the click happens
};
</code></pre>
</section>
<section>
<h3>Types of Events</h3>
<p>There are <a href="http://www.w3schools.com/jsref/dom_obj_event.asp">variety of events</a>. Some of the most common:</p>
<ul>
<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>How do you make an event trigger some code?</p>
<ol>
<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>You can call a function directly from your HTML code:</p>
<pre><code>
<button id="clickMe" onclick="sayHi()">Click Me!</button>
</code></pre>
<pre><code>
function sayHi() {
alert ('Hi!');
}
</code></pre>
</section>
<section>
<h3>Listening Functions</h3>
<p>Listening functions work like many of the other things we have done. First, find the object:</p>
<pre><code>
var myTarget = document.getElementById('clickMe');
</code></pre>
<p>Then add an event to that object:</p>
<pre><code>
myTarget.onclick=function(){
alert ('Hi!');
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Download <a href="files/class4.zip">this week's sample code</a>.</p>
<p>Make some JavaScript code fire after an <code>onmouseover</code> event. Try adding the event to the HTML and adding a listening function.</p>
</section>
<section>
<h3>Preventing Defaults</h3>
<p>Elements like buttons and links have a default behaviors. However, the <code>event</code> objects has a built-in method to handle that:</p>
<pre><code>
element.onclick = function (event) {
event.preventDefault();
};
</code></pre>
</section>
<section>
<h3>This</h3>
<p>The keyword <code>this</code> references the element that the user has just interacted with</p>
<pre><code>
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>Write code that targets this link:</p>
<pre><code>
<a href="http://girldevelopit.com/" id="gdiLink">Girl Develop It</a>
</code></pre>
<p>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>
<h3>Forms</h3>
<p>You can collect information from users to use in functions. The most common method is an HTML form</p>
<pre><code>
<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 />
<label for="submitButton"></label> <input id="tempSubmitButton" type="submit" value="Submit" />
</form>
</code></pre>
</section>
<section>
<h3>Retrieving Form Data</h3>
<p>You retrieve the values of form elements using the <code>value</code> method.</p>
<pre><code>
var temperature = document.getElementById('temp').value;
console.log (temperature);
</code></pre>
<p>You can retrieve the value of a form at any time. Try <code>onblur</code> (when a form element loses focus).</p>
</section>
<section>
<h3>Radio Buttons</h3>
<p>Radio buttons usually do not have IDs, so you will need to use an array:</p>
<pre><code>
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>If you are going to retrieve form values with the submit button, be sure to prevent the default action!</p>
<pre><code>
var submitButton = document.getElementById('tempSubmitButton');
submitButton.onclick = function () {
event.preventDefault();
var temperature = document.getElementById('temp').value;
console.log (temperature);
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Collect a value from the input box on the page. 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>
<h3>Timing Events</h3>
<p>In JavaScript, is possible to execute some code at specified time intervals.</p>
<ul>
<li><code>setInterval()</code> executes a function over and over again, at specified time intervals</li>
<li><code>setTimeout()</code> executes a function, once, after waiting a specified number of milliseconds</li>
</ul>
</section>
<section>
<h3>setInterval</h3>
<p>The syntax for <code>setInterval()</code> is:</p>
<pre><code>
setInterval(function, milliseconds);
</code></pre>
<p>For example, this is a simple clock:</p>
<pre><code>
var simpleClock=setInterval(myClock, 1000);
function myClock() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById('resultsBox').innerHTML = t;
}
</code></pre>
</section>
<section>
<h3>clearInterval</h3>
<p>You can use <code>clearInterval()</code> to stop a setInterval loop</p>
<pre><code>
clearInterval(intervalVariable)
</code></pre>
<p>To stop our clock:</p>
<pre><code>
function myStopFunction() {
clearInterval(simpleClock);
}
</code></pre>
</section>
<section>
<h3>setTimeout</h3>
<p>The method <code>setTimeout()</code> is similar, but it will only run once.</p>
<pre><code>
setTimeout(function, milliseconds);
</code></pre>
<p>For example, this code will wait 3 seconds, then create a popup box, unless the user triggers the <code>clearTimeout()</code></p>
<pre><code>
var helloBox;
function sayHello() {
helloBox=setTimeout(function(){alert('Hello')},3000);
}
function dontSayIt() {
clearTimeout(helloBox);
}
</code></pre>
</section>
<section>
<h3>Animations</h3>
<p>By changing values slowly over time, we can create simple animations</p>
<pre><code>
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
}
</code></pre>
</section>
<section>
<h3>Let's Develop It</h3>
<p>Using the function from the previous slide as a base, try changing some animation parameters. What happens?</p>
</section>
<section>
<h3>Resources</h3>
<ul>
<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">Code Academy</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>
</div><!-- Close .slides -->
<footer>
<div class="copyright">
JavaScript for Beginners ~ 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="reveal/lib/js/head.min.js"></script>
<script src="reveal/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,
touch: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>