鍍金池/ 問答/Java  PHP  Python  C  HTML/ 一行12個月份,超出橫行滾動,如何讓當(dāng)前月份顯示在中間,如下圖和代碼

一行12個月份,超出橫行滾動,如何讓當(dāng)前月份顯示在中間,如下圖和代碼

圖片

當(dāng)前效果(現(xiàn)在是2月份,所以顯示2月的內(nèi)容)
clipboard.png

問題

1-6月份,頭部月分選中狀態(tài)沒有問題
但是7-12月份是要橫向滾動,才顯示出來,
我想要如果當(dāng)前月份是7-12月份的時候,頁面一打開就顯示出來,如下圖

clipboard.png

代碼

<!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>
    // 點擊觸發(fā)
    function tabHeader(index){
        activeSwitch(index)
    }
    // 頭部變色
    // 底部顯示對應(yīng)內(nèi)容
    function activeSwitch(index){
        $('.item-group').eq(index).addClass('actived').siblings().removeClass("actived");
        $('.content-item').eq(index).addClass('actived').siblings().removeClass("actived");
    }
    // 月份
    function monthFn(){
        var date = new Date();
        var month = date.getMonth();
        console.log(typeof month);
        // 顯示對應(yīng)月份的內(nèi)容
        $('.item-group').eq(month).addClass('actived');
        $('.content-item').eq(month).addClass('actived')
    }
    monthFn();
</script>
回答
編輯回答
避風(fēng)港

那你只能js判斷了,當(dāng)在6后面的時候,動態(tài)設(shè)置box-item的橫向滾動值

2018年9月12日 10:25