购物车加减计数按钮效果

  input:number样式修改

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车计数</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            outline: none;
            box-sizing: border-box;
            text-align: center;
        }

        section {
            width: 128px;
            display: flex;
            margin: 50px auto;
        }

        span {
            display: inline-block;
            width: 35px;
            height: 30px;
            background-color: #fff;
            cursor: pointer;
            vertical-align: middle;
            border: 1px solid #999999;
            font-size: 16px;
            font-weight: bolder;
            line-height: 28px;
        }

        input {
            width: 58px;
            height: 30px;
            padding: 1px 0;
            border: 1px solid #999999;
            border-left: none;
            border-right: none;
        }

        /*  取消右侧加减按钮解决方法 */
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {
            -webkit-appearance: none;
        }
    </style>
</head>

<body>
    <section>
        <span onclick="opera('val', false);">-</span>
        <input type="number" id="val" value="1">
        <span onclick="opera('val', true);">+</span>
    </section>
</body>
<script>
    //  input样式修改及绑定左右按钮
    function opera(x, y) {
        var rs = new Number(document.getElementById(x).value);

        if (y) {
            document.getElementById(x).value = rs + 1;
        }
        else if (rs > 0) {
            document.getElementById(x).value = rs - 1;
        }
    }
</script>

</html>

 不用input直接写

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车计数</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            vertical-align: middle;
            box-sizing: border-box;
        }

        .box {
            width: 130px;
            margin: 50px auto;
            display: flex;
        }

        button {
            width: 35px;
            height: 30px;
            border: 1px solid #e4e4e4;
            cursor: pointer;
        }

        span {
            display: inline-block;
            width: 60px;
            height: 30px;
            text-align: center;
            line-height: 30px;
            border: 1px solid #e4e4e4;
        }
    </style>
</head>

<body>
    <div class="box">
        <button class="suntract">-</button>
        <!-- <input type="number" id="btn"> -->
        <span class="num">0</span>
        <button class="add">+</button>
    </div>
</body>
<script>
    // 获取元素
    var suntract = document.querySelector(".suntract");
    var add = document.querySelector(".add");
    var num = document.querySelector(".box .num");


    var x = 0;

    // 给定事件
    suntract.onclick = function () {
        // 每次点击减一
        x--;
        if (x < 0) {
            num.innerHTML = "0";
            x = 0;
        }
        else {
            num.innerHTML = x;
        }
    }
    add.onclick = function () {
        x++;
        num.innerHTML = x;

    }
</script>

</html>

posted @ 2022-05-12 08:56  卟怪  阅读(64)  评论(0编辑  收藏  举报