HTML 滑块图标

效果截图:

Html页:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滑块图标</title>
    <link href="SliderIconStyle.css" rel="stylesheet" />
</head>
<body>
    <div class="SliderIcon">
        <p>当前位置0%</p>
        <div class="bar">
            <div class="barContent" id="barContent">
                <div class="box">&#x27F7;</div>
            </div>
        </div>
    </div>
   <script src="SliderIconScript.js"></script>
</body>
</html>

css样式:

.SliderIcon {
            width: 250px;
            height: 80px;
            margin: 100px auto;
            position: relative;
        }

        .bar {
            width: 250px;
            height: 5px;
            border-radius: 10px;
            background: #aaa;
            background-size: 30% 0%;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            cursor: pointer;
        }
        .barContent {
            height: 5px;
            border-radius: 10px;
            background: #1199c6;
            position: absolute;
            cursor: pointer;
        }
        .box {
            width: 22px;
            height: 22px;
            background: #1199c6;
            position: absolute;
            bottom: 0;
            top: 0;
            margin: auto 0;
            border-radius: 50%;
            cursor: pointer;
            transition: left 0.1s linear 0s;
            text-align: center;
            font-size: 14px;
            color: #ffffff;
            font-weight:bold;
        }

JS逻辑:

var box = document.getElementsByClassName('box')[0]
        var bar = document.getElementsByClassName('bar')[0]
        var SliderIcon = document.getElementsByClassName('SliderIcon')[0]
        var p = document.getElementsByTagName('p')[0]
        var cha = bar.offsetWidth - box.offsetWidth
        var barContent = document.getElementById("barContent");
        box.onmousedown = function (ev) {
            let boxL = box.offsetLeft
            let e = ev || window.event //兼容性
            let mouseX = e.clientX //鼠标按下的位置
            window.onmousemove = function (ev) {
                let e = ev || window.event
                let moveL = e.clientX - mouseX //鼠标移动的距离
                let newL = boxL + moveL //left值
                // 判断最大值和最小值
                if (newL < 0) {
                    newL = 0
                }
                if (newL >= cha) {
                    newL = cha
                }
                // 计算比例
                let bili = newL / cha * 100;
                // 改变left值
                box.style.left = barContent.style.width = newL + 'px';
                p.innerHTML = '当前位置' + Math.ceil(bili) + '%'
                return false //取消默认事件
            }
            window.onmouseup = function () {
                window.onmousemove = false //解绑移动事件
                return false
            }
            return false
        };
        // 点击音量条
        bar.onclick = function (ev) {
            let left = ev.clientX - SliderIcon.offsetLeft - box.offsetWidth / 2
            if (left < 0) {
                left = 0
            }
            if (left >= cha) {
                left = cha
            }
            let bili = left / cha * 100;
            box.style.left = barContent.style.width = left + 'px';
            p.innerHTML = '当前位置' + Math.ceil(bili) + '%'
            return false
        }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值