JavaScript自定義瀏覽器滾動條相容IE、 火狐和chrome
阿新 • • 發佈:2019-02-08
今天為大家分享一下我自己製作的瀏覽器滾動條,我們知道用css來自定義滾動條也是挺好的方式,css雖然能夠改變chrome瀏覽器的滾動條樣式可以自定義,css也能夠改變IE瀏覽器滾動條的顏色。但是css只能是改變IE瀏覽器的顏色,而且CSS不能做到改變火狐瀏覽器的樣式和顏色。所以只能是通過JavaScript來實現了。也有外掛可以做到。我分享一下我自己使用原生JavaScript實現的思路。先上個圖看下效果:
JavaScript實現的思路就是模擬瀏覽器自身滾動條。我製作的思路是先將整個文件放在一個容器裡面,然後通過改變容器裡面的div的top值來實現滾動效果佈局如下:
<style> *{ margin:0; padding:0; } body{ overflow:hidden; } #box{ float:right; top:0; right:0; width:20px; background:#ccc; position:relative; } #drag{ position: absolute; top:0 left:0; width:20px; background:green; } #content{ position:absolute; left: 0; } </style> <body> <div id="box"> <div id="drag"></div> </div> <div id="content"> <div style="background:#ccc;width: 100px;"> Although many people talk about the super performance of quantum computing, such as one second to complete the current supercomputer computing tasks for several years, but so far did not create a true sense of the quantum computer, one of the very important reason is that, The state of particles used in quantum computation is not stable, and any electromagnetic or physical interference can easily disrupt its work. The state of the Mayola fermion is very stable, which makes it a perfect choice for making quantum computers. Six months ago in the laboratory of Shanghai Jiaotong University, Jia Jinfeng successfully captured it. Speaking of the scene, Jia Jinfeng said: "In fact, I started to hear the Mayolana fermions, I think this thing may not be done 20 years out. Using a special material preparation method, Jia Jinfeng's research team has grown topological insulators on the superconductors with thickness of 5 nanometers. The topological superconductor materials are prepared and finally the Mayolana fermions are found at the interface of the topological superconductors. The mysterious particles were captured 80 years, but also let Jia Jinfeng more firm with its confidence in the manufacture of quantum computers. Speaking of the future of the plan, Jia Jinfeng said: "I hope to within a few years to do the topological quantum bit!" (Before) the world has not, so if we cut into this from the point, we are the same with the world The starting line, for our country, this is able to catch up with the footsteps of quantum computing, a starting point. <div> </div> </body>
先定義滑塊和滑動條,然後在定義一個裝內容的盒子,佈局很簡單,body的 overflow設定成hidden隱藏預設滾動條。
實現主要思路就是:滑塊移動距離/滑塊滾動範圍=內容滾動距離/內容可滾動高度;滑塊移動距離就是滑鼠按下後拖動的距離,
內容可滾動高度就是內容總高度減去可視區域高度。另外,滾動條的總高度就是可視區域的高度,滑塊的高度=可視區域的高度/內容的總高度*可視區域的高度。最後就是判斷瀏覽器是否是火狐。
<script type="text/javascript"> window.onload=function(){ var oBox=document.getElementById('box'); var oDrag=document.getElementById('drag'); var content=document.getElementById('content'); var viewHeight=document.documentElement.clientHeight; var conHeight=content.clientHeight oBox.style.height=viewHeight+'px'; oDrag.style.height=viewHeight/conHeight*viewHeight+'px'; window.onresize = function(){ viewHeight=document.documentElement.clientHeight; oBox.style.height=viewHeight+'px'; oDrag.style.height=viewHeight/conHeight*viewHeight+'px'; oDrag.style.top=-content.offsetTop/(content.clientHeight-viewHeight)*(oBox.clientHeight-oDrag.clientHeight)+'px'; } oDrag.onmousedown=function (ev){ //阻止預設事件 var e=ev||window.event; if (e.preventDefault) { e.preventDefault(); } else{ e.returnValue=false; }; //e.clientY滑鼠當前座標 var downY=e.clientY-oDrag.offsetTop; document.onmousemove=function (ev){ var e=ev||window.event; var top=e.clientY-downY; if (top<=0) { top=0; }; if (top>=oBox.clientHeight-oDrag.clientHeight) { top=oBox.clientHeight-oDrag.clientHeight; }; var scale=top/(oBox.clientHeight-oDrag.clientHeight); var contentY=scale*(content.clientHeight-viewHeight); oDrag.style.top=top+'px'; content.style.top=-contentY+'px'; } document.onmouseup=function (){ document.onmousemove=null; } } var str=window.navigator.userAgent.toLowerCase(); //火狐瀏覽器 if (str.indexOf('firefox')!=-1){ document.addEventListener('DOMMouseScroll',function (e){ e.preventDefault();//阻止視窗預設的滾動事件 if (e.detail<0) { var scrollHei=content.offsetTop+25; if (scrollHei>=0) { scrollHei=0; }; if (scrollHei<=-(content.clientHeight-viewHeight)) { scrollHei=-(content.clientHeight-viewHeight); }; var scale=scrollHei/(content.clientHeight-viewHeight); var top=scale*(oBox.clientHeight-oDrag.clientHeight); content.style.top=scrollHei+'px'; oDrag.style.top=-top+'px'; } if (e.detail>0) { var scrollHei=content.offsetTop-25; if (scrollHei>=0) { scrollHei=0; }; if (scrollHei<=-(content.clientHeight-viewHeight)) { scrollHei=-(content.clientHeight-viewHeight); }; var scale=scrollHei/(content.clientHeight-viewHeight); var top=scale*(oBox.clientHeight-oDrag.clientHeight); content.style.top=scrollHei+'px'; oDrag.style.top=-top+'px'; }; },false); } else{//非火狐瀏覽器 document.onmousewheel=function (ev){ var e=ev||window.event; if (e.preventDefault) { e.preventDefault(); } else{ e.returnValue=false; }; if (e.wheelDelta>0) { var scrollHei=content.offsetTop+25; if (scrollHei>=0) { scrollHei=0; }; if (scrollHei<=-(content.clientHeight-viewHeight)) { scrollHei=-(content.clientHeight-viewHeight); }; var scale=scrollHei/(content.clientHeight-viewHeight); var top=scale*(oBox.clientHeight-oDrag.clientHeight); content.style.top=scrollHei+'px'; oDrag.style.top=-top+'px'; }; if (e.wheelDelta<0) { var scrollHei=content.offsetTop-25; if (scrollHei>=0) { scrollHei=0; }; if (scrollHei<=-(content.clientHeight-viewHeight)) { scrollHei=-(content.clientHeight-viewHeight); }; var scale=scrollHei/(content.clientHeight-viewHeight); var top=scale*(oBox.clientHeight-oDrag.clientHeight); content.style.top=scrollHei+'px'; oDrag.style.top=-top+'px'; }; } } } </script>
以上就是我自己實現的整個過程,其中也存在不少BUG,比如沒有解決瀏覽器縮放時候的問題。感謝大家的閱讀,如有指正的地方歡迎大家指正糾錯