A way of realizing vertical scroll bar with JavaScript

Irving the procedural ape 2020-11-10 17:25:25
way realizing vertical scroll javascript


Design objectives : I want to copy the function of the browser's original vertical scroll bar , And can do some personalized configuration

The test page :

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <script src="MyScrolly.js"></script> 7 </head> 8 <body> 9 <div id="div_allbase" style="width: 800px;height: 600px;background-color: beige;overflow-y: hidden">10 <div id="div_outer" style="width: 700px;height: 500px;background-color: cornflowerblue;overflow-y: auto">11 <div id="div_inner1" style="width: 600px;height: 300px;background-color: darkseagreen">12 11111111111111111111111111111111111111111111111111111111111111113 </div>14 <div id="div_inner2" style="width: 600px;height: 300px;margin-top: 50px;background-color: darkseagreen"">15 22222222222222222222222222222222222222222222222222222222222222216 </div>17 <div id="div_inner3" style="width: 600px;height: 300px;margin-top: 50px;background-color: darkseagreen"">18 33333333333333333333333333333333333333333333333333333333333333319 </div>20 </div>21 </div>22 </body>23 <script>24 var myScroll=new MyScrolly(document.getElementById("div_outer")// The element to add the scroll bar 25 ,{parentelem:document.getElementById("div_allbase")})// Configuration parameters 26 myScroll.func_count(myScroll);// stay innerHTML Change or change onresize And then recalculate dragbar The length of 
// In this way, you can configure different scrollbar styles for multiple elements in the page 27 </script>28 </html>

Code implementation :

1 function MyScrolly(elem,obj_p) 2 { 3 if(elem) 4 { 5 obj_p=obj_p||{}; 6 this.elem=elem; 7 this.func_render=obj_p.func_render||MyScrolly.defaultRender;// Scrollbar's dom structure  8 this.func_count=obj_p.func_count||MyScrolly.countChildren;// How the scroll bar changes when the page changes  9 this.func_render(elem,this); 10 this.parentelem=obj_p.parentelem||elem;// Support drag and drop 、 Release 、 Prohibit the selection of peripheral elements of actions such as , The default setting is document Maybe it works better  11 //this.clientY0=this.elem.clientY;// Not used  12 this.last_clientY=-1; 13 //elem.onload 14 var that=this; 15 // Use the page viewer to observe dom change !!<- How about compatibility ??<-html5, And will lead to this Be replaced by MutationObserver object , And it's not easy to control the call conditions  16 // var MutationObserver=window.MutationObserver||window.WebKitMutationObserver||window.MozMutationObserver; 17 // //var mo=new MutationObserver(that.countChildren); 18 // var mo=new MutationObserver(function(records){ 19 // that.func_count(that); 20 // }); 21 // this.mo=mo; 22 // var option={ 23 // childList:true, 24 // subtree:true, 25 // } 26 //mo.observe(this.elem,option); 27 28 this.div2.onpointerdown=function (event) {// Press down scrollbar 29 that.picked=true; 30 that.last_clientY=event.clientY;// Take a reference point for relative positioning  31 that.parentelem.onselectstart=function(event){// Prevent selecting when sliding up and down div The text in the  32 event.returnValue=false; 33 return false; 34 } 35 } 36 this.parentelem.onpointerup=function (event) { 37 that.picked=false; 38 that.parentelem.onselectstart=null; 39 //that.parentelem.onmousewheel=null; 40 } 41 this.parentelem.onpointerleave=function (event) { 42 that.picked=false; 43 that.parentelem.onselectstart=null; 44 that.parentelem.onmousewheel=null; 45 } 46 this.parentelem.onpointermove=function (event) {// Drag effect in div_allbase Within the range  47 if(that.picked==true&&(that.last_clientY>=0)) 48 { 49 //event.preventDefault();// Prevent default behavior from happening  50 51 var int_clientY=event.clientY-that.last_clientY;// Because it's hard to locate elem Initial position ( Maybe elem It can move or deform itself ), The relative variation is used here  52 that.last_clientY=event.clientY; 53 var top_div2=parseInt(that.div2.style.top);// The distance from the upper end of the slider to the upper end of the slide rail , About div2 The meaning of the property is shown in defaultRender Method  54 var int1=top_div2+int_clientY; 55 if((that.outer_height-that.height_scrollbar)<int1) 56 {// If it's too low  57 int1=that.outer_height-that.height_scrollbar 58 } 59 if(int1<0) 60 {// If it's too far up  61 int1=0 62 } 63 64 // if((that.outer_height-that.height_scrollbar)>=(top_div2+int_clientY)&&((top.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple