标签切换选中样式

实现标签切换选中背景凸出来的效果,先上效果图:

页面代码:

<div style="width: 30%; height: 70px; position: absolute; top: 0; right: 0;">
            <div id="btn_All" class="main-element main-element-selected" style="cursor: pointer; width: calc(25% + 6px); height: 100%; float: left; text-align: center; display: inline-block; position: relative;" >
                <img class="state-img" src="/Images/New/Main/icon1.png" />
                <div style="width: 100%; height:35%; font-size: 15px; margin-top: 4%;">全部</div>
                <div id="div_All" style="width: 100%; height:65%; font-size: 22px;">0</div>
            </div>
            <div id="btn_Normal" class="main-element" style="cursor: pointer; width: calc(25% + 6px); height: 100%; float: left; text-align: center; display: inline-block; position: relative;" >
                <img class="state-img" src="/Images/New/Main/icon2-green.png" />
                <div style="width: 100%; height:35%; font-size: 15px; margin-top: 4%;">正常</div>
                <div id="div_Normal" style="width: 100%; height:65%; font-size: 22px;">0</div>
            </div>
            <div id="btn_Fault" class="main-element" style="cursor: pointer; width: calc(25% + 6px); height: 100%; float: left; text-align: center; display: inline-block; position: relative;" >
                <img class="state-img" src="/Images/New/Main/icon3-red.png" />
                <div style="width: 100%; height:35%; font-size: 15px; margin-top: 4%;">故障</div>
                <div id="div_Fault" style="width: 100%; height:65%; font-size: 22px;">0</div>
            </div>
            <div id="btn_Scrap" class="main-element" style="cursor: pointer; width: calc(25% + 6px); height: 100%; float: left; text-align: center; display: inline-block; position: relative;" >
                <img class="state-img" src="/Images/New/Main/icon4-yellow.png" />
                <div style="width: 100%; height:35%; font-size: 15px; margin-top: 4%;">报废</div>
                <div id="div_Scrap" style="width: 100%; height:65%; font-size: 22px;">0</div>
            </div>
        </div>

css代码:

/* 未选中样式 */
        .main-element {
            background: url(/Images/New/Main/state2.png) no-repeat 0 0;
            background-size: 100% 80%;
            margin: -1px -1px;
            color: #2997c1;
        }

        /* 选中样式 */
        .main-element-selected {
            background: url(/Images/New/Main/state2-check.png) no-repeat center;
            background-size: 100% 100%;
            z-index: 999;
            margin: -1px -26px;  /* 设置外边距负值,实现覆盖两边元素的效果 */
            width: calc(25% + 40px) !important;
            color: #03fffc;
            line-height: 1.8;
        }

        /* 最后一个元素选中样式,背景图不一样 */
        .main-element-selected-last {
            background: url(/Images/New/Main/state2-check-last.png) no-repeat center;
            background-size: 100% 100%;
            z-index: 999;
            margin: -1px -26px;
            width: calc(25% + 16px) !important;  /* 根据靠边的效果调整宽度 */
            color: #03fffc;
            line-height: 1.8;
        }

js代码:

// 按钮点击事件
$("#btn_All").click(function () {
        $("#btn_All").attr("class", "main-element main-element-selected");
        $("#btn_Normal").attr("class", "main-element");
        $("#btn_Fault").attr("class", "main-element");
        $("#btn_Scrap").attr("class", "main-element");
    });

    $("#btn_Normal").click(function () {
        $("#btn_All").attr("class", "main-element");
        $("#btn_Normal").attr("class", "main-element main-element-selected");
        $("#btn_Fault").attr("class", "main-element");
        $("#btn_Scrap").attr("class", "main-element");
    });

    $("#btn_Fault").click(function () {
        $("#btn_All").attr("class", "main-element");
        $("#btn_Normal").attr("class", "main-element");
        $("#btn_Fault").attr("class", "main-element main-element-selected");
        $("#btn_Scrap").attr("class", "main-element");
    });

    $("#btn_Scrap").click(function () {
        $("#btn_All").attr("class", "main-element");
        $("#btn_Normal").attr("class", "main-element");
        $("#btn_Fault").attr("class", "main-element");
        // $("#btn_Scrap").attr("class", "main-element main-element-selected");
        $("#btn_Scrap").attr("class", "main-element main-element-selected-last");
    });

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值