02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Making our slideshow more dynamic

In the last lesson we left off where our slideshow was dependent on how many label names the buttons of the slide had. In this lesson, we'll revert back and update this logic by removing those labels and making our slideshow capable of running unlimited slides. In the process, we will discover the jQuery data method.

Video Transcript

00:00Although promised that we'll move on to the next topic, not yet, there's one thing that
00:05we didn't change that we really wanted to.
00:07Right now, we're depending to know what button are we in based on that label that we gave,
00:13that class that we gave each button.
00:15I want to delete that label and show you how can we figure it out without it.
00:20And with it, we're going to learn about another feature of jQuery, which enables us to save
00:23data inside of our elements.
00:26So let's see what I mean by that.
00:27I actually lied.
00:29I didn't intend to lie.
00:30I am sorry.
00:31All right.
00:32So let's get back what I forgot that we wanted also to get rid of all these in our three and
00:37what I'm going to do is I'm just going to get rid of the class button because we could
00:43do better than that.
00:44We don't even need that.
00:46We get rid of that, get rid of that and get rid of that.
00:49So if I save this and I'm going to go back to my code now, everything won't work anymore.
00:54But we're going to fix that immediately.
00:56Now our problem right now is the reason why I want to fix this double.
01:00One is that we're relying too heavily on the HTML, which we don't want to do is if you look
01:06at this line of code that we're doing every single time there's a mouse over what we're
01:11doing is we're calling one function, a second function, a third function, a fourth function.
01:18So we're calling four functions every single time and we could avoid calling four functions
01:22and just call one.
01:24So to be able to do that, what I want to do is I want to assign arbitrary value basically
01:30using, well, I'll just show you how to do it, right?
01:33So first of all, what I'm going to do, oh, that was commented out.
01:37So what I'm going to do is I'm going to create here, I'm going to take my buttons again
01:41that I just got here and originally they're coming as a string.
01:44So I'm going to wrap it around and make sure that they're going to be a jQuery element.
01:49And as we know, it's going to be an array.
01:51What I want to do is I want to cycle through each item, basically setting my eye first at
01:56zero and expanding my eye as long as I is smaller than my buttons length.
02:04And we want to grow it each time by one.
02:06Now what I want to do is I want to approach each button.
02:09So I'm going to basically approach each button by going to my buttons and the position
02:14that we're currently in.
02:16And what I want to do is I want to approach this item and tell it this.
02:22So I got into it.
02:23I want to tell it, hey, I want you to add a data attribute to yourself.
02:27And this is going to be something that I'll be able to fetch at any point using jQuery later.
02:32So the data that I want to arbitrary add in is not that arbitrary.
02:35I want to add an index into it.
02:37And I'm going to set that index to be the eye that we're currently at.
02:41So literally what I'm doing here is I'm using a for loop to cycle through all the buttons.
02:46And then to assign the value inside of our index value to our property.
02:51All that we really need to do now is we could literally just delete this whole thing.
02:55And don't worry, you have a copy of that original sequence up above there where we
02:59commented out.
03:00I could literally delete basically all of this.
03:03And instead of approaching an attribute, just go to my data directly and say, hey,
03:07could I fetch that index right now because I'm going to need it.
03:10And there you go.
03:11And now we have access to that index and we only called basically two functions
03:16instead of calling five or four, whatever it was before.
03:19Now above that, what we could do and should do is now that we're using our our
03:24buttons more than once, we don't need to wrap it into the jQuery twice because we
03:28already wrapped it up here.
03:30So I could literally just go ahead and just approach that directly,
03:33cutting down another call to a function, which is always a good thing.
03:36So whenever you don't need to call a function, don't call it.
03:40And that's it.
03:40Now, if I save this and run my application, it should now run smoothly.
03:44And just that as it did before.
03:45And now it's not dependent on those values.
03:47We don't have those button values in it anymore.
03:50So if I click on refresh and try it one more time, we'll see that now when I go
03:54over here, everything's working exactly the way we expected it to work,
03:58just a little bit better and more dynamic.
04:00So that's it.
04:01In our next lesson, we're going to continue looking into other plugins to try
04:04to make our animation even smoother and even cooler.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!