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


Posted in Javascript onJanuary 05, 2017

今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色。但是css只能是改变IE浏览器的颜色,而且CSS不能做到改变火狐浏览器的样式和颜色。所以只能是通过JavaScript来实现了。也有插件可以做到。我分享一下我自己使用原生JavaScript实现的思路。先上个图看下效果:

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

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,比如没有解决浏览器缩放时候的问题。感谢大家的阅读,如有指正的地方欢迎大家指正纠错

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
js 多种变量定义(对象直接量,数组直接量和函数直接量)
May 24 Javascript
妙用Jquery的val()方法
Jun 27 Javascript
intro.js 页面引导简单用法 分享
Aug 06 Javascript
随鼠标移动的时钟非常漂亮遗憾的是只支持IE
Aug 12 Javascript
jquery复选框多选赋值给文本框的方法
Jan 27 Javascript
jQuery使用load()方法载入另外一个网页文件内的指定标签内容到div标签的方法
Mar 25 Javascript
解决bootstrap中使用modal加载kindeditor时弹出层文本框不能输入的问题
Jun 05 Javascript
JavaScript条件判断_动力节点Java学院整理
Jun 26 Javascript
javascript验证form表单数据的案例详解
Mar 25 Javascript
微信小程序实现禁止分享代码实例
Oct 19 Javascript
javascript实现用户必须勾选协议实例讲解
Mar 24 Javascript
vue如何批量引入组件、注册和使用详解
May 12 Vue.js
javascript添加前置0(补零)的几种方法
Jan 05 #Javascript
微信小程序 实战实例开发流程详细介绍
Jan 05 #Javascript
利用jquery禁止外层滚动条的滚动
Jan 05 #Javascript
bootstrap table配置参数例子
Jan 05 #Javascript
bootstrap table 表格中增加下拉菜单末行出现滚动条的快速解决方法
Jan 05 #Javascript
AngularJS中update两次出现$promise属性无法识别的解决方法
Jan 05 #Javascript
jQuery展示表格点击变色、全选、删除
Jan 05 #Javascript
You might like
PHP开发需要注意的安全问题
2010/09/01 PHP
PHP下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
2011/09/19 PHP
如何把php5.3版本升级到php5.4或者php5.5
2015/07/31 PHP
php类中的$this,static,final,const,self这几个关键字使用方法
2015/12/14 PHP
swoole锁的机制代码实例讲解
2021/03/04 PHP
javascript的键盘控制事件说明
2008/04/15 Javascript
Javascript结合css实现网页换肤功能
2009/11/02 Javascript
jQuery 2.0.3 源码分析之core(一)整体架构
2014/05/27 Javascript
js实现页面跳转重定向的几种方式
2014/05/29 Javascript
JavaScript中用字面量创建对象介绍
2014/12/31 Javascript
JS往数组中添加项性能分析
2015/02/25 Javascript
javascript获取重复次数最多的字符
2015/07/08 Javascript
Jquery简单分页实现方法
2015/07/24 Javascript
javascript实现html页面之间参数传递的四种方法实例分析
2015/12/15 Javascript
AngularJS入门教程之过滤器详解
2016/08/19 Javascript
在web中js实现类似excel的表格控件
2016/09/01 Javascript
细数JavaScript 一个等号,两个等号,三个等号的区别
2016/10/09 Javascript
JS实现移动端实时监听输入框变化的实例代码
2017/04/12 Javascript
Angular2搜索和重置按钮过场动画
2017/05/24 Javascript
深入理解Angular4中的依赖注入
2017/06/07 Javascript
Node 代理访问的实现
2019/09/19 Javascript
JavaScript中的执行环境和作用域链
2020/09/04 Javascript
python3实现短网址和数字相互转换的方法
2015/04/28 Python
使用k8s部署Django项目的方法步骤
2019/01/14 Python
在PyCharm导航区中打开多个Project的关闭方法
2019/01/17 Python
TensorFlow 显存使用机制详解
2020/02/03 Python
Pyqt助手安装PyQt5帮助文档过程图解
2020/11/20 Python
CSS3 animation ? steps 函数详解
2019/08/30 HTML / CSS
HTML5-WebSocket实现聊天室示例
2016/12/15 HTML / CSS
美国网上眼镜商城:Zenni Optical
2016/11/20 全球购物
瑞典轮胎在线:Tirendo.se
2018/06/21 全球购物
一道SQL存储过程面试题
2016/10/07 面试题
高级Java程序员面试题
2016/06/23 面试题
自荐书模板
2013/12/19 职场文书
护士个人自我鉴定
2014/03/24 职场文书
三方合作意向书范本
2015/05/09 职场文书