<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Author" content=" ">
<title>Document</title>
<style type="text/css">
*{margin: 0;padding: 0;}
a{text-decoration: none;}
ul,li{list-style: none;}
body{font-size: 14px;font-family: "微软雅黑";-webkit-user-select:none;-moz-user-select:none;user-select:none;-o-user-select:none; -ms-user-select:none;}
#box{width: 700px;height: 500px;background: #FCF3B4;margin: 30px auto;padding: 20px;font-size: 16px;color: #333;position: relative;overflow: hidden;}
.pbold{font-size: 20px;font-weight: bold;font-family: "Vivaldi";color: #EF41B7}
#scrollBar{width: 20px;height: 100%;border-radius:10px;background:linear-gradient(#52C5FC,#E78EFD);position: absolute;top: 0;right: 0;display: none;}
#bar{width: 20px;height: 50px;background: rgba(0,0,0,.5);border-radius:10px;position: absolute;top: 0;right;z-index: 2}
#b_over{width: 100%;height: 100%;background: transparent;position: absolute;top: 0;right;z-index: 0}
#content{position: relative;}
</style>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
var barDom = document.getElementById("bar");
var boxDom = document.getElementById("box");
var overDom = document.getElementById("b_over");//遮罩
var contentDom = document.getElementById("content");
var scrollDom = document.getElementById("scrollBar");
var maxH = boxDom.offsetHeight - parseInt(getStyle(barDom,"height"));//barDom.offsetHeight获取不到barDom.offsetHeight==>0 ==>none
// console.log(maxH);
var conHeight = contentDom.offsetHeight - boxDom.offsetHeight + contentDom.offsetTop*2; //padding值
barDom.onmousedown = function(e){
var e = e || window.event;
e.cancelBubble = true;
var pos = getXY(e);
// 只需要高度
var ctop = pos.y - this.offsetTop;
// var maxH = boxDom.offsetHeight-this.offsetHeight;
// var cleft = pos.x - this.offsetLeft;
boxDom.onmouseout = null;//按下时让onmouseout事件为null;
document.onmousemove = function(e){
var e = e || window.event;
var pos = getXY(e);
var ntop = pos.y - ctop;
if(ntop<=0)ntop=0;
if(ntop>=maxH)ntop = maxH;
barDom.style.top = ntop + "px";
contentDom.style.top = -(ntop/maxH)*conHeight + "px";
};
document.onmouseup = function(){
document.onmousemove = null;
document.onmouseup = null;
// 放下时再开启事件;
boxDom.onmouseout = function(){
scrollDom.style.display = "none";
if(document.onmousewheel === null){
document.onmousewheel = null;
}else{
document.removeEventListener("DOMMouseScroll",scrollFn);
};
}
};
};
// overDom.onmousedown = function(e){
// var e = e||window.event;
// var ctop = getXY(e).y - boxDom.offsetTop - barDom.offsetHeight; //
// if(ctop<=0)ctop = 0;
// barDom.style.top = ctop + "px";
// // console.log(ctop,maxH,conHeight);
// contentDom.style.top = -(ctop/maxH)*conHeight + "px";
// }
scrollDom.onmousedown = function(e){ //错误 ,同时触发barDom, 在boxDom
var e = e||window.event;
var ctop = getXY(e).y - boxDom.offsetTop - barDom.offsetHeight; //
if(ctop<=0)ctop = 0;
barDom.style.top = ctop + "px";
// console.log(ctop,maxH,conHeight);
contentDom.style.top = -(ctop/maxH)*conHeight + "px";
};
boxDom.onmouseover = function(){
scrollDom.style.display = "block";
// 兼容
// alert(document.onmousewheel);
if(document.onmousewheel === null){
document.onmousewheel = scrollFn;
}else{
document.addEventListener("DOMMouseScroll",scrollFn);
};
};
// 滚轮事件
function scrollFn(e){
var e = e||window.event;
var d = e.wheelDelta || -e.detail;
var ntop = parseInt(getStyle(barDom,"top")); //去单位
if(d<0){
ntop += 20;
}else{
ntop -= 20;
};
if(ntop<=0)ntop=0;
console.log(ntop);
if(ntop>=maxH)ntop = maxH;
barDom.style.top = ntop + 'px';
contentDom.style.top = -(ntop/maxH)*conHeight + "px";
// 屏蔽浏览器滚轮;
e.preventDefault();
};
boxDom.onmouseout = function(){ //只在这里写,拖动的时候 若移出boxDom.就会触发.
scrollDom.style.display = "none";
// 清除滚轮
if(document.onmousewheel === null){
document.onmousewheel = null;
}else{
document.removeEventListener("DOMMouseScroll",scrollFn);
};
};
// 获取鼠标坐标
function getXY(e){
var e = e || window.event;
var x=0,y=0;
if(e.pageX){
x = e.pageX;
y = e.pageY;
}else{
var ctop = 0,cleft =0;
if(document.documentElement){
ctop = document.documentElement.scrollTop;
cleft = document.documentElement.scrollLeft;
}else{
ctop = document.body.scrollTop;
cleft = document.body.scrollLeft;
}
x = ctop + e.clientX;
y = cleft + e.clientY;
};
return {x:x,y:y};
};
function getStyle(obj,attr){//有单位
return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr];
};
};
</script>
<div id="box">
<div id="scrollBar">
<div id="bar"></div>
<div id="b_over"></div>
</div>
<div id="content" onselectstart="return false">
<h2>阿甘正传</h2>
<br/>
<p class="pbold">1、Life was like a box of chocolates, you never know what you're going to get.</p>
<p>生命就像一盒巧克力,结果往往出人意料。</p>
<p class="pbold">2、Stupid is as stupid does.</p>
<p>蠢人做蠢事,也可理解为傻人有傻福。</p>
<p class="pbold">3、Miracles happen every day.</p>
<p>奇迹每天都在发生。</p>
<p class="pbold">4、it made me look like a duck in water.</p>
<p>它让我如鱼得水。</p>
<p class="pbold">5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
<p>我不懂我们是否有着各自的命运,还是只是到处随风飘荡。</p>
<p class="pbold">6、Death is just a part of life, something we're all destined to do.</p>
<p>死亡是生命的一部分,是我们注定要做的一件事。</p>
<p class="pbold">7、You have got to put the past behind you before you can move on.</p>
<p>放下包袱,继续前进。</p>
<p class="pbold">8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
<p>就像太阳在落山前映射在河口上,有无数的亮点在闪闪发光。</p>
<p class="pbold">9、If there is anything you need, I will not be far away.</p>
<p>只要你需要,我就在这里。</p>
<p class="pbold">10、I am a man of my word.</p>
<p>我是信守承诺的人。</p>
<p class="pbold">11、There is one small step for a man,a giant leap for mankind.</p>
<p>某人的一小步就是人类的一大步。</p>
</div>
</div>
</body>
</html>
自定义滚轮
最新推荐文章于 2022-03-29 20:42:18 发布