JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用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,比如没有解决浏览器缩放时候的问题。感谢大家的阅读,如有指正的地方欢迎大家指正纠错
JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome的更多相关文章
- 自定义浏览器滚动条的样式,打造属于你的滚动条风格——兼容IE和webkit(ff不支持)
前段时间,到网上找素材时,看到了一个很个性的滚动条式,打开Chrome的调试工具看了一下,发现不是用JavaScript来模拟实现的,觉得 有必要折腾一下.于是在各大浏览器中对比了一下,发现只用Chr ...
- javascript自定义浏览器右键菜单
javascript自定义浏览器右键菜单 在书上看到document对象还有一个contextmenu事件,但是不知为什么w3school中找不到这个耶... 利用这个特性写了个浏览器的右键菜单, ...
- 总结JavaScript中浏览器的兼容问题
浅析JavaScript中浏览器的兼容问题 浏览器兼容性问题是在实际开发中容易忽略而又最重要的一部分.我们在讲老版本浏览器兼容问题之前,首先要了解什么是能力检测,它是来检测浏览器有没有这种能力,即判断 ...
- 网站开发中使用javascript获取浏览器滚动条宽度
在网站开发中,有时候需要获取浏览器滚动条的宽度,在武汉蚂蹄软件服务中心的技术人员指导之下,我实现了该需求.记录如下: 首先说明一下原理: ①生成一个div,设置滚动条不可见,记录其宽度: ②将上面的d ...
- javascript多浏览器的兼容
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...
- CSS3自定义浏览器滚动条样式
一个完整滚动条右以下部分组成: ::-webkit-scrollbar 滚动条整体部分,常用属性:width,height,background,border: ::-webkit-scrollbar ...
- 新版本火狐 ,Chrome不支持showModalDialog解决办法
平常的网站中,有时我们会希望使用者按下按钮后开启一个保持在原窗口前方的子窗口,在IE中,我们可以使用showModalDialog来达成,但是chrome早就不支持showModalDialog,最近 ...
- javascript自定义滚动条插件,几行代码的事儿
在实际项目中,经常由于浏览器自带的滚动条样式太戳,而且在各个浏览器中显示不一样,所以我们不得不去实现自定义的滚动条,今天我就用最少的代码实现了一个自定义滚动条,代码量区区只有几十行,使用起来也非常方便 ...
- 兼容ie[6-9]、火狐、Chrome、opera、maxthon3、360浏览器的js本地图片预览
html代码: <div id="divPreview"> <img id="imgHeadPhoto" src="Images/H ...
随机推荐
- 微信小程序体验(2):驴妈妈景区门票即买即游
驴妈妈因为出色的运营能力,被腾讯选为首批小程序内测单位.驴妈妈的技术开发团队在很短的时间内完成了开发任务,并积极参与到张小龙团队的内测问题反馈.驴妈妈认为,移动互联网时代,微信是巨大的流量入口,也是旅 ...
- ExtJS 4.2 Date组件扩展:添加清除按钮
ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...
- WebApi接口 - 响应输出xml和json
格式化数据这东西,主要看需要的运用场景,今天和大家分享的是webapi格式化数据,这里面的例子主要是输出json和xml的格式数据,测试用例很接近实际常用情况:希望大家喜欢,也希望各位多多扫码支持和点 ...
- js:给定两个数组,如何判断他们的相对应下标的元素类型是一样的
题目: 给Array对象原型上添加一个sameStructureAs方法,该方法接收一个任意类型的参数,要求返回当前数组与传入参数数组(假定是)相对应下标的元素类型是否一致. 假设已经写好了Array ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- SQLServer 版本之八大方法搞清 "我是谁"
你正在使用 SQL Server 的哪个版本? 贴士:作为一个SQL Server数据库管理者或维护.支持人员,应该会经常问自己这样一个问题:我当前SQL Server版本号是?当前版本已经有的累计更 ...
- jQuery标准的AJAX模板
$('#saveInformationTemplate_button').on('click', function(){ if(isEmpty($("#name").val())) ...
- ASP.NET 5 Beta 8 发布
ASP.NET 5 的路线图(详见 ASP.NET 5 Schedule and Roadmap : https://github.com/aspnet/home/wiki/roadmap ):Bet ...
- Spring7:基于注解的Spring MVC(下篇)
Model 上一篇文章<Spring6:基于注解的Spring MVC(上篇)>,讲了Spring MVC环境搭建.@RequestMapping以及参数绑定,这是Spring MVC中最 ...