zoukankan      html  css  js  c++  java
  • 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>

    终极效果图

  • 相关阅读:
    python字符串与字典转换
    B站弹幕爬取 / jieba分词
    【计算机网络】第四章 网络层(1)
    【计算机网络】第三章 传输层(4)
    【计算机网络】第三章 传输层(3)
    【计算机网络】第三章 传输层(2)
    【计算机网络】第三章 传输层(1)
    【计算机网络】第二章 网络应用(5)
    【计算机网络】第二章 网络应用(4)
    【计算机网络】第二章 网络应用(3)
  • 原文地址:https://www.cnblogs.com/li-sir/p/9675190.html
Copyright © 2011-2022 走看看