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

Playing with the ScrollToPlugin

There are actually a lot of sites online that use scrolling fx. Not very complicated and quite fun. With that said, let's make things a bit more complicated as we talk about how to stop events from propagating. Don't worry we are not going to dig deep into the subject, just deep enough to show you how to stop events(for more check our object oriented course).

Video Transcript

00:00You remember that really cool website that had like these really cool as it's animating animations were happening and all that stuff
00:07Which today I think every single site does that I think everyone got the same template somewhere, but anyways
00:14The way you do that is with animating the scroll of the user
00:17Well, maybe it's hidden but anyways, we need to know how to scroll things for that, right?
00:21And for that we have the scroll plug in really easy
00:24We're gonna look at it look at it and learn a few more things in the process because we like learning things
00:29right and with it we will complete this chapter and as my window is
00:34Suggesting I am gonna retire for the day and go get dinner was ready to wrap up this chapter
00:40But before we do I wanted to show you one last plug-in for this chapter, but don't worry
00:45We're gonna see a couple more in the next chapter
00:48All right. So the plug-in that I want to show you is the plug-in that enables you to scroll things that are scrollable
00:55It's a very cute very lovely
00:58plugin and
01:00To be able to use it. We're gonna want to incorporate the
01:04scroll
01:05Scroll to there you go. So we got the scroll to I'm just gonna copy this now anything that is scrollable that we can scroll
01:12We'll be able to what I'm gonna do is I'm gonna focus on our actual window itself and we're gonna scroll our full window
01:18So just copy away just the script there and remember I'm not gonna say it again
01:22But basically version, you know if we cross that 23 kilobytes
01:26It's probably time to move up to use tween max because it will incorporate most of the plugins
01:31But you know you have to make sure and check which ones are in this case scroll to is not incorporated into the tween max
01:38automatically
01:39Alright, so if I grab that and I just go right into my code
01:42Let's go ahead and add that script into our script and to be able to create this animation
01:48I'm gonna go all the way to the bottom of my screen and right before my close my body
01:52I want to add here a break line and I'm gonna add quite a lot of them because I want to make sure that we
01:58Have what to scroll and obviously this is not a real world type of scenario, but who cares?
02:04It's not about real world right now
02:06And next stage is what I want it what I want to do next is I'm gonna create here a quick little button
02:11I'm gonna call it a I'm gonna give it an idea of jump top and
02:16I'm gonna give it a link to let's say it's gonna go to
02:19itself and
02:21I am gonna go
02:24Go to top now for this to work one
02:28What I'm gonna do is I'm gonna actually activate it in a minute
02:30But let's first create a scroll now that we have scrollable content all I really need to do
02:35I'm just gonna go into my on ready function to the bottom here
02:38And I'm gonna create a new tween light and this time around I'm gonna tween to my window itself will be the object
02:44Window you could put anything that's scrollable into here
02:48The next thing that I'm gonna do is I'm gonna animate the time will be two seconds, and I'm just gonna call this scroll
02:55scroll
02:57To
02:59And set it inside of it an object and define inside of it what I'm scrolling in my case
03:04I'm gonna scroll my y and I want to send it to the maximum of our screen so to do that
03:09I'm gonna type here the value of max
03:12I could be your also numeric values by the way which will do that momentarily and now if I say this go back to my app
03:18Go here and click on refresh
03:20We're gonna see that it's gonna load up and then it's gonna two seconds animate down to our go to top
03:25Now let's let's activate our go to top to actually animate its way back up and to do that
03:31We're gonna step right back into our editor, and we're gonna add a little bit more code a little bit more logic
03:36I'm gonna actually approach that button and because I called it jump to I'm gonna give it that idea of
03:41Jump to jump top that was and I'm gonna create a click event here and inside of it
03:48I'm gonna send to it a function. This is a jQuery
03:51event that enables me to trigger whenever a click happens and
03:56I'm just gonna make sure that I'm opening closing that function. I'm literally sending an anonymous function right into that click event
04:03It also accepts an event, and that's actually really important for us, and we're gonna see why in a second here
04:08I like calling it e for short really usually so I'll just leave it as e cuz for me
04:12It's obvious that it's an event in your case. It might not be so just type the whole word event
04:16So it'll be easy for you to recognize that this is an event an object of the event that's actually happening
04:22So I'm gonna go ahead and create the our tween and it's not really gonna work initially
04:26And then we're gonna look into it and understand why it wasn't working
04:29And I'm just gonna go ahead and just copy this full animation, but instead of going to the max
04:33I'm just gonna send the value of zero to go back to the top
04:37now if I do that and I go back into my application and I click on refresh and
04:42We're on the top and I go to the bottom and I click on refresh
04:45It's actually gonna reload the page and you see that up here, but it didn't create an actual animation
04:51The reason is very simple
04:52The reason is is because our our button by default is gonna load our page all over
04:57So for me to stop my egg from my browser from reloading the page what I have to do before I actually
05:04Asks to animate or even after I'm gonna go to my event and say hey mr. Event
05:09prevent
05:10Default now by doing that. I'm basically I'm basically I'm letting me just make sure that I wrote it correctly
05:16prevent default
05:18Perfect what I'm actually doing is I'm basically telling the browser. I'm taking over this event
05:23Don't trigger what you usually would have triggered now if I save this and go back into my application and click click on refresh
05:29It's not going to automatically auto
05:32Refresh the page when I click on this it's gonna leave that to my function and I could do in my function whatever I want
05:38So if I click on here now, it's gonna actually work
05:40It's gonna go to the top and we're not gonna have that jump that we saw previously
05:46So that's it. We're done with chat with this chapter. We're not done with plugins
05:50But we've explored many of the three loosely tied plugins in the next chapter
05:56We're gonna dive much deeper into plugins
05:58But specifically plugins that are more
06:03They go into the intricacies of our animation
06:06We're just breaking them down also because we want to break down our chapter so they don't become too big
06:11So we'll see you in the next chapter where we explore more fun things. Goodbye for now

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!