Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
OK, so now we want to get into the JavaScript, which is actually going to be fairly simple, and this
2
I plan on this being one of the first projects in at least in the first 10.
3
So I'm going to explain kind of stuff that many of you might already know.
4
Now, remember, the whole thing.
5
What we need to do is just change the class really on each of these.
6
So the first one has a class of active right now.
7
If I put that class on the second one, just manually by typing it in here, it's going to show the
8
second one.
9
So I'm going to start with that on the first like I had.
10
But we want to be able to switch that in our JavaScript.
11
So we need to first bring in all of these panels into the into our JavaScript files.
12
So I'm going to create a variable called panels and those different way to select elements we can use,
13
you know, get element by D, we can use query selector.
14
So a query selector does is allows us to select anything, could be, you know, a paragraph and each
15
one, a class, an ID, whatever we want the panels which have a class of panel.
16
Now the problem here is there's more than one panel, right.
17
We have all these divs with panels, so we can't just do this or it's just going to select the first
18
one.
19
So we need to use query, select.
20
They're all in that case.
21
And with query, select, they're all instead of just selecting the element directly, it puts all of
22
the panels into what's called the node list, which is similar to an array.
23
In fact, I can show you that if I can.
24
So log here panels.
25
And we open up our chrome dev tools here, you'll see that it's going to it prints out this node list
26
with with each div and some properties, and I can target specific panels, like if I put zero in here
27
because arrays are zero index, it's going to give me the very first panel.
28
You can see it has a class of active, so I could get the second one with one and so on.
29
Now you can also loop through a no list, just like you can with an array.
30
And that's what I want to do.
31
I want to take my panels and use it for each, which is a high order array method.
32
And these methods take in a function as an argument so you could do a function or what you usually see
33
is an arrow function like this.
34
OK, and then we need to pass in here whatever we want to use for each iteration.
35
These are panels.
36
So I'm going to use panel singular and I can console log here panel, and it's just going to loop through
37
and show me each panel.
38
Now, what do we actually want to do here?
39
We want to have an event listener on each of these so that if we click it, something happens.
40
So we'll take each panel and say, add event listener and I want to listen for a click.
41
And when that click happens, we're going to run a function.
42
And for that function, let's just do a console log here and I'll just say, I don't know, one, two,
43
three.
44
And if I click on any of these, we're going to log one, two, three.
45
So I want to whatever when we click on I want to add a class of active.
46
So if I say panel dot class list, that gives me a list of the classes.
47
But we also have methods on this class, this object like add where we can specifically out of class
48
and I'm going to add a class of active.
49
So now if I just close this, if I click on this one, it gets added the class of active, which then
50
changes it to flex five if I click on this one same thing.
51
But you can see that active is still on these other ones.
52
So it's expanding them, but it's not it's not retracting the other ones.
53
So what we'll do is before we we put a class of active let's remove the class of active on all of the
54
other ones.
55
So I'm going to have a function here called Remove Active Classes, and we'll create that down here.
56
That's a function.
57
Remove active classes.
58
Now, since there's more than one panel, we again, we're going to loop through them and then remove
59
all the active classes.
60
So we're going to do just what we did above.
61
So panel dot for each and and here we'll put an arrow function.
62
Now, when you have an arrow function with just one argument like this, because we can also get the
63
index, we can get the the complete array.
64
But since we're only we only need this one panel.
65
We don't even need these curly sorry, these parentheses.
66
We can just do this.
67
All right.
68
And up here we can do the same thing.
69
I mean, you can keep them if you want, but you don't have to.
70
And then in here, let's say for the particular panel that we're looping through, we want on the class
71
list objects we want to call remove and we want to remove all the active classes.
72
OK, so now what's going to happen is when we click, it's going to remove all the active classes from
73
all of them and then put the active class on the one we click.
74
So if I click here,
75
looks like something a panel is not defined.
76
Oh, panels.
77
So we're looping through panels here, which is coming from here.
78
All right.
79
So if I click on this now, you'll see we get the effect that we want because right before it adds the
80
active class to this, it takes it off the rest of them.
81
Now, one thing I would like to change, though, is as soon as I click on this, you'll see the H3
82
and has that kind of weird falling down effect.
83
So let's add a transition to the opacity on the H3.
84
So I'm going to go to the active H3 and let's add a transition here and let's say opacity.
85
So we want to add a transition on the opacity, zero three point six zero point three seconds and ease
86
in and let's add a delay as well.
87
So we'll add zero point four seconds on the delay.
88
And now if I click, you see that it doesn't actually feed in until the until the card is fully expanded,
89
which is a lot smoother than it was before.
90
OK, so that's pretty much it, guys.
91
I mean, you can make these into any kind of card.
92
It doesn't have to be just a heading with a background image.
93
It could be like a testimonial or absolutely anything you want.
94
So hopefully this helps you in building further projects with little widgets like this.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.