模拟垂直滚动条

1.滚动条的高度 = 容器的高度/内容的高度*容器的高度
2.内容移动的距离=(内容的高度-容器的高度) / (容器的高度-滚动条的高度) * 滚动条移动的距离

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 300px;
            height: 500px;
            border: 1px solid red;
            margin: 100px;
            position: relative;
            overflow: hidden;
        }
        .content{
            height: auto;
            padding-right: 20px;
            position: absolute;
            top: 0;
            left: 0;
            padding: 5px 20px 5px 5px;
        }
        .scroll{
            width: 20px;
            height: 100%;
            background-color: #eee;
            position: absolute;
            right: 0;
            top: 0;
        }
        .bar{
            width:  100%;
            height: 100px;
            background-color: red;
            cursor: pointer;
            border-radius:10px;
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="box" id="box">
        <div class="content">
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
            这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容
        </div>
        <div class="scroll">
            <div class="bar"></div>
        </div>
    </div>
</body>
</html>
<script>
    var box = document.getElementById("box");
    var content = box.children[0];
    var scroll = box.children[1];
    var bar = scroll.children[0];
    //滚动条的高度 = 容器的高度/内容的高度*容器的高度
    if(content.offsetHeight<=box.offsetHeight){
        bar.style.height = 0;
    }else{
        bar.style.height = box.offsetHeight*scroll.offsetHeight/content.offsetHeight +"px";
    }
    //拖动滚动条
    bar.onmousedown = function (event) {
        var event = event || window.event;
        //var val = event.clientY - scroll.offsetHeight;
        var val = event.clientY - this.offsetTop;
        document.onmousemove = function (event) {
            var event = event || window.event;
            var barTop = event.clientY - val;
            var conTop = (content.offsetHeight-box.offsetHeight)/(box.offsetHeight-bar.offsetHeight)*barTop;
            if(barTop<0){
                barTop = 0;
            }else if(barTop > (box.offsetHeight - bar.offsetHeight)){
                barTop = box.offsetHeight - bar.offsetHeight;
            }else{
                content.style.top = -conTop + "px";
            }
            bar.style.top = barTop +"px";
        }
    }
    document.onmouseup  = function () {
        window.getSelection() ? window.getSelection().removeAllRanges() : document.selection.empty();
        document.onmousemove = null;
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值