JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
作者:依然仰望 发布时间:2024-04-16 09:37:25
标签:js,自定义,滚动条
今天为大家分享一下我自己制作的浏览器滚动条,我们知道用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>
来源:http://www.cnblogs.com/wswq/p/6251826.html


猜你喜欢
- 看到一篇Implementing an Infinite Scroll with Vue.js , 觉得挺实用的就看了下, 顺便简单翻译了一
- PDOStatement::closeCursorPDOStatement::closeCursor — 关闭游标,使语句能再次被执行。(P
- 本文记录了python安装及环境配置方法,具体内容如下Python安装 Windowns操作系统中安装Python步骤一 下载安装包从Pyt
- 一、Oracle 11g安装安装之前要先确定自己的电脑配置,以windows为例,如果是win7以下系统如xp等,可以选择Oracle 10
- 表单在网页中主要负责数据采集功能。一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数
- 概述map是基于key-value键值对的无序的集合Go语言中的map是引用类型必须初始化才能使用。语法声明和初始化配合make使用,否则是
- 为什么要将MySQL数据库必须运行在“普通用户”的状态下呢?与MSSQL SERVER一样,因为如果使用了“超级管理员”或者“本地系统用户”
- 0.引言利用python开发,借助Dlib库进行人脸识别,然后将检测到的人脸剪切下来,依次排序显示在新的图像上;实现的效果如下图所示,将图1
- 一个封装好的JavaScript拖动类,使用方便:<div id="idDrag" style="bor
- 倒排索引一 倒排索引是什么倒排索引源于实际应用中需要根据属性的值来查找记录,这种索引表中的每一个项都包括一个属性值和具有该属性值的各记录的地
- 1.vue中echarts的使用 引入echarts后let myChart = echarts.init(document.getElem
- Pycharm Python Console用法Pycharm的下方工具栏中有两个窗口:Python Console和Terminal(如下
- 求f(x) = sin(x)/x 的不定积分和负无穷到正无穷的定积分sin(x)/x 的不定积分是信号函数sig ,负无穷到正无穷的定积分为
- 我们需要评估模型预测值来评估训练的好坏。 模型评估是非常重要的,随后的每个模型都有模型评估方式。使用TensorFlow时,需要把模型评估加
- 从低版本迁移到MySQL 8后,可能由于字符集问题出现 Illegal mix of collations (utf8mb4_general
- 每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto
- 本文实例为大家分享了wxPython整点报时的具体代码,供大家参考,具体内容如下# C盘要有个wav文件,内含报时音频import wx &
- 本文实例为大家分享了JavaScript实现切换多张图片的具体代码,供大家参考,具体内容如下循环切换图片HTML+CSS+JavaScrip
- 合并两个没有共同列的dataframe,相当于按行号求笛卡尔积。最终效果如下以下代码是参考别人的代码修改的:def cartesian_df
- 一、在线程中获取时间,判断当前时间三面之后,触发“事件”对象。 运行结果:二、在另一个线程