We are going to use jQuery to create a menu that slides out from the left side.
- The file index.html contains the page structure and content.
- The file style.css has the styling for the page.
- The file app.js has JavaScript and jQuery code. This code makes the menu interactive.
- Click on the menu icon. The menu displays by pushing the page's body over to the right.
- Then click on the X icon. The menu closes by pulling the body back to where it was.
- Click Save & Submit Code to get started!
The index.html file has two scriptelements right before the closing </body>
tag:
- The first script loads jQuery.
- The second script loads app.js. The fileapp.js is where we'll write out code for the Uber menu.
Let's start by setting up the basic skeleton for JavaScript programs.
- Inside the app.js file, use the keyword
var
and create a function calledmain
. - Leave the function's code block empty.
- Use jQuery to run the
main
function once the web page has fully loaded. - See the hint if you're stuck!
/*Inside the app.js file, use the keyword var and create a function called main.*/ var main = function(){ }; /*Use jQuery to run the main function once the web page has fully loaded.*/ $(document).ready(main);
What does this skeleton do?
- The
main
function is where we'll write our program. - The
$(document).ready
runs themain
function as soon as the HTML document has loaded in the browser.
Let's start filling out the main
function. Here, we'll select an HTML element in order to operate on it. jQuery lets us select HTML elements using CSS selectors.
- In app.js, find the
main
function. - Inside the
main
function, use jQuery to select the class'icon-menu'
. $('.icon-menu'); - Small hint: jQuery is all about the dollars! And to select a class, use a period (ie. full stop).
- Start with a
$( )
to tell the computer we're using jQuery. - To use CSS to select the class
'icon-menu'
, we type in'.icon-menu'
.
We have just used jQuery to select the'icon-menu'
class using $('.icon-menu')
Now we want to respond to user action.
Specifically we want to respond to a user clicking on this '.icon-menu'
HTML element.
After $('.icon-menu')
, add the .click()
method.
We now have code that says something will happen when the user clicks on theicon-menu
HTML element. But what will happen?!
Clicking on the menu icon should open the menu.
- Add a function inside the
.click()
method. - Select the
'menu'
class and animate it. Right now, the menu is 285px past the left edge of the screen. That's why it is not visible! Inside the animate method:
a move it 0px to the left
b. make this happen over 200 milliseconds.We now want to push the rest of the page to the left 285px. Use jQuery to select the body HTML element, animate it, and move it left 285px over 200 milliseconds.
Once the menu is open, we need a way to close it. Let's adapt the three steps we used to open the menu in order to close the menu.
- Use jQuery to select the
'.icon-close'
element. - Add the
.click()
method. - When the
'.icon-close'
element is clicked, use.animate()
to change theleft
offset of the menu to -285px, and theleft
offset of the body to 0px. Both are done over 200 milliseconds. - Click on the menu icon and the X to see if your code works!
'Frontend' 카테고리의 다른 글
make an Interactive Website 4 Event (0) | 2014.08.22 |
---|---|
make an Interactive Website 3 JavaScript (0) | 2014.08.22 |
make an Interactive Website 1 (0) | 2014.08.21 |
make website 3 coding (0) | 2014.08.21 |
make web site 2 bootstrap (0) | 2014.08.21 |