Home  >  Article  >  Web Front-end  >  JavaScript custom browser scroll bar is compatible with IE, Firefox and chrome

JavaScript custom browser scroll bar is compatible with IE, Firefox and chrome

高洛峰
高洛峰Original
2017-01-07 13:22:301178browse

Today I will share with you the browser scroll bar I made myself. We know that using css to customize the scroll bar is also a good way. Although css can change the scroll bar style of the chrome browser, it can also be customized. css can also be changed The color of the IE browser scroll bar. However, CSS can only change the color of IE, and CSS cannot change the style and color of Firefox. So it can only be achieved through JavaScript. There are also plugins that can do this. Let me share my own implementation ideas using native JavaScript. Let’s take a look at the effect in the last picture:

JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome

The idea of ​​JavaScript implementation is to simulate the browser’s own scroll bar. My idea is to first put the entire document in a container, and then change the top value of the div in the container to achieve the scrolling effect. The layout is as follows:

<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&#39;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>

Define the slider and slider first, and then define A box containing content. The layout is very simple. The overflow of the body is set to hidden to hide the default scroll bar.

The main idea of ​​​​implementation is: slider moving distance/slider scrolling range = content scrolling distance/content scrollable height; slider moving distance is the distance dragged after the mouse is pressed,

The scrollable height of the content is the total height of the content minus the height of the visible area. In addition, the total height of the scroll bar is the height of the visual area. The height of the slider = the height of the visual area/the total height of the content*the height of the visual area. The last step is to determine whether the browser is Firefox.

<script type="text/javascript">
window.onload=function(){
 var oBox=document.getElementById(&#39;box&#39;);
 var oDrag=document.getElementById(&#39;drag&#39;);
 var content=document.getElementById(&#39;content&#39;);
 var viewHeight=document.documentElement.clientHeight;
 var conHeight=content.clientHeight
 oBox.style.height=viewHeight+&#39;px&#39;;
oDrag.style.height=viewHeight/conHeight*viewHeight+&#39;px&#39;;
 window.onresize = function(){
 viewHeight=document.documentElement.clientHeight;
 oBox.style.height=viewHeight+&#39;px&#39;;
oDrag.style.height=viewHeight/conHeight*viewHeight+&#39;px&#39;;
 oDrag.style.top=-content.offsetTop/(content.clientHeight-viewHeight)*(oBox.clientHeight-oDrag.clientHeight)+&#39;px&#39;; 
 }
 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+&#39;px&#39;;
  content.style.top=-contentY+&#39;px&#39;;
 }
 document.onmouseup=function (){
  document.onmousemove=null;
 }
 }
 var str=window.navigator.userAgent.toLowerCase();
 //火狐浏览器
 if (str.indexOf(&#39;firefox&#39;)!=-1){
  document.addEventListener(&#39;DOMMouseScroll&#39;,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+&#39;px&#39;;
  oDrag.style.top=-top+&#39;px&#39;;
  }
  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+&#39;px&#39;;
  oDrag.style.top=-top+&#39;px&#39;;
  };
 },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+&#39;px&#39;;
  oDrag.style.top=-top+&#39;px&#39;;
  };
  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+&#39;px&#39;;
  oDrag.style.top=-top+&#39;px&#39;;
  };
 }
 }
}
</script>

The above is the entire process of my own implementation. There are also many BUGs in it, such as the problem of browser zooming. Thank you for reading. If you have any corrections, you are welcome to correct them.

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. I also hope for your support. PHP Chinese website!

For more JavaScript custom browser scroll bars compatible with IE, Firefox and chrome related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn