Dynamic visualization with JavaScript and jQuery
One of the great powers of javascript when it comes to visualization is the capability of improving user experience based on variables and changes even in the users environment. In this lecture we will test to see the time of day and based on it change our CSS to create a darker or lighter background based on if its day time or night.
Keynotes:
- Loading in jQuery
- Loading in CSS
- Creating a custom user experience with JavaScript.
Video Transcript
00:00Welcome to the last section of our HTML5 web visualization course.
00:06In this section, we're going to be talking about JavaScript and jQuery.
00:11Our main topics are going to be how to use jQuery and JavaScript
00:15to create a more visually engaging site,
00:18really focusing more on animations.
00:21We're going to talk about how to starting with dynamic visualizations,
00:24how to make our designs update and reflect what the users want
00:29using JavaScript.
00:30Our example is going to be using a daytime site versus a nighttime site.
00:35We'll learn more about that in this lecture.
00:38In the next lecture, we're going to talk about creating a typewriter effect.
00:42And this effect will be really cool.
00:43And with it show you the steps involved with creating basic animations.
00:49We're also after that, once we have that typewriter in place,
00:53we're going to want to create a generic typewriter effect mechanism.
00:57To do that, we're going to need to create a queuing order
00:59so we could send in animations.
01:01In two lectures, we're going to go through the process of creating a queue
01:05and also getting feedback from our animation when it completes
01:08so we could activate the next item in the queue.
01:11Last but not least, we're going to go back into talking about animation libraries.
01:15With it, we'll take a look at how could you animate using jQuery
01:19and hint over other libraries and possibilities when it comes to animation.
01:23So let's jump right into it.
01:27In this lecture, we're going to start off with dynamic visualization.
01:31What that means is we're going to go ahead and we're going to...
01:33First of all, we're going to load in our jQuery.
01:35After we load in the jQuery, we're going to load in a CSS file.
01:40But instead of loading it no matter what,
01:42we're going to create a custom user experience
01:44that basically will only load the CSS given certain terms.
01:48In this case, we want it to load only when it's nighttime
01:51and have a nighttime version of our site.
01:54So let's jump right into it.
01:55And let's see, how do we create this really interesting version of our site
02:00for users that are at nighttime, for example?
02:03This section in general is really dedicated to JavaScript,
02:08but we're going to be using third-party libraries to manipulate.
02:12Although you could do most of these stuff with pure JavaScript,
02:15it would take a lot more steps
02:17and we're trying to create something that is light and quick
02:19so you could see the things that you could do with JavaScript.
02:22But that does not mean that you have to use a third-party tool.
02:26With that said, I'm going to go ahead and in your source files
02:29in the latest chapter in section 5,
02:31you're going to find in your CSS a night CSS.
02:35The idea behind our night CSS is I really want to create
02:38a version of our site where if the user is coming to us during the night,
02:42they'll have a site that is more accommodating to users at night
02:46versus users at day.
02:47The idea behind it is very simple.
02:49During the day, vivid colors are great.
02:51We love them.
02:52During nighttime, most of the time when it's dark out,
02:54we prefer less vivid colors.
02:57It's easier to read also.
02:58By the way, on a mobile device,
03:00we'd probably use some sort of logic to be able to fetch and see
03:03what is the current level of light in front of us
03:06and then manipulate the color to create a customization for the user.
03:10These small little tweaks and improvements
03:13are going to really make the performance of your site
03:15and the experience of users just better in general.
03:18What I'm going to do next is let's go into our source code
03:21and I'm going to start off by adding in jQuery right below our script.
03:26Notice that I'm right above our script actually.
03:29I want to make sure that jQuery is loaded
03:31before we even start approaching our script.
03:34The reason is because our script itself, once it starts running,
03:37it assumes that jQuery has been already loaded
03:40and the document has been loaded as well.
03:43To do that, I'm going to go ahead and I'm going to create a new script tag
03:47but I'm going to put inside of it the jQuery script.
03:50To find the jQuery script, I'm going to delete the current script that's there.
03:54I'm going to go into my browser and just go right into jQuery.com.
03:59When you go into jQuery.com, just go ahead and search for your download jQuery
04:04but instead of actually literally downloading it,
04:07I'm going to go ahead and select the production version,
04:10the compressed production version.
04:12I know that we're looking here at version 1.0 or 2.0.
04:17The idea is behind them very simplistically.
04:19The big difference between 1.something and 2.something
04:24is does it support really older versions of browsers such as IE6, 7, and 8
04:29while jQuery 2.0, 2.whatever will be, doesn't and won't.
04:341.0 does.
04:36If it's important for you to support those browsers,
04:39such as it is for me,
04:41then we're going to go ahead and just use the 1.1 version
04:44as we work on it currently.
04:47And I'm going to go ahead and select the compressed production version and it's going
04:50to open up right away.
04:51So I'm just going to copy that link because I really don't even need to download it.
04:55And I'm going to use their CDN.
04:57So I'm going to go ahead and in my source files, just update the script tag to have
05:01directly that jQuery version.
05:03And if you want to make sure that you have the latest, just make sure that you go on
05:06the site each time and fetch the latest version.
05:09So every time you're building a new production, you're going to work with the latest version.
05:13I'm going to go ahead and save this.
05:14And that's it.
05:15Now jQuery has been loaded up specifically also because the script itself is only going to
05:20trigger the init function once everything has loaded once we got to the bottom of the page.
05:25So I know that everything is there and loaded and I could start working and manipulating
05:30our content right away.
05:32What I want to do next is I want to make sure that when I want to basically decide when
05:37should I trigger versus version one versus version two, the regular version, which is
05:42our current CSS versus our nighttime version, which is basically going to be independent
05:46of it.
05:48This nighttime version, literally all it has is a few different changes to the colors, mainly
05:53the backgrounds to make sure that the colors are not too vivid, making sure that our background
05:58is a dark color versus a light color.
06:01All right.
06:02So that said, let's go ahead and create here a new variable, which I'm going to
06:05call H for hour.
06:07And I'm just going to go ahead and create a new date.
06:09And right away, once I've created it, I'm going to ask to get the hours.
06:14Now hours come back between zero and 23 when zero represents midnight and 23 represents
06:1911 PM.
06:21So what I want to do is I want to ask you a very simple question.
06:24Are we during hours that are expected to be nighttime hours?
06:29And for me, I'm going to assume just for the sake of our presentation that if it's
06:32before 6 AM or it's after 7 PM, then it's probably nighttime.
06:39And I'm going to go ahead here.
06:41And in this case, and only this case, I'm going to go ahead and actually append in
06:44another style sheet.
06:46To do that, I'm going to first approach and create a new style.
06:50And to do that, just like when we created CSS, we added a new link, I'm literally
06:54what I'm going to do is I could go ahead and just copy this whole link process.
06:59And if I go into my script, I could literally just go inside of here and
07:02create a new script by sending inside.
07:05And I'm just going to make sure that I'm going to wrap it in a single quote.
07:08That way I could use double quotes inside.
07:10So I'm literally just creating a new script.
07:12And I could also, if I want to just go ahead directly and assign the values.
07:16Now, our next step for us is to obviously go ahead and
07:20change the style to be the style that we want it to be,
07:23which is the nighttime style.
07:25And we're almost done and I just want to walk through this.
07:28Literally, I just went ahead and send in a HTML that was already pre-configured.
07:33And it's going to render it and turn it into a DOM element behind the scenes.
07:37Now, obviously, we could do it the long way, but we won't,
07:40because it would take a lot more steps.
07:42Now, before we were done, all that's left for us to do is actually append it into
07:45the document.
07:47And to do that, we're actually going to take this jQuery object that is going
07:50to be returned, add a dot after it, and call the appendTo method.
07:55Now, the appendTo method expects to ask to what is it going to append.
07:59You could put inside a jQuery object, you could put inside a DOM object, or
08:04you could go ahead and just put here a string that would represent what you want.
08:08And in this case, I'm going to put here the head, which represents our head.
08:11Now, that's all we really had to do.
08:13And if we set this up and go back into our application and
08:16click on refresh, you should see that nothing is happening.
08:19Well, the reason is nothing is happening, although it is working.
08:22If you go into your inspector, well, it actually is not working.
08:25You won't be able to see it because it's working, but
08:27it's not going to append it yet because the time right now that I'm recording is 4 p.m.
08:32So let me go ahead there and just push down the time to be 15.
08:35So if it's three or up, then we'll be able to see it.
08:39And I'm going to go ahead and save this.
08:41Go back and click here on refresh, and
08:42we should see now that our whole application is going to be updated.
08:46Now, these type of really small, really big in this case,
08:50tweaks that you do to your site will add a lot to the user experience.
08:54Especially if you have video on a page.
08:56If you have a video and users are watching it at night,
08:58the most annoying thing I could say as a user is a white background.
09:02It's just so frustrating.
09:03Specifically, YouTube and Google, please update that.
09:06At night, I don't want to see a white background anywhere really,
09:10because it's not that easy to consume.
09:12So in general, you could create really,
09:14really cool customizations using JavaScript that would tailor and
09:17target to your user, okay?
09:22In this lecture, we learned how to create customizations using JavaScript.
09:26And obviously, there's a big and
09:28endless amount of things that you could do with these customizations.
09:31If you feel like you need to learn more JavaScript,
09:34I urge you to check out my packed title, Mastering JavaScript.
09:39You'll find a reference inside of this lecture, inside of the HTML file.
09:44Okay, in the next lecture,
09:46we're going to continue down this path as we create a typewriter effect.
