猿问

一行12个月份,超出横行滚动,如何让当前月份显示在中间,如下图和代码

图片

当前效果(现在是2月份,所以显示2月的内容)

问题

1-6月份,头部月分选中状态没有问题
但是7-12月份是要横向滚动,才显示出来,
我想要如果当前月份是7-12月份的时候,页面一打开就显示出来,如下图

https://img3.mukewang.com/5c8f53290001015303750668.jpg

代码

<!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");
    }
    // 月份
    function monthFn(){
        var date = new Date();
        var month = date.getMonth();
        console.log(typeof month);
        // 显示对应月份的内容
        $('.item-group').eq(month).addClass('actived');
        $('.content-item').eq(month).addClass('actived')
    }
    monthFn();
</script>
MMMHUHU
浏览 378回答 1
1回答

浮云间

那你只能js判断了,当在6后面的时候,动态设置box-item的横向滚动值
随时随地看视频慕课网APP
我要回答