jQuery static menu too much code
Hi, I just started with jQuery and I wanted to do a website with 4 menu buttons and after I click them only content div will change. So i did something but I have too many lines of code for this simple task. I hope somebody can explain me how to to thing more efficiently.
This is my index.html page
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <link type="text/css" rel="stylesheet" href="style.css">
- <title>Rainer TestPage</title>
- <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
- <script type="text/javascript" src="script.js"></script>
- </head>
- <body>
- <div id="header">
- <div id="menubar">
- <div id="home">
- Home
- </div>
- <div id="images">
- Images
- </div>
- <div id="contact">
- Contact
- </div>
- <div id="other">
- Other
- </div>
- </div>
- </div>
- <div id="content">
- <div id="homecontent">
- Home content goes here!
- </div>
- <div id="imagescontent">
- Images content goes here!
- </div>
- <div id="contactcontent">
- Contact content goes here!
- </div>
- <div id="othercontent">
- Other content goes here!
- </div>
- </div>
- <div id="footer">
-
- </div>
- </body>
- </html>
This is my style.css
- #header
- {
- position:absolute;
- height:100px;
- left:0;
- top:0;
- width:100%;
- background:gold;
- }
- #footer
- {
- position:absolute;
- height:50px;
- left:0;
- bottom:0;
- width:100%;
- background:blue;
- }
- #content
- {
- position:absolute;
- top:100px;
- bottom:50px;
- left:0;
- width:100%;
- background:red;
- }
- #menubar
- {
- margin:auto;
- width:30%;
- }
- #home,
- #images,
- #contact,
- #other
- {
- text-align:center;
- border-style:solid;
- border-width:1px;
- float:left;
- font-size:20pt;
- margin-right:1px;
- margin-top:30px;
- width:100px;
- height:50px;
- background:lightyellow;
- }
And script.js
- $(document).ready(function(){
- //Hide all content but home
- $("#imagescontent").hide();
- $("#contactcontent").hide();
- $("#othercontent").hide();
- //Home tab stuff
- $("#home").mouseenter(function(){
- $("#home").css("background","lightsalmon");
- });
- $("#home").mouseleave(function(){
- $("#home").css("background","lightyellow");
- });
- $("#home").click(function(){
- $("#homecontent").show();
- $("#imagescontent").hide();
- $("#contactcontent").hide();
- $("#othercontent").hide();
- });
- // Images tab stuff
- $("#images").mouseenter(function(){
- $("#images").css("background","lightsalmon");
- });
- $("#images").mouseleave(function(){
- $("#images").css("background","lightyellow");
- });
- $("#images").click(function(){
- $("#homecontent").hide();
- $("#imagescontent").show();
- $("#contactcontent").hide();
- $("#othercontent").hide();
- });
- // Contact tab stuff
- $("#contact").mouseenter(function(){
- $("#contact").css("background","lightsalmon");
- });
- $("#contact").mouseleave(function(){
- $("#contact").css("background","lightyellow");
- });
- $("#contact").click(function(){
- $("#homecontent").hide();
- $("#imagescontent").hide();
- $("#contactcontent").show();
- $("#othercontent").hide();
- });
- // Other tab stuff
- $("#other").mouseenter(function(){
- $("#other").css("background","lightsalmon");
- });
- $("#other").mouseleave(function(){
- $("#other").css("background","lightyellow");
- });
- $("#other").click(function(){
- $("#homecontent").hide();
- $("#imagescontent").hide();
- $("#contactcontent").hide();
- $("#othercontent").show();
- });
- });
And also I have a problem with centering the button text in the middle. Right now it looks like it is vertically top bot how can I put it in the middle.
Sorry about my english, it is my second language.