今天听到别人说自定义滚动条,所以就在吃饭的时间写了个

html部分

 <div class="out" id="out">
<div class="inner" id="inner">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
<div class="box">11</div>
<div class="box">12</div>
</div>
<div class="scrollbar" id="scrollbar">
<div class="scrollbtn" id="scrollbtn"></div>
</div>
</div>

css部分

<style>
*{
padding:;margin:;border:;
}
.out{
width:300px;height:600px;
margin:20px auto;
background:#866C51;
overflow: hidden;
position: relative;
}
.inner{
width:300px;height:auto;
margin:0px auto;
background:#866C51;
}
.box{
width:100%;height:200px;
text-align:center;
line-height:200px;
}
.box:nth-child(1){
background: #11ff4c;
}
.box:nth-child(2){
background: #19faff;
}
.box:nth-child(3){
background: #ff831e;
}
.box:nth-child(4){
background: #111111;
}
.box:nth-child(5){
background: #ff6dd2;
}
.box:nth-child(6){
background: #223aff;
}
.box:nth-child(7){
background: #111111;
}
.box:nth-child(8){
background: #d9e8ff;
}
.box:nth-child(9){
background: #ff80d8;
}
.box:nth-child(10){
background: #a033ff;
}
.box:nth-child(11){
background: #2b33ff;
}
.box:nth-child(12){
background: #17ffda;
}
.scrollbar{
width:10px;height:600px;
background:#66513B;
position: absolute;
right:;top:;
}
.scrollbtn{
width:100%;height:50px;
background:#BCA68E;
border-radius:20px;
position: absolute;
top: 0px;
}
</style>

js部分

var inner=document.getElementById('inner');
var scrollbtn=document.getElementById('scrollbtn');
var out=document.getElementById('out');
var scrollbar=document.getElementById('scrollbar');
var bili=inner.offsetHeight/out.offsetHeight;//盒子和内容的比例
scrollbtn.style.height=scrollbar.offsetHeight/bili+'px';//内容的高
var zdh=scrollbar.offsetHeight-scrollbtn.offsetHeight;//最大的top值
var spend=20
scrollbtn.onmousedown = function(e){
var ev=e||window.event;
var y=ev.clientY-scrollbtn.offsetTop;//鼠标点击时滚动条的位置
document.onmousemove = function (e) {
var ev=e||window.event;
var tops=ev.clientY-y;//滚动时top值
if(tops<0){
tops=0
}else if(tops>zdh){
tops=zdh
}
scrollbtn.style.top=tops+'px';
inner.style.marginTop=-tops*bili+"px";
}
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
} }
//检测鼠标滚动
function mouseWheel(obj,upfun,downfun){
if(obj.addEventListener){
obj.addEventListener("mousewheel",fn);
obj.addEventListener("DOMMouseScroll",fn)
}else{
obj.attachEvent("onmousewheel",fn);
}
function fn(e){
var ev=e||window.event;
//鼠标滚轮滚动的方向
var val=ev.detail||ev.wheelDelta;
if(val==-3||val==120){
upfun();//向上滚
}else if(val==3||val==-120){
downfun();//向下滚
}
if(ev.preventDefault){
ev.preventDefault();
}else{
ev.returnValue=false;
}
}
} mouseWheel(out,function(){
if(scrollbtn.offsetTop<=0){
scrollbtn.style.top=0+'px';
inner.style.marginTop=0+'px';
}else{
inner.style.marginTop=inner.offsetTop+spend+"px";
scrollbtn.style.top=scrollbtn.offsetTop-spend/bili+'px';
} },function(){
if(scrollbtn.offsetTop>=zdh){
scrollbtn.style.top=zdh+'px';
inner.style.marginTop=-zdh*bili+'px';
}else{
inner.style.marginTop=inner.offsetTop-spend+"px";
scrollbtn.style.top=scrollbtn.offsetTop+spend/bili+'px';
} })

效果如下

