By the end of today, you will be able to:
The JS Console is very important:
console.log
messages are printed
In general, you should always have the JS console open when you are coding JS.
You owe it to yourself to learn the keyboard shortcut for opening the JS console. I do it a zillion times a day, and if I can save myself 5 seconds each time...
Command-option-j on the Macs will do the trick. Do that now, please.
Look at each of the following expressions and guess their value. Write down your predictions. (This is important, so that you will notice if you are wrong.) Then, type or copy/paste the expressions into the JS console and see if you were right.
3 < 11; "3" < "11"; 3 < "11";
Hunh? What does this all mean?
The take-home message is that if you are treating things as numbers, make sure they are numbers, not strings.
We'll return to the last two exercises from last time (the time-based ones).
We saw this in the reading:
if (grade >= 90) { letterGrade = "A"; } else if (grade >= 80) { letterGrade = "B"; } else if (grade >= 70) { letterGrade = "C"; } else if (grade >= 60) { letterGrade = "D"; } else { letterGrade = "F"; }
We all know that a B is a grade between 80 and 90 (at least in this example), but the code above does not check that the grade is less than 90! Is that okay?
It's okay, the letterGrade
variable gets overwritten by the correct value
It's okay, the test comes after the test for A
It's okay, the test comes in the else
Oops, that's a bug in the code!
Suppose we want to categorize a screen as "narrow", "medium" or "wide", storing the result in a variable called "screenKind". Imagine we do it this way:
Your code should prompt the user for the screen width. (But, you
should know that there is a built-in screen.availWidth
global variable that contains this info.)
You might come up with something like this:
var width = parseInt(prompt("Enter screen width in pixels")); if( width >= 960 ) { screenKind = "wide"; } else if( width > 480 ) { screenKind = "medium"; } else { screenKind = "narrow"; }
Is 480 narrow or medium?
If you use if-else
, you don't have to test for the
negation of something.
Don't test for things that you know are true. If you know that a coin flip is not heads, you don't have to test that it's tails.
Even more generally, keep your code as simple as possible. If you're given someone's age, you don't have to test that it's a positive number. Professional code is littered with "sanity checks" like that, but even the pros find that annoying. For us, let's focus on the important conditions.
Let's refresh some CSS knowledge.
You're given the following CSS rule:
div p { width: 100%; max-width: 500px; }
When will the elements p
within the div
have
exactly the width 500px?
How can you read the following CSS rule (called a media query) in English:
@media handheld and (max-width: 600px) { /* ... */ }
You have an opportunity to take a web page with a fixed-width design and convert it to responsive design that changes appearance based on the width of the device. Download this folder that contains the HTML file and three images. Unzip it on your Desktop. Or, get the files one at a time from the directory
Change the CSS for the element wrapper
to move from
fixed-width to fluid width. (Note the margin property with 3 values:
top left-and-right bottom.)
#wrapper { width: 960px; margin: -10px auto 0; background-color: #FFEFDF; }
Original Version
#wrapper { max-width: 960px; width: 100%; margin: -10px auto 0; background-color: #FFEFDF; }
Modified Version
What does it mean for the wrapper
to have a width of 100%?
Resize the page after this change. What do you see?
The banner image is not changing because the image width is also 960px. When the page becomes smaller than the image, a horizontal scrollbar is added. We can force the banner image to change its width with the page by creating a rule to set it to 100%. Do that now and test the result.
After the first two steps, the page text flows with the page and the banner image shrinks too when we resize. The biggest problem for the moment is the navbar. Let's look at its code:
nav ul li { list-style-type: none; display: inline-block; text-align: center; width: 137px; padding: 20px 0; }
Every value in pixels for the width property is a problem for the fluid design, we need to convert it to a % value. How do we do that?
The value 137px doesn't seem random. Why is every list item of the navbar set to 137px? What is the corresponding in %? Make that change into the code and test the page.
The menu items are showing in a horizontal line because of the property display: inline-block;
.
If you were to delete the word "inline", you'd see them stacking vertically. However, this is not a behavior
you want from the beginning. We want this only when the screen is small. In this case we will need a media
query rule.
@media screen and (max-width: 680px){ nav ul li { display: block; width: 100%; /* let's fill all available space*/ padding: 1% 0; /* 1% of width of element*/ border-bottom: 1px solid white; } }
Add this rule to the page and resize it.
We're almost done. If you resize to a small width, you'll see the images sticking out left and right. This can also be fixed with the following media query:
@media screen and (max-width: 480px) { #text img {width: 100%}; }
We hope that after these activities you have a good understanding of:
The foxes exercise and the solution to the foxes exercise. Compare the CSS.
Will be posted later, visit again after .
Quiz answers:
1. C we don't have to test for the negation because we used ELSE 2. C it can be smaller than 500 if the parent is smaller than 500 3. D "max" means "it can't be more than, but it can be less"
Activity solutions.
Exercise on Page Width:
The foxes exercise and the solution to the foxes exercise. Compare the CSS.