原生js用MVVM的思想实现简易滑动条

本文介绍了如何使用MVVM模式创建可复用的滑动条和进度条组件,通过实例展示了如何绑定数据并动态更新UI。通过DOM操作和定时器实现滑动效果,并演示了如何在JavaScript中结合Knockout.js进行组件交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上一节进度条的demo,这节实现的是滑动条
请添加图片描述

div {
            margin-bottom: 10px;
            height: 40px;
        }

        .third,
        .first,
        .second {
            background: green;
            margin-top: 30px;
            position: relative;
        }

        .first b,
        .second b {
            position: absolute;
            top: -20px;
        }

        .ui-progressbar {
            background: red;
            height: inherit;
            color: #fff;
            line-height: 40px;
            text-align: center;

        }

        .progressText {
            position: absolute;
            top: -20px;
        }

        .progress {
            background: gray;
        }

        .bar {
            display: inline-block;
            width: 20px;
            height: 40px;
            background: red;
            position: absolute;
            top: 0;
            cursor: pointer;
        }
    <div class="first" data-bind="type:'slider',data:demo2">1</div>
    <div class="second" data-bind="type:'slider',data:demo2"></div>
    <div class="third" data-bind="type:'progressBar',data:demo1"></div>
    <div class="third" data-bind="type:'progressBar',data:demo3"></div>
~(function () {
            //(0, eval)('this')其实这里的this就是指向全局对象
            var window = this || (0, eval)('this')
            var FONTSIZE = function () {
                return parseInt(document.body.currentStyle ? document.body.currentStyle['fontSize'] : getComputedStyle(document.body, false)['fontSize'])
            }()
            var VM = function () {
                var Method = {
                    progressBar(dom, data) {
                        var progress = document.createElement('div')
                        let timer = null
                        params = data.data
                        progress.style.width = (params.position || 100) + '%'
                        if (params.flag) {
                            timer = setInterval(() => {
                                console.log(params.position, 'params.position');
                                if (params.position == 100) {
                                    alert("加载完毕")
                                    clearInterval(timer)
                                } else {

                                    params.position += 1
                                    progress.style.width = params.position + '%'
                                    progress.innerHTML = params.position + '%'
                                }

                            }, 100)
                        }
                        progress.className = 'ui-progressbar'
                        dom.appendChild(progress)
                    },
                    slider(dom, data) {
                        //小拨片
                        var bar = document.createElement('span'),
                            progress = document.createElement('div'),
                            totalText = null,
                            progressText = null,
                            param = data.data,
                            width = dom.clientWidth,
                            left = dom.offsetLeft,
                            realWidth = (param.position || 100) * width / 100;
                        dom.innerHTML = ''
                        if (param.totle) {
                            //容器总量的提示文案
                            text = document.createElement('b')
                            //拨片位置提示文案
                            progressText = document.createElement('em')
                            progressText.className = 'progressText'
                            text.innerHTML = param.totle
                            // dom.appendChild(text)
                            dom.appendChild(progressText)

                        }
                        bar.className = "bar"
                        setStyle(realWidth)
                        dom.className += ' ui-slider'
                        progress.className = 'progress'
                        dom.appendChild(progress)
                        dom.appendChild(bar)
                        bar.onmousedown = function () {
                            document.onmousemove = function (e) {
                                var e = e || window.event;
                                var w = e.clientX - left;
                                setStyle(w < width ? (w > 0 ? w : 0) : width)
                            }
                            document.onmouseup = function () {
                                // bar.onmousedown = null
                                document.onmousemove = null
                            }
                            document.onselectstart = function () {
                                return false
                            }
                        }
                        function setStyle(w) {
                            progress.style.width = w + 'px'
                            bar.style.left = w - FONTSIZE / 2 + 'px'
                            if (progressText) {
                                progressText.style.left = w - FONTSIZE / 2 * 2.4 + 'px'
                                progressText.innerHTML = parseFloat(w / width * 100).toFixed(2) + '%'
                            }
                        }
                    }
                }
                // debugger
                function getBindData(dom) {
                    var data = dom.getAttribute('data-bind')
                    console.log(data, '9999')
                    return !!data && (new Function("return ({" + data + "})"))()
                    // return !!data && { data }
                }
                return function () {
                    var doms = document.body.getElementsByTagName("*"), ctx = null
                    for (var i = 0; i < doms.length; i++) {
                        ctx = getBindData(doms[i])
                        console.log(ctx, '00');
                        ctx.type && Method[ctx.type] && Method[ctx.type](doms[i], ctx)
                    }
                }
            }()
            window.VM = VM

        })()
        var demo1 = {
            position: 20,
            total: 200,

        }
        var demo3 = {
            position: 10,
            total: 200,
            flag: true
        }

        var demo2 = {
            position: 20,
            totle: 200
        }
        window.onload = function () {
            VM()
        }

这样就实现了mvvm的实现创建组件的方法~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风清云淡_A

觉得有帮助的话可以给点鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值