横向滑动

<!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>
    原文作者:渣渣辉
    原文地址: https://segmentfault.com/a/1190000013323888
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