<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body{ padding: 0; margin: 0; } .box{ width: 100%; height: 50px; background-color: #00aaee; } .box .box-item{ display: flex; flex-wrap: nowrap; align-items: center; width: 100%; height: 50px; overflow-x: scroll; overflow-y: hidden; white-space: nowrap; background-color: #ff0000; } .box .box-item .item-group{ display: block; flex: 0 0 16.667%; width: 16.667%; height: 50px; line-height: 50px; text-align: center; box-sizing: border-box; border-right: 1px solid #ccc; } .box .box-item .item-group.actived{ color: #fff; } .content .content-item{ display: none; } .content .content-item.actived{ display: block; height: 200px; background-color: yellow; } ::-webkit-scrollbar { display: none } </style> </head> <body> <div class="box"> <div class="box-item"> <div onclick="tabHeader(0)" class="item-group">1月</div> <div onclick="tabHeader(1)" class="item-group">2月</div> <div onclick="tabHeader(2)" class="item-group">3月</div> <div onclick="tabHeader(3)" class="item-group">4月</div> <div onclick="tabHeader(4)" class="item-group">5月</div> <div onclick="tabHeader(5)" class="item-group">6月</div> <div onclick="tabHeader(6)" class="item-group">7月</div> <div onclick="tabHeader(7)" class="item-group">8月</div> <div onclick="tabHeader(8)" class="item-group">9月</div> <div onclick="tabHeader(9)" class="item-group">10月</div> <div onclick="tabHeader(10)" class="item-group">11月</div> <div onclick="tabHeader(11)" class="item-group">12月</div> </div> </div> <div class="content"> <div class="content-item">11</div> <div class="content-item">22</div> <div class="content-item">33</div> <div class="content-item">44</div> <div class="content-item">55</div> <div class="content-item">66</div> <div class="content-item">77</div> <div class="content-item">88</div> <div class="content-item">99</div> <div class="content-item">10</div> <div class="content-item">11</div> <div class="content-item">12</div> </div> </body> </html> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script> // 點擊觸發 function tabHeader(index){ activeSwitch(index) } // 頭部變色 // 底部顯示對應內容 function activeSwitch(index){ $('.item-group').eq(index).addClass('actived').siblings().removeClass("actived"); $('.content-item').eq(index).addClass('actived').siblings().removeClass("actived"); } // 月份 monthFn(); function monthFn(){ var date = new Date(); var month = date.getMonth() + 1; var year = date.getFullYear(); if(month <10){ month = Number("0" + month); } var monthtimt = year + "-" + month; // 顯示對應月份的內容 // 若是月份大於6月份,則顯示6-12月 if(month >5){ $('.box-item').scrollLeft( $(window).width()); } $('.item-group').eq(month-1).addClass('actived'); $('.content-item').eq(month-1).addClass('actived'); } </script>