JavaScript自定义浏览器滚动条兼容IE、 火狐和chr
网络编程 2021-07-04 19:20www.168986.cn编程入门
本文主要分享了使用原生JavaScript实现自定义浏览器滚动条兼容IE、 火狐和chrome的思路与方法,具有一定的参考价值,狼蚁网站SEO优化跟着长沙网络推广一起来看下吧
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用css来自定义滚动条也是挺好的方式,css虽然能够改变chrome浏览器的滚动条样式可以自定义,css也能够改变IE浏览器滚动条的颜色。css只能是改变IE浏览器的颜色,而且CSS不能做到改变火狐浏览器的样式和颜色。所以只能是通过JavaScript来实现了。也有插件可以做到。我分享一下我自己使用原生JavaScript实现的思路。先上个图看下效果
JavaScript实现的思路就是模拟浏览器自身滚动条。我制作的思路是先将整个文档放在一个容器里面,然后通过改变容器里面的div的值来实现滚动效果布局如下
<style> { margin:0; padding:0; } body{ overflow:hidden; } #box{ float:right; :0; right:0; width:20px; background:#c; position:relative; } #drag{ position: absolute; :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:#c;width: 100px;"> Although many people talk about the super performance of quantum puting, such as one second to plete the current superputer puting tasks for several years, but so far did not create a true sense of the quantum puter, one of the very important reason is that, The state of particles used in quantum putation is not stable, and any electromagic 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 puters. Six months ago in the laboratory of Shanghai Jiaotong University, Jia Jinfeng suessfully 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 ological insulators on the superconductors with thickness of 5 nanometers. The ological superconductor materials are prepared and finally the Mayolana fermions are found at the interface of the ological superconductors. The mysterious particles were captured 80 years, but also let Jia Jinfeng more firm with its confidence in the manufacture of quantum puters. Speaking of the future of the plan, Jia Jinfeng said: "I hope to within a few years to do the ological 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 puting, 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/conHeightviewHeight+'px'; window.onresize = function(){ viewHeight=document.documentElement.clientHeight; oBox.style.height=viewHeight+'px'; oDrag.style.height=viewHeight/conHeightviewHeight+'px'; oDrag.style.=-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 =e.clientY-downY; if (<=0) { =0; }; if (>=oBox.clientHeight-oDrag.clientHeight) { =oBox.clientHeight-oDrag.clientHeight; }; var scale=/(oBox.clientHeight-oDrag.clientHeight); var contentY=scale(content.clientHeight-viewHeight); oDrag.style.=+'px'; content.style.=-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 =scale(oBox.clientHeight-oDrag.clientHeight); content.style.=scrollHei+'px'; oDrag.style.=-+'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 =scale(oBox.clientHeight-oDrag.clientHeight); content.style.=scrollHei+'px'; oDrag.style.=-+'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 =scale(oBox.clientHeight-oDrag.clientHeight); content.style.=scrollHei+'px'; oDrag.style.=-+'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 =scale(oBox.clientHeight-oDrag.clientHeight); content.style.=scrollHei+'px'; oDrag.style.=-+'px'; }; } } } </script>
以上就是我自己实现的整个过程,其中也存在不少BUG,比如没有解决浏览器缩放时候的问题。感谢大家的阅读,如有指正的地方欢迎大家指正纠错
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望多多支持狼蚁SEO!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程