<!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>
终极效果图