javascript - Display div's on Menu item click -
i working on site has 1 page, instead of loading different pages different bits of content; want content change not page therefore decreasing loading , creating nice effect.
as can see code below it's long works, sure there easier way , animating between divs.
html
<ul class="side-nav" id="sidenav"> <li id="homenav"><a href="#">home</a></li> <li id="bionav"><a href="#">biography</a></li> <li id="musicnav"><a href="#">music</a></li> <li id="photosnav"><a href="#">photos</a></li> <li id="shopnav"><a href="#">shop</a></li> </ul> <div id="homepage"> <p>home page</p> </div> <div id="biogpage"> <p>biog page</p> </div> <div id="musicpage"> <p>music page</p> </div> <div id="photospage"> <p>photos page</p> </div> <div id="shoppage"> <p>shop page</p> </div>
js
$('#biogpage').hide(); $('#musicpage').hide(); $('#photospage').hide(); $('#shoppage').hide(); $('#homenav').click(function () { $('#homepage').show(); $('#biogpage').hide(); $('#musicpage').hide(); $('#photospage').hide(); $('#shoppage').hide(); }); $('#bionav').click(function () { $('#homepage').hide(); $('#biogpage').show(); $('#musicpage').hide(); $('#photospage').hide(); $('#shoppage').hide(); }); $('#musicnav').click(function () { $('#homepage').hide(); $('#biogpage').hide(); $('#musicpage').show(); $('#photospage').hide(); $('#shoppage').hide(); }); $('#photosnav').click(function () { $('#homepage').hide(); $('#biogpage').hide(); $('#musicpage').hide(); $('#photospage').show(); $('#shoppage').hide(); }); $('#shopnav').click(function () { $('#homepage').hide(); $('#biogpage').hide(); $('#musicpage').hide(); $('#photospage').hide(); $('#shoppage').show(); });
try wrap of divs inside parent div:
<div id="wrapper"> <div id="homepage"><p>home page</p></div> <div id="biogpage"><p>biog page</p></div> <div id="musicpage"><p>music page</p></div> <div id="photospage"><p>photos page</p></div> <div id="shoppage"><p>shop page</p></div> </div>
then can take advantage of index of list show according div element:
$(document).ready(function(){ $('#biogpage, #musicpage, #photospage, #shoppage').hide(); $(".side-nav li").each(function(i) { $(this).click(function() { $("#wrapper").find("div:eq('" + + "')").show().siblings().hide(); }); }); });
Comments
Post a Comment