js模拟滚动条滚动

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
<meta name="keywords" content="">
<meta name="description" content="">
<style>
    *{margin:0; padding:0; list-style:none;}
    body{
        height:2000px;
    }
    .box1{
        width: 320px;
        height: 400px;
        background: #ccc;
        overflow: hidden;
        overflow-y: scroll;
        margin: 20px 0 0 100px;
    }
    .con1{
        font-size: 18px;
    }
    #out{
        width: 320px;
        height: 400px;
        background: url(bg1.png) 0 0 repeat-x;
        position: absolute;
        left: 500px;
        top: 20px;
        overflow: hidden;
    }
    #con{
        width: 280px;
        padding: 5px;
        font-size: 18px;
        position: absolute;
        left: 0px;
        top: 0px;
    }
    #box{
        width: 30px;
        height: 400px;
        position: absolute;
        right: 0;
        top: 0;
    }
    #drag{
        width: 30px;
        height: 53px;
        background: url(icon2.png) 0 0 no-repeat;
        position: absolute;
        left: 0px;
        top: 0px;
    }
</style>
</head>
<body>
<div class="box1">
    <p class="con1">模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条</p>
</div>

<div id="out">
    <div id="con">
        模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条模拟滚动条
    </div>
    <div id="box">
        <p id="drag"></p>
    </div>
</div>
<script>
    var out=document.getElementById('out');
    var con=document.getElementById('con');
    var box=document.getElementById('box');
    var drag=document.getElementById('drag');
    drag.onmousedown=function (ev){
        var e=ev||window.event;
        if (e.preventDefault) {
            e.preventDefault();
        } else{
            e.returnValue=false;
        };
        var d_bkt=e.clientY-drag.offsetTop;
        document.onmousemove=function (ev){
            var e=ev||window.event;
            var top=e.clientY-d_bkt;
            if (top<=0) {
                top=0;
            };
            if (top>=box.clientHeight-drag.clientHeight) {
                top=box.clientHeight-drag.clientHeight;
            };
            var scale=top/(box.clientHeight-drag.clientHeight);
            var cony=scale*(con.clientHeight-out.clientHeight);
            drag.style.top=top+'px';
            con.style.top=-cony+'px';
            console.log(top);
        }
        document.onmouseup=function (){
            document.onmousemove=null;
        }
    }
    var str=window.navigator.userAgent.toLowerCase();
    if (str.indexOf('firefox')!=-1) {//火狐浏览器
        out.addEventListener('DOMMouseScroll',function (e){
            e.preventDefault();//阻止窗口默认的滚动事件
        if (e.detail<0) {
            var t=con.offsetTop+20;
            if (t>=0) {
                t=0;
            };
            if (t<=-(con.clientHeight-out.clientHeight)) {
                t=-(con.clientHeight-out.clientHeight);
            };
            var scale=t/(con.clientHeight-out.clientHeight);
            var top=scale*(box.clientHeight-drag.clientHeight);
            con.style.top=t+'px';
            drag.style.top=-top+'px';
        };
        if (e.detail>0) {
            var t=con.offsetTop-20;
            if (t>=0) {
                t=0;
            };
            if (t<=-(con.clientHeight-out.clientHeight)) {
                t=-(con.clientHeight-out.clientHeight);
            };
            var scale=t/(con.clientHeight-out.clientHeight);
            var top=scale*(box.clientHeight-drag.clientHeight);
            con.style.top=t+'px';
            drag.style.top=-top+'px';
        };
        },false);
    } else{//非火狐浏览器
        out.onmousewheel=function (ev){
            var e=ev||window.event;
            if (e.preventDefault) {
                e.preventDefault();
            } else{
                e.returnValue=false;
            };
            if (e.wheelDelta>0) {
            var t=con.offsetTop+20;
            if (t>=0) {
                t=0;
            };
            if (t<=-(con.clientHeight-out.clientHeight)) {
                t=-(con.clientHeight-out.clientHeight);
            };
            var scale=t/(con.clientHeight-out.clientHeight);
            var top=scale*(box.clientHeight-drag.clientHeight);
            con.style.top=t+'px';
            drag.style.top=-top+'px';
        };
        if (e.wheelDelta<0) {
            var t=con.offsetTop-20;
            if (t>=0) {
                t=0;
            };
            if (t<=-(con.clientHeight-out.clientHeight)) {
                t=-(con.clientHeight-out.clientHeight);
            };
            var scale=t/(con.clientHeight-out.clientHeight);
            var top=scale*(box.clientHeight-drag.clientHeight);
            con.style.top=t+'px';
            drag.style.top=-top+'px';
        };
        }
    };
</script>
</body>
</html>

终极效果图

 

转载于:https://www.cnblogs.com/li-sir/p/9675190.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值