js自定义滚动条
今天听到别人说自定义滚动条,所以就在吃饭的时间写了个
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自定义滚动条的更多相关文章
- js 自定义滚动条
http://visugar.com/2017/08/18/20170818CustomScroll/ chrome浏览器 https://www.cnblogs.com/yclblog/p/6 ...
- Vue.js 桌面端自定义滚动条组件|vue美化滚动条VScroll
基于vue.js开发的小巧PC端自定义滚动条组件VScroll. 前段时间有给大家分享一个vue桌面端弹框组件,今天再分享最近开发的一个vue pc端自定义滚动条组件. vscroll 一款基于vue ...
- jquery自定义滚动条 鼠标移入或滚轮时显示 鼠标离开或悬停超时时隐藏
一.需求: 我需要做一个多媒体播放页面,左侧为播放列表,右侧为播放器.为了避免系统滚动条把列表和播放器隔断开,左侧列表的滚动条需要自定义,并且滚动停止和鼠标离开时要隐藏掉. 二.他山之石: 案例来自h ...
- 利用JS实现自定义滚动条
一般默认的滚动条会比较丑,我们可以用简单的js实现自定义滚动条的功能: 代码如下: <!doctype html> <html> <head> <meta c ...
- css和js实现硬件加速渲染自定义滚动条
听别人说用CSS的变换来实现渲染有硬件加速的效果,看到很多大网站都开始陆续使用上了,我也来说说怎么做,我这边实现的滚动条有自然滚动效果,看起来比较自然,说的再多不如直接写,让我们开始吧! 我们需要自己 ...
- 自定义滚动条Js简版
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>自定义滚 ...
- javascript 学习之自定义滚动条加滚轮事件
要自己写一个自定义滚动条加上滚轮事件,之前的没有滚轮事件不完整,今天整理了一个. 1.滚轮事件是不兼容的,firefox中是必需要用事件绑定的添加,用的DOMMouseScroll,当滚动鼠标的时候, ...
- js自定义弹出框
js自定义弹出框: 代码如下 <html> <head><title>自定义弹出对话框</title> <style type ="te ...
- CSS3自定义滚动条样式 -webkit-scrollbar
今天写项目碰上需要改滚动条效果,我的第一反应是,需要用js写滚动条,顿时头大,上网搜了一下,原来css3就可以修改滚动条样式了,非常好啊,下面分享原文地址:http://www.xuanfengge. ...
随机推荐
- .Net Email操作类
using System; using System.Text; using System.Net.Mail; using System.Net; using System.Linq; using S ...
- HBase实际应用中的性能优化方法
- 快速傅里叶变换(FFT)学习
首先,在写这篇博客之前,我还没有完全学会FFT. 先把会的部分打好,加深一下记忆(也可以说是做笔记吧). 初三了,还不会FFT,要退役喽-- 多项式乘法 点开这篇博客之前,你就应该知道,FFT是用来求 ...
- 开源代码分析-react-native-eyepetizer
目录结构: app----imgs --- pages ------ home ------ explore ------ follow ------ profile ------ selected ...
- MUI使用vue示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- cookie - 提示上一次访问该网页的时间
案例:记住上一次访问时间 1. 需求: 1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问. 2. ...
- 初探.NET CORE WEB API(RESTful风格)
前面有4篇系列博客 (一)Asp.net web api中的坑-[找不到与请求 URI匹配的 HTTP 资源] (二)Asp.net web api中的坑-[http get请求中的参数] (三)As ...
- tortoisegit如何删除远程分支
图片来自:https://zhidao.baidu.com/question/134542616148384045.html
- Idea SSH框架整合基础代码
第一步 pom.xml注入jar包依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- Leetcode400Nth Digit第N个数字
在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). 示例 1: ...