-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfoundations.html
More file actions
811 lines (737 loc) · 36.8 KB
/
foundations.html
File metadata and controls
811 lines (737 loc) · 36.8 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Jiwon Shin">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<link rel="stylesheet" href="css/style.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.16/addons/p5.dom.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<title>Foundations of Web Development</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-110518171-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-110518171-1');
</script>
</head>
<style>
.col{
width: calc(100% / 3);
float: left;
}
.form-row{
width: 33%;
float: left;
margin-bottom: 1rem;
}
#panel{
min-height: 450px;
}
#typePanel{
width: 100%;
height: 2em;
text-align: center;
}
#typedText{
font-size: 2em;
}
</style>
<body>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<main class="mdl-layout__content">
<div class="page-content">
<div class="full-page-content">
<h4>Foundations of Web Development</h4>
<h5>General</h5>
<p class="p-head"><u>File Organization</u></p>
<p>A website generally will contain similar file types. This means that you can decide on a structure for websites and keep it that way. Once you get used to how to structure your files for the web, giving file paths for your images, videos, audios, stylesheets, etc., will no longer be confusing. In the image above, within the folder of my website "assignment1", I have three major folders: assets, css, and js. The two folders, css and js, are pretty self-explanatory. They contain css stylesheets and javascript files, respectively.</p>
<img class="p-mid-img" src="img/foundations/folderstructure.png">
<p>The assets folder contains all of my media assets. Inside, it has three other folders: audio, img and video. This way, I can upload my media assets into their categories and keep them organized.</p>
<p>Please keep in mind that this isn't the only way that you can structure your folders and files. What is important is that you try to keep a uniform format that you are comfortable with and repeatedly use.</p>
<p class="p-head"><u>Including External Files to HTML: CSS and Javascript</u></p>
<p>Generally, CSS stylesheets are added between the <head> tags and Javscript files are added <b>AFTER</b> the closing </body> tag.</p>
<img class="p-mid-img" src="img/foundations/link.png">
<img class="p-mid-img" src="img/foundations/script.png">
<p>Reason for this is that CSS styles needs to be loaded before HTML to apply the styles as the HTML elements are loaded, and Javascript files need the HTML elements inside <body> to load beforehand, because they usually contain code that use and manipulate the HTML elements.</p>
<p>Assuming that you are following the layout of the folder structure as described in the previous section, if we need to access image files from the css stylesheet, we would need to set the path to the file like below:</p>
<pre>
background-image: url("../assets/img/imgName.jpg");
</pre>
<p>The <b>../</b> is a way of indicating to move out of the current folder. In this case, since the style.css file is inside css folder, it will move one folder out into the root folder of the project, where index.html is located, then go into the assets folder then img folder to locate a file called imgName.jpg</p>
<p class="p-head"><u>Indentation</u></p>
<p>Although indentation has no effect in the actual result of the website, it is important to keep a practice of neatly indenting your code for your own good. As the code gets more complex and long, it will help you reduce errors as well as visually see where a single line of code fits into forming the structure / logic of the web page.</p>
<img class="p-mid-img" src="img/foundations/indent.png">
<p>Above image is an example of proper indentation for HTML pages. In HTML, another added reason for good indentation will be that CSS styles are <b>inherited</b> by the elements that are surrounding an html element. For example, if using percentage as unit of the size of .col-img <img> tags, it will depend on what the defined sizes are for the .col and .row <div> elements.</p>
<img class="p-mid-img" src="img/foundations/indent-js.png">
<p>In javascript, indentation becomes especially helpful when using multiple loops and if-statements. It will help you see what sections of code are inside an if-statement or a loop as well as help you see you have the correct number of brackets.</p>
<p class="p-head"><u>Comments</u></p>
<p>In any programming language, you can <b>comment</b> on the source code, meaning that you can make annotations for you and your collaborators. Generally, it is a good practice to make comments that briefly describe what different parts of the code do. You can also make comment out sections of code that you want to keep as reference but no longer want. If you are a complex function, commenting is a good way to make note of the syntax for future reference. Comments will not be executed and will be ignored by the web browser. Future you will appreciate the comments you make today!</p>
<p>Comments can be written followed by a group of characters that differ from a programming language to another.</p>
<p><u>HTML</u>:</p>
<pre>
<!-- This is a comment in html -->
</pre>
<img class="p-mid-img" src="img/foundations/comment-html.png">
<p><u>CSS</u>:</p>
<pre>
/* This is a comment in css */
</pre>
<img class="p-mid-img" src="img/foundations/comment-css.png">
<p><u>Javascript</u>:</p>
<pre>
// This is a one line comment in javascript
/*
This is a multi line comment
in javascript
*/
</pre>
<img class="p-mid-img" src="img/foundations/comment-js.png">
<h5>HTML</h5>
<p class="p-head">Class & Id</p>
<p>There are three major ways that HTML elements can be referenced in CSS and Javascript: by tag name, class and id. Tag name is the most general way of referencing HTML elements. Referencing the <p> tag will select ALL <p> tags in the HTML document. Classes can be used to select a <b>GROUP of HTML elements</b>, and thus one class can be given to multiple HTML elements. Ids are used to select a <b>SINGLE HTML element</b>, thus is given to an HTML element that you want to separate out / is an unique element on an html document. Below example will help make a more clear distinction between use of classes and ids.</p>
<style>
.code-demo-row{
width: 200px;
height: 100px;
margin: auto;
}
.code-demo-col{
width: 50%;
height: 100%;
float: left;
}
#firstBox{
background-color: red;
}
#secondBox{
background-color: blue;
}
#thirdBox{
background-color: green;
}
#fourthBox{
background-color: yellow;
}
</style>
<div class="code-demo-row">
<div id="firstBox" class="code-demo-col"></div>
<div id="secondBox" class="code-demo-col"></div>
</div>
<div class="code-demo-row">
<div id="thirdBox" class="code-demo-col"></div>
<div id="fourthBox" class="code-demo-col"></div>
</div>
<p><u>HTML</u>:</p>
<pre>
<div class="row">
<div id="firstBox" class="col"></div>
<div id="secondBox" class="col"></div>
</div>
<div class="row">
<div id="thirdBox" class="col"></div>
<div id="fourthBox" class="col"></div>
</div>
</pre>
<p><u>CSS</u>:</p>
<pre>
.row{
width: 200px;
height: 100px;
margin: auto;
}
.col{
width: 50%;
height: 100%;
float: left;
}
#firstBox{
background-color: red;
}
#secondBox{
background-color: blue;
}
#thirdBox{
background-color: green;
}
#fourthBox{
background-color: yellow;
}
</pre>
<p>Above code is the basic HTML structure for creating a 2 by 2 grid. Within each <div> of class .row, there are two <div> of class .col - hence classes are used for applying a single set of styles for multiple elements. Here, you can see that classes should be used to elements that you want to have the same styles. Each <div> element of class .col also have a unique id that are being used to apply different background-color property. Thus, id is used to apply differentiated and unique values for an individual HTML element.</p>
<p class="p-head">Attributes</p>
<p>Attributes are properties that belong to a particular HTML element. Attributes of HTML tags are written inside of the opening tag in the form of:</p>
<pre>
<p attribute="value"></p>
</pre>
<p>This should look familiar, because we've seen things like below before:</p>
<pre>
<img class="grid-img" width="100%" src="img/1.jpg">
</pre>
<p>In the above line inside the <img> tag, class, width and src are all <b>attributes</b> of the <img> tag. Some HTML tags have <b>required attributes</b>. For example, src is a required attribute for <img>. Some attributes are applicable only for certain HTML tags, again like src for <img> - because src cannot be used for a <p> element, for example. However, there are attributes that can be used for <b>ANY</b> HTML elements (they are called global attributes) - two of the most commonly used being <b>class</b> and <b>id</b>. There are also another type of attributes that we've seen - event attributes.</p>
<p><b>Event Attributes:</b></p>
<p>Event attributes are used to have an event trigger javascript code for a specific HTML element. There are other ways of attaching javascript code to an event (in pure javascript), but you can also simply attach a function to an HTML element using an event attribute like below:</p>
<pre>
<img onclick="changeImg()" src="img/1.jpg">
</pre>
<p>The list below includes commonly used event attributes:</p>
<ul>
<li>Window Events:</li>
<ul>
<li>onload</li>
<li>onresize</li>
<li>onscroll</li>
</ul>
<li>Mouse Events:</li>
<ul>
<li>onclick</li>
<li>ondblclick</li>
<li>onmouseover</li>
<li>onmouseout</li>
<li>onmousemove</li>
</ul>
<li>Keyboard Events:</li>
<ul>
<li>onkeypress</li>
<li>onkeyup</li>
<li>onkeydown</li>
</ul>
<li>Media (Audio & Video) Events:</li>
<ul>
<li>oncanplaythrough</li>
<li>onended</li>
<li>onpause</li>
<li>onplay</li>
<li>onratechange</li>
<li>onseeking</li>
<li>ontimeupdate</li>
<li>onvolumechange</li>
</ul>
</ul>
<p class="p-head">Non-Roman characters: Charset</p>
<p>To be able to display non-English / or non-roman characters on a web page, you will need to specify that the web page also uses sets of non-roman characters. This can be done by simply adding the line below within the <head> tag.</p>
<pre>
<meta charset="utf-8">
</pre>
<h5>CSS</h5>
<p class="p-head">Units</p>
<p><b>px</b> is the <u>absolute unit of measurement</u>, meaning that 1 px on one screen is generally the same as 1 px on aother screen. But this also means that if you set a width of an HTML element in px, no matter the size of the screen, it will always the absolute fixed size. One alternative is % (percentage).</p>
<p>There are actually a lot more units of measurement than just pixels and percentages you can use in css. This <a href="https://www.w3schools.com/cssref/css_units.asp" target="_blank">w3schools page</a> shows all the existing units of measurement in css. Out of the many units of measurement, I want to emphasize just a couple: <b>vw, vh and em</b>.</p>
<p><b>vw</b> and <b>vh</b> stands for <u>"viewport width"</u> and <u>"viewport height"</u>. 1 vw is 1% of the viewport width, which means the current width of the window. This means that if the width of your window changes, the absolute size (in pixels) of 1 vw will change accordingly. Thus, 100 vw will signify the width of your browser window and 100 vh will be the height of your browser window. You can also make something 120 vw, which would be the 120% of your viewport width.</p>
<p>See below demo for comparison between %, vw and vh: (<a target="_blank" href="css-units-simulator.html">Click here for full page demo.</a>)</p>
<iframe id="code-demo" height="500px" src="css-units-demo.html"></iframe>
<br>
<form id="panel" oninput="checkNum()" onchange="checkVal()">
<div class="form-col">
Unit:<br>
<div class="form-row">
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="uVal" value="%" checked>
<span class="mdl-radio__label">%</span>
</label>
</div>
<div class="form-row">
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="uVal" value="vw">
<span class="mdl-radio__label">vw</span>
</label>
</div>
<div class="form-row">
<label class="mdl-radio mdl-js-radio mdl-js-ripple-effect">
<input type="radio" class="mdl-radio__button" name="uVal" value="vh">
<span class="mdl-radio__label">vh</span>
</label>
</div>
</div>
<br><br>
<hr>
<br>
<div class="col">
<span style="color:red;">RED Outer <div></span>:<br>
Width: <span id="outer-width">100%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="outer" min="0" max="100" value="100" tabindex="0">
</p>
<br>
Height: <span id="outer-height">100%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="outer" min="0" max="100" value="100" tabindex="0">
</p>
Margin Top: <span id="outer-mt">0%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="outer" min="0" max="100" value="0" tabindex="0">
</p>
<br>
Margin Left: <span id="outer-ml">0%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="outer" min="0" max="100" value="0" tabindex="0">
</p>
<br>
</div>
<div class="col">
<span style="color:blue;">BLUE Middle <div></span>:<br>
Width: <span id="mid-width">50%</span>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="mid" min="0" max="100" value="50" tabindex="0">
</p>
<br>
Height: <span id="mid-height">50%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="mid" min="0" max="100" value="50" tabindex="0">
</p>
Margin Top: <span id="mid-mt">0%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="mid" min="0" max="100" value="0" tabindex="0">
</p>
<br>
Margin Left: <span id="mid-ml">0%</span>
<br>
<p style="width: 80%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="mid" min="0" max="100" value="0" tabindex="0">
</p>
<br>
</div>
<div class="col">
<span style="color: green">GREEN Inner <div></span>:<br>
Width: <span id="inner-width">50%</span>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="inner" min="0" max="100" value="50" tabindex="0">
</p>
<br>
Height: <span id="inner-height">50%</span>
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="inner" min="0" max="100" value="50" tabindex="0">
</p>
Margin Top: <span id="inner-mt">0%</span>
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="inner" min="0" max="100" value="0" tabindex="0">
</p>
<br>
Margin Left: <span id="inner-ml">0%</span>
<br>
<p style="width: 100%; margin-top: 1rem;">
<input class="mdl-slider mdl-js-slider" type="range" name="inner" min="0" max="100" value="0" tabindex="0">
</p>
<br>
</div>
</form>
<br>
<hr>
<br>
<p><b>em</b> is a unit based on the size of the font. em means the width of your letter "m". 1 em means the width of letter "m" according to the font size you set for that particular div (or the inherited size of the font size of the div) For example, if you are sizing the width of your <h1> element using em, and if your h1 tag has font-size of 32px, 1 em = 32 px.</p>
<p class="p-head">Positioning Elements</p>
<p>Centering text elements: such as <h1> and <p></p>
<pre>
text-align: center;
</pre>
<p>Centering non-text elements: such as <div> and <img></p>
<pre>
position: relative;
display: block;
margin: auto;
</pre>
<p>The <b>margin: auto;</b> line of CSS automatically adjusts the margin-left and margin-right to center the HTML element <b>HORIZONTALLY</b>.</p>
<h5>Javascript</h5>
<p>The best way to get use to Javascript is simply to play with it. Sections of the Javascript simulator are added below, but <a target="_blank" href="dom-bom-events.html">click here to see the full version</a>.</p>
<p class="p-head">Looking up HTML element(s)</p>
<pre>
document.getElementById('idName')
</pre>
<p>: returns a single HTML element of id "idName".</p>
<pre>
document.getElementsByClassName('className')
</pre>
<p>: returns an <b>ARRAY</b> of HTML elements of class "className". You can reference individual elements of the array using an <b>ARRAY INDEX</b>.</p>
<pre>
var boxes = document.getElementsByClassName('box');
for(var i = 0; i < boxes.length; i++){
boxes[i].style.width = i * 50 + "px";
}
</pre>
<p>Above for-loop iterates through all elements of class "box" and sets the width of each div to be in increasing multiples of 50px.</p>
<pre>
document.getElementsByTagName('h1')
</pre>
<p>: returns an <b>ARRAY</b> of HTML elements of tag "h1". Same as the way for getElementsByClassName, you can access individual elements using an <b>ARRAY INDEX</b>.</p>
<pre>
var titles = document.getElementsByClassName('title');
for(var i = 0; i < titles.length; i++){
titles[i].style.color = red;
}
</pre>
<p class="p-head">Changing CSS with .style</p>
<p>You can look up the CSS styles applied to a particular HTML element as well as set them to a value of your choice. To refer to the css related to a particular HTML element, simply add .style after. If you want to look up / set a value of a particular CSS property, add another . (dot) and follow it by that CSS property. If the CSS property you want to look up / set has a - (dash) in its name, change the way it is written using camel casing. Camel casing is a way of writing compound / multiple words that usually replaces spaces, but in this cas dashs, by capitalizing the word that would usually come after the space / dash. For example, background-color can be rewritten as backgroundColor.</p>
<hr>
<div style="background-color: black; color: white; padding: 24px;" id="text-box">
<p style="font-size: 24px; text-align: center; margin:0;" id="inner-text">Javascript is fun!</p>
</div>
<br>
<p><u>HTML</u>:</p>
<pre>
<div id="text-box">
<p id="inner-text">Javascript is fun!</p>
</div>
</pre>
<p><u>CSS</u>:</p>
<pre>
#text-box{
background-color: black;
color: white;
padding: 24px;
}
#inner-text{
font-size: 24px;
text-align: center;
}
</pre>
<hr>
<div class="code-window">
<div style="width: calc(100% - 64px - 4rem); margin-right: 1rem" class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" id="js-editor-3" type="text" name="tVal" value="document.getElementById('text-box').style.backgroundColor = 'pink'">
<label class="mdl-textfield__label" for="js-editor-3">Your javascript code...</label>
</div>
<button onclick="runCode(this)" class="mdl-button mdl-js-button mdl-button--raised code-run">RUN</button>
<pre class="code-result"></pre>
</div>
<br>
<p class="p-head">DEMO 1: Type Animation with Javascript</p>
<p>This demo focuses on the use of <b>arrays</b>, <b>if-statements</b> and <b>setInterval</b> function.</p>
<hr>
<div id="typePanel"><span id="typedText"></span></div>
<hr>
<p><u>HTML</u>:</p>
<pre>
<div id="typePanel"><span id="typedText"></span></div>
</pre>
<p><u>CSS</u>:</p>
<pre>
#typePanel{
width: 100%;
height: 2em;
text-align: center;
}
#typedText{
font-size: 2em;
}
</pre>
<p><u>Javascript</u>:</p>
<pre>
var words = ["Hello.", "Welcome.", "How are you today?"];
var currText = "";
var currWord = "";
var wordIndex = 0;
var letterIndex = 0;
var typeDirection = 0;
setInterval(function(){
if(typeDirection == 0){
currWord = words[wordIndex];
currText += currWord[letterIndex];
if(letterIndex < words[wordIndex].length - 1){
letterIndex++;
}else{
typeDirection = 1;
}
}else{
currText = words[wordIndex].slice(0, letterIndex);
if(letterIndex > 0){
letterIndex--;
}else{
if(wordIndex < words.length - 1){
wordIndex++;
}else{
wordIndex = 0;
}
typeDirection = 0;
}
}
document.getElementById('typedText').innerHTML = currText;
}, 200);
</pre>
<p class="p-head">DEMO 2: onclick Event with variable <b><i>this</i></b></p>
<p>This demo demonstrates the use of <b><i>this</i></b> variable</p>
<iframe height="400px" src="foundations-click-demo.html"></iframe>
<p><u>HTML</u>:</p>
<pre>
<div id="box-container"></div>
</pre>
<p><u>CSS</u>:</p>
<pre>
body, html{
margin: 0;
background-color: white;
}
.box{
width: 10vw;
height: 10vh;
background-color: red;
}
</pre>
<p><u>Javascript</u>:</p>
<pre>
var newDiv;
window.onload = function(){
for(var i = 0; i < 10; i++){
newDiv = document.createElement("DIV");
newDiv.className = "box";
newDiv.setAttribute("onclick", "changeBG(this)");
newDiv.style.marginLeft = i * 10 + "vw";
document.getElementById("box-container").appendChild(newDiv);
}
}
function changeBG(currDiv){
currDiv.style.backgroundColor = "blue";
}
</pre>
</div>
<footer class="mdl-mini-footer">
<div class="mdl-mini-footer__left-section">
<ul class="mdl-mini-footer__link-list">
<li><u><a class="p-link" href="index.html" target="_self">BACK TO MAIN</a></u></li>
</ul>
</div>
<div class="mdl-mini-footer__right-section">
<ul class="mdl-mini-footer__link-list">
<li>Workshops By <u><a class="p-link" href="http://jiwonshin.com" target="_blank">Jiwon Shin</a></u></li>
</ul>
</div>
</footer>
</div>
</main>
</div>
</body>
<script src="js/background.js"></script>
<script>
var currUnit = "%";
var outDiv, midDiv, inDiv;
function checkNum(){
outDiv = document.getElementById('code-demo').contentDocument.getElementById('outer-div');
midDiv = document.getElementById('code-demo').contentDocument.getElementById('mid-div');
inDiv = document.getElementById('code-demo').contentDocument.getElementById('inner-div');
outDiv.style.width = document.getElementById('outer-width').innerHTML = document.getElementsByName('outer')[0].value + currUnit;
outDiv.style.height = document.getElementById('outer-height').innerHTML = document.getElementsByName('outer')[1].value + currUnit;
outDiv.style.marginTop = document.getElementById('outer-mt').innerHTML = document.getElementsByName('outer')[2].value + currUnit;
outDiv.style.marginLeft = document.getElementById('outer-ml').innerHTML = document.getElementsByName('outer')[3].value + currUnit;
midDiv.style.width = document.getElementById('mid-width').innerHTML = document.getElementsByName('mid')[0].value + currUnit;
midDiv.style.height = document.getElementById('mid-height').innerHTML = document.getElementsByName('mid')[1].value + currUnit;
midDiv.style.marginTop = document.getElementById('mid-mt').innerHTML = document.getElementsByName('mid')[2].value + currUnit;
midDiv.style.marginLeft = document.getElementById('mid-ml').innerHTML = document.getElementsByName('mid')[3].value + currUnit;
inDiv.style.width = document.getElementById('inner-width').innerHTML = document.getElementsByName('inner')[0].value + currUnit;
inDiv.style.height = document.getElementById('inner-height').innerHTML = document.getElementsByName('inner')[1].value + currUnit;
inDiv.style.marginTop = document.getElementById('inner-mt').innerHTML = document.getElementsByName('inner')[2].value + currUnit;
inDiv.style.marginLeft = document.getElementById('inner-ml').innerHTML = document.getElementsByName('inner')[3].value + currUnit;
}
function checkVal(){
// var selectSpan = document.getElementById('code-demo').contentDocument.getElementsByClassName('selection')[0];
for(var i = 0; i < document.getElementsByName('uVal').length; i++){
if(document.getElementsByName('uVal')[i].checked){
currUnit = document.getElementsByName('uVal')[i].value;
}
}
checkNum();
}
var currCode, codeEval, codeResult;
var currHTML;
var strHTML = "";
function runCode(currButton){
codeResult = currButton.nextElementSibling;
codeResult.style.height = "100%";
strHTML = "";
// console.log(currButton);
currCode = currButton.previousElementSibling.firstElementChild.value;
// console.log(currCode);
if(currCode.search('=') > -1){
doCode();
}else if(currCode.indexOf('(') > -1){
if(currCode.search('window') > -1 || currCode.search('alert') > -1 || currCode.search('confirm') > -1 || currCode.search('prompt') > -1){
doCode();
}else{
getCode();
}
}
else{
getCode();
}
}
function getCode(){
if(currCode == "document"){
currCode = "document.documentElement";
}
codeEval = eval(currCode);
if(codeEval == null){
currHTML = 'Error! Please double check your script!'
// strHTML = currHTML;
}else if(codeEval == ""){
currHTML = "No Result!"
}else if(codeEval == window){
currHTML = "Can't print the whole window object here!";
}else if(typeof codeEval == "undefined"){
currHTML = "successfully ran " + currCode;
}
else{
if(codeEval.outerHTML == null){
if(typeof codeEval == "object"){
currHTML = JSON.stringify(codeEval);
}else{
currHTML = codeEval;
}
}else{
currHTML = codeEval.outerHTML;
}
}
if(typeof currHTML == "number"){
currHTML = currHTML.toString();
}
for(var i = 0; i < currHTML.length; i++){
if(currHTML[i] == "<"){
strHTML += "<"
}else{
strHTML += currHTML[i];
}
}
// console.log(strHTML);
codeResult.innerHTML = strHTML;
if(codeResult.clientHeight > 200){
codeResult.style.height = "200px";
}
}
function doCode(){
codeResult.innerHTML = "successfully executed: " + currCode;
try{
eval(currCode);
}catch(e){
if(e instanceof SyntaxError){
codeResult.innerHTML = "Syntax Error! Check your script again!";
}
}
}
var events;
var sDiv;
function applyEvent(){
events = document.getElementsByName('eVal');
sDiv = document.getElementById('square');
for(var i = 0; i < events.length; i++){
if(events[i].checked){
sDiv.setAttribute(events[i].id, chooseEvent(i));
}else{
sDiv.removeAttribute(events[i].id);
}
}
updateDivText();
}
function chooseEvent(i){
switch(i){
case 1:
return "dblclickEvent()";
break;
case 2:
return "onmouseoverEvent()";
break;
case 3:
return "onmouseoutEvent()";
break;
default:
return "clickEvent()";
break;
}
}
function updateDivText(){
var divEval = eval(sDiv).outerHTML;
var divHTML = "";
for(var i = 0; i < divEval.length; i++){
if(divEval[i] == "<"){
divHTML += "<"
}else{
divHTML += divEval[i];
}
}
document.getElementById('currDiv').innerHTML = divHTML;
}
function clickEvent(){
sDiv.style.backgroundColor = "red";
updateDivText();
}
function dblclickEvent(){
sDiv.style.backgroundColor = "blue";
updateDivText();
}
function onmouseoverEvent(){
sDiv.style.border = "solid 5px green";
updateDivText();
}
function onmouseoutEvent(){
sDiv.style.border = "dotted 10px yellow";
updateDivText();
}
function onkeypressEvent(){
var keyVal = event.key;
if(keyVal == " "){
keyVal = "Space";
}
document.getElementById('keyInput').innerHTML = "You pressed: " + keyVal;
}
function onkeydownEvent(){
document.getElementById('keyBox').style.backgroundColor = "pink";
}
function onkeyupEvent(){
document.getElementById('keyBox').style.backgroundColor = "lightblue";
}
function resetStyles(){
sDiv.removeAttribute('style');
updateDivText();
}
var words = ["Hello. ", "Welcome. ", "How are you today? "];
var currText = "";
var currWord = 0;
var currLetter = 0;
var typeDirection = 0;
var typeSpeed = 200;
setInterval(function(){
// if(currLetter < words[currWord].length - 1){
// currLetter++;
// }else{
// if(currWord < words.length - 1){
// currWord++;
// }else{
// currWord = 0;
// }
// currText = "";
// currLetter = 0;
// }
if(typeDirection == 0){
currText += words[currWord][currLetter];
if(currLetter < words[currWord].length - 1){
currLetter++;
}else{
typeDirection = 1;
}
}else{
currText = words[currWord].slice(0, currLetter);
if(currLetter > 0){
currLetter--;
}else{
if(currWord < words.length - 1){
currWord++;
}else{
currWord = 0;
}
typeDirection = 0;
}
}
document.getElementById('typedText').innerHTML = currText;
}, typeSpeed);
</script>
</html>