类似选项卡切换 翻页


<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <style type="text/css"> *{margin: 0;padding:0;list-style: none;} .warp{width:600px;margin: 10px auto;} .nav ul{width:500px;clear: both;overflow: hidden;} .nav ul li{width:100px;height:50px;background: #ccc;float: left;text-align: center;line-height: 50px;} .nav li.active{background: #000;color: #fff;} .con ul{width: 300px;height:100px;background:#000;} .con ul li{display: none;color: #fff;} .con ul li.active{display: block;} .btn button{width: 50px;height: 30px;margin:5px 30px;} </style> </head> <body> <div class="warp"> <div class="nav"> <ul> <li class="active">首页</li> <li>列表</li> <li>内容</li> </ul> </div> <div class="con"> <ul> <li class="active">首页1</li> <li>列表2</li> <li>内容3</li> </ul> </div> <div class="btn"> <button>上一页</button> <button>下一页</button> </div> </div> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('.nav li').click(function(){ $('.nav li').removeClass('active').eq($(this).index()).addClass('active'); $('.con li').removeClass('active').eq($(this).index()).addClass('active'); }) $('.btn button').click(function(){ var li=$('.nav li.active').index(); //首先获取菜单数量 var btn=$(this).index(); //按钮上一步 只有俩个 所以获取索引 Switch(li,btn); }) function Switch(li,btn){ if(btn==0){ //判断 按钮只有俩个0 或 1,所以当按钮索引等于0时直接等于-1 btn = -1; } var index = li+btn; //得到该附加样式的菜单以及对于的展示块的索引 if(index>$('.nav li').length-1){ //判断 索引大雨菜单长度归0,重新计算 index = 0; } if(index<0){ //判断 索引小于0切换到最后一个。 index=$('.nav li').length-1; } //清除样式及对应菜单和展示块附加样式 $('.nav li').removeClass('active').eq(index).addClass('active'); $('.con li').removeClass('active').eq(index).addClass('active'); } }); </script> </body> </html>

  

posted @ 2017-06-22 17:50  多幸运1号  阅读(393)  评论(0编辑  收藏  举报