js自定义滚动条的更多相关文章

  1. js 自定义滚动条

    http://visugar.com/2017/08/18/20170818CustomScroll/    chrome浏览器 https://www.cnblogs.com/yclblog/p/6 ...

  2. Vue.js 桌面端自定义滚动条组件|vue美化滚动条VScroll

    基于vue.js开发的小巧PC端自定义滚动条组件VScroll. 前段时间有给大家分享一个vue桌面端弹框组件,今天再分享最近开发的一个vue pc端自定义滚动条组件. vscroll 一款基于vue ...

  3. jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏

    一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...

  4. 利用JS实现自定义滚动条

    一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...

  5. css和js实现硬件加速渲染自定义滚动条

    听别人说用CSS的变换来实现渲染有硬件加速的效果,看到很多大网站都开始陆续使用上了,我也来说说怎么做,我这边实现的滚动条有自然滚动效果,看起来比较自然,说的再多不如直接写,让我们开始吧! 我们需要自己 ...

  6. 自定义滚动条Js简版

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>自定义滚 ...

  7. javascript 学习之自定义滚动条加滚轮事件

    要自己写一个自定义滚动条加上滚轮事件,之前的没有滚轮事件不完整,今天整理了一个. 1.滚轮事件是不兼容的,firefox中是必需要用事件绑定的添加,用的DOMMouseScroll,当滚动鼠标的时候, ...

  8. js自定义弹出框

    js自定义弹出框: 代码如下 <html> <head><title>自定义弹出对话框</title> <style type ="te ...

  9. CSS3自定义滚动条样式 -webkit-scrollbar

    今天写项目碰上需要改滚动条效果,我的第一反应是,需要用js写滚动条,顿时头大,上网搜了一下,原来css3就可以修改滚动条样式了,非常好啊,下面分享原文地址:http://www.xuanfengge. ...

随机推荐

  1. mysql 中将汉字(中文)按照拼音首字母排序

    因为数据库中可以设定表的编码格式,不同编码格式下,中文的排序有区别,下面分别介绍常用编码下的排序方法. 1.如果数据表的某字段的字符编码是 utf8_general_ci,排序写法: ORDER BY ...

  2. 洛谷P3745 [六省联考2017]期末考试

    传送门 题解 //Achen #include<algorithm> #include<iostream> #include<cstring> #include&l ...

  3. 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)

    移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...

  4. MyISAM 与 innoDB 的选择

    1.MyISAM:默认表类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写,它是存储记录和文件的标准方法.不 ...

  5. 【agc019f】AtCoder Grand Contest 019 F - Yes or No

    题意 有n个问题答案为YES,m个问题答案为NO. 你只知道剩下的问题的答案分布情况. 问回答完N+M个问题,最优策略下的期望正确数. 解法 首先确定最优策略, 对于\(n<m\)的情况,肯定回 ...

  6. git放弃本地所有未提交的修改

    1.未添加至暂存区的 git checkout . 2.已添加至暂存区的 git reset HEAD . git checkout .

  7. VC++1.5太伟大了

    功能样样俱全,一点不比现在的VS系列差 VC++1.5全貌,虽然很古老,但是已经可以了. 新建的文档试图,多文档工程,很像样 文档试图,单文档工程,已经非常像样了,和记事本差不多了 这是资源编辑器,寒 ...

  8. 免费提取百度文库 doc 文件

    首先说明,今天要推荐的这款软件,不能不能不能免费提取百度文库里 PDF 格式的文件. 对于其他的格式,无论收费与否都能免费提取. 只是口头说说免不了耍流氓的嫌疑,举栗如下: 百度文库里<喜迎党的 ...

  9. mysql 安装失败 start service执行不下去

    简单说一下自己安装mysql的经历坑点,新手应该都会遇到.新买了一个电脑,第一次安装的情况:在网上下载好几个不同的mysql,安装都在最后一步执行的时候,执行不下去,最后打开客户端,就是闪一下就消失了 ...

  10. 入门servlet:request获取请求行数据

    /** * 演示Request对象获取请求行数据 */ @WebServlet("/test") public class RequestDemo1 extends HttpSer ...