-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
292 lines (282 loc) · 12.4 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>An Introduction to jQuery Events</title>
<meta name="description" content="An introduction to jQuery's Events.">
<meta name="author" content="Russell Heimlich">
<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.css">
<link rel="stylesheet" href="css/theme/sky.css" id="theme">
<style>
.reveal p.via {
font-size:75%;
margin-top:1em;
}
</style>
<!-- For syntax highlighting -->
<link rel="stylesheet" href="css/prism.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>jQuery Events</h1>
<p>presented at</p>
<p><a href="http://www.meetup.com/DC-jQuery-Users-Group/events/87412532/" target="_blank">dcjq[18] Back to Basics</a></p>
<p> </p>
<p>November 12, 2012</p>
</section>
<section>
<h2>Follow Along</h2>
<p><a href="http://v.gd/jqevents" target="_blank">v.gd/jqevents</a></p>
</section>
<section>
<h2>Hi, I'm Russell Heimlich</h2>
<ul>
<li class="fragment">Lead technical figure-outer at
<a href="http://www.pewresearch.org" target="_blank">Pew Research Center</a>
<br><br>
</li>
<li class="fragment">Creator of <a href="http://dummyimage.com" target="_blank">http://dummyimage.com</a><br><br>
</li>
<li class="fragment">Left handed</li>
</ul>
</section>
<section>
<h2>What Are Events?</h2>
<img class="fragment" src="img/dcjq-event.jpeg">
</section>
<section>
<h2>What are <em>JavaScript</em> Events?</h2>
<ul>
<li>JavaScripts adds <strong>interactivity</strong> to a page<br><br></li>
<li>User does something, the page <strong>reacts</strong><br><br></li>
<li>Events <strong>trigger</strong> something else to happen</li>
</ul>
<p class="via">via <a href="http://www.quirksmode.org/js/introevents.html" target="_blank">Introduction to Events</a> by Quirksmode.org</p>
</section>
<section>
<h2>Classic Inline Event Example</h2>
<pre class="language-markup"><code><a href="#" onClick="alert('hello world')">Show Pointless Alert</a></code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/y2E7t/" target="_blank">http://jsfiddle.net/kingkool68/y2E7t/</a></p>
</section>
<section>
<h2>Lots of Events To Choose From</h2>
<ul>
<li>onClick</li>
<li>onDblClick</li>
<li>onFocus</li>
<li>onBlur</li>
<li>onChange</li>
<li>onSubmit</li>
<li>onLoad</li>
</ul>
<p class="via">via <a href="http://www.irt.org/articles/js058/" target="_blank">http://www.irt.org/articles/js058/</a></p>
</section>
<section>
<h2>Keyboard And Mouse Specific Events</h2>
<ul>
<li>onKeyDown</li>
<li>onKeyUp</li>
<li>onKeyPress<br><br></li>
<li>onMouseDown</li>
<li>onMouseUp</li>
<li>onMouseOver</li>
<li>onMouseOut</li>
<li>onMouseMove</li>
</ul>
</section>
<section>
<h2>Inline Event Attributes are Bad</h2>
<ul>
<li>Mix HTML and JavaScript, no separation<br><br></li>
<li>Escaping quotes will drive you insane<br><br></li>
<li>General all-around pain in the rump</li>
</ul>
<p class="via">via <a href="http://stackoverflow.com/questions/6888651/javascript-events-best-practice" target="_blank">JavaScript Events Best Practices</a> on Stackoverflow</p>
</section>
<section>
<h1>Remember Dreamweaver Image Rollovers?</h1>
<p>OMGWTFBBQ?!?!</p>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/ZpaS3/" target="_blank">http://jsfiddle.net/kingkool68/ZpaS3/</a></p>
</section>
<section>
<h2>A Better, Unobtrusive Way to Register Events Arrived</h2>
<pre class="language-javascript"><code>element.addEventListener('click', functionName, false);</code></pre>
<p><strong>click</strong> not <strong>onclick</strong></p>
<p class="via">via <a href="http://www.quirksmode.org/js/events_advanced.html" target="_blank">Advanced Event Registration Models</a> by Quirksmode</p>
</section>
<section>
<h2>But Microsoft Had Their Own Way</h2>
<pre class="language-javascript"><code>element.attachEvent('onclick', functionName);</code></pre>
</section>
<section>
<h2>jQuery To the Rescue!</h2>
<ul>
<li>Smooths out cross-browser differences<br><br></li>
<li>Uses an intuitive syntax for events<br><br></li>
<li>Allows us to writes less, but do more</li>
</ul>
<p class="via">via <a href="http://www.albertopl.com/blog/javascript-vs-jquery-cross-browser-compatibility-event-handlers-and-selectors/" target="_blank">JavaScript vs. jQuery</a> by AlbertoPL</p>
</section>
<section>
<h2>Steps to Register An Event Using Jquery</h2>
<ol>
<li>Select one or more elements<br><br></li>
<li>Tell jQuery what type of event to listen for<br><br></li>
<li>Write a function that will be fired when that event happens</li>
</ol>
</section>
<section>
<h2>What does that look like?</h2>
<p>(Pseudo code)</p>
<pre class="language-javascript"><code>$('selector').event(function() {
//Your Code Goes Here
});</code></pre>
</section>
<section>
<h2>Basic Example</h2>
<pre class="language-javascript"><code>$('a').click(function() {
alert('Hello World');
});</code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/KSyKp/" target="_blank">http://jsfiddle.net/kingkool68/KSyKp/</a></p>
</section>
<section>
<p><img src="http://static.someecards.com/someecards/usercards/1339349657919_8805207.png"></p>
<p class="fragment">Just kidding. There's more…</p>
</section>
<section>
<h2>Using This</h2>
<p><strong>this</strong> is a special variable that refers to the specific element triggering the event.</p>
<pre class="language-javascript"><code>$('a').click(function () {
alert(this.href);
});</code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/MjqZv/" target="_blank">http://jsfiddle.net/kingkool68/MjqZv/</a></p>
</section>
<section>
<h2>Using jQuery With This</h2>
<p>You can use <strong>this</strong> as a selector and use jQuery methods just like any other element.</p>
<pre class="language-javascript"><code>$('li').click(function() {
alert( $(this).text() );
});</code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/aj4N3/" target="_blank">http://jsfiddle.net/kingkool68/aj4N3/</a></p>
</section>
<section>
<h2>Input Validation</h2>
<p>Using <strong>keyup()</strong> attached to an input field, we can check if the value of the input meets a certain criteria.</p>
<pre class="language-javascript"><code>$('input').keyup(function() {
$(this).attr('class', '');
var val = $(this).val();
if( val.length < 4 ) {
$(this).addClass( 'bad' );
}
});
</code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/kUj2W/" target="_blank">http://jsfiddle.net/kingkool68/kUj2W/</a></p>
</section>
<section>
<h2>You Can Interupt Submitting a Form</h2>
<p>This is useful for checking if certain fields are completed or you want to perform an AJAX request.</p>
<pre class="language-javascript"><code>$('form').submit(function() {
if( condition == false ) {
//returning false cancels the form from being submitted.
return false;
}
//returning true will allow the form to be submitted.
return true;
});
</code></pre>
</section>
<section>
<h2>Are you sure you want to search Google?</h2>
<p>Displays a confirmation box asking if you want to search for the entered term on Google.</p>
<pre class="language-javascript"><code>$('form').submit(function() {
return confirm('Are you sure you want to search Google?');
});
</code></pre>
<p class="jsfiddle"><a href="http://jsfiddle.net/kingkool68/fZQEJ/" target="_blank">http://jsfiddle.net/kingkool68/fZQEJ/</a></p>
</section>
<section>
<h2>Getting Additional Event Details</h2>
<p>jQuery passes an event object to the function being triggered by the event.</p>
<p> </p>
<p>The event object is normalized across different browsers and contains extra data about the context of the event.</p>
<p class="via">via <a href="http://api.jquery.com/bind/#event-object" target="_blank">http://api.jquery.com/bind/#event-object</a></p>
</section>
<section>
<h2>The Event Object Contains Data Like:</h2>
<ul>
<li><code><a href="http://api.jquery.com/event.type/" target="_blank">type</a></code> - click, keyup, submit etc.</li>
<li><code><a href="http://api.jquery.com/event.which/" target="_blank">which</a></code> - key codes or mouse buttons</li>
<li><code><a href="http://api.jquery.com/event.pageX/" target="_blank">pageX</a></code> - mouse position relative to left edge of document</li>
<li><code><a href="http://api.jquery.com/event.pageY/" target="_blank">pageY</a></code> - mouse position relative to top edge of document</li>
</ul>
<p class="via">via <a href="http://api.jquery.com/category/events/event-object/" target="_blank">Event Object jQuery API Docs</a></p>
</section>
<section>
<h2>Follow the Twitter Example</h2>
<p>Get x most recent followers and move them around the screen as a swarm based on where you click.</p>
<pre class="language-javascript"><code>$(document).mouseup(function(e) {
imgAttract(e.pageX, e.pageY);
});
</code></pre>
<p class="via">via <a href="http://dev.kingkool68.com/war10k/follow-the-twitter.htm" target="_blank">Follow the Twitter</a> by Me</p>
</section>
<section>
<h2>Stuff I Don't Have Time To Cover But You Should Know</h2>
<ul>
<li><a href="http://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing" target="_blank">What is Event Bubbling?</a><br><br></li>
<li><a href="http://api.jquery.com/event.preventDefault/" target="_blank">event.preventDefault()</a><br><br></li>
<li><a href="http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false" target="_blank">preventDefault() vs. Return False</a></li>
</ul>
</section>
<section>
<h2>Resources</h2>
<ul>
<li><a href="http://api.jquery.com/category/events/" target="_blank">api.jquery.com/category/events/<br><br></a></li>
<li><a href="http://jqfundamentals.com/chapter/events" target="_blank">Events - jQ Fundamentals</a><br><br></li>
<li><a href="http://www.learningjquery.com/2008/03/working-with-events-part-1" target="_blank">learningjquery.com/2008/03/working-with-events-part-1<br><br></a></li>
<li><a href="https://www.youtube.com/watch?v=XXwhegVTWQg" target="_blank">youtube.com/watch?v=XXwhegVTWQg</a></li>
</ul>
</section>
<section>
<h1>Questions?</h1>
<p>Tweet me, <a href="http://www.twitter.com/kingkool68/" target="_blank">@kingkool68</a>, or <a href="http://www.russellheimlich.com/contact.html" target="_blank">contact me</a>.</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script src="js/prism.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,
center: true,
theme: Reveal.getQueryHash().theme || 'sky', // 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>