2023年6月20日发(作者:)
JS模拟简易滚动条效果代码(附demo源码)_
这篇文章主要介绍了JS模拟简易滚动条效果代码,可模拟出滚动条拖动显示的效果,涉及JavaScript鼠标大事的响应及页面元素运算的相关技巧,并附带demo源码供读者下载参考,需要的伴侣可以参考下
本文实例讲解并描述了JS模拟简易滚动条效果的方法。分享给大家供大家参考,具体如下:
用法Js模拟滚动条。简易模式,类似手机上常见的滚动条。
效果如下:
Js代码如下:
var scrollMoveObj = null, scrollPageY = 0, scrollY
= 0;
var scrollDivList = new Array();
// obj需要添加滚动条的对象 w滚动条宽度 className滚动条样式名称
// obj元素 必需指定高度,并设置overflow:hidden;
// 如要兼容IE6 必需给obj元素 指定 overflow:hidden;
function jsScroll(obj, w, className)
{ if(typeof(obj) == 'string') {
obj = mentById(obj);
}
//当内容未超消失在高度时,不添加滚动条
if(!obj || Height = Height ||
Height == 0) {
return;
}
BarWidth = w||6;
ow = 'hidden';
Bar = Element('div');
Child(Bar);
BarIndex
Element('div');
Child(BarIndex);
on = 'absolute';
on = 'absolute';
ame = className || '';
if(!className) {
oundColor = '#ddd';
oundColor
'#aaa';
=
= }
(obj);
scrollResetSize(obj);
//用法鼠标滚轮滚动
Div = obj;
Div = obj;
ewheel = scrollMove;
ewheel = scrollMove;
ewheel = scrollMove;
//拖动滚动条滚动
edown = function(evt){
evt = evt || event;
scrollPageY = Y;
scrollY = Top;
isScrollMove = true;
ctstart = function(){return
false};
scrollMoveObj = Div;
if(ame == '') {
oundColor
= '#888'; }
return false;
}
}
//当页面大小发生改变时,重新计算滚动条位置
ze = function(){
for(var i=0; ; i++) {
scrollResetSize(scrollDivList[i]);
}
}
//计算滚动条位置
function scrollResetSize(o) {
if(Height = Height) {
Top = 0;
y = 'none';
} else {
y = 'block';
}
var x=0, y=0;
var p = o;
while(p) {
x += Left; y += Top;
p = Parent;
}
var borderTop =
parseInt(TopWidth||0);
var borderBottom =
parseInt(BottomWidth||0);
= BarWidth + 'px';
= Height + 'px';
= y + borderTop + 'px';
= x + Width -
BarWidth + 'px';
= BarWidth +
'px';
var h = Height - (Height -
Height);
//当滚动条滑块最小20个像素
if(h 20) {
h = 20;
}
BarHeight = h;
= h + 'px';
2023年6月20日发(作者:)
JS模拟简易滚动条效果代码(附demo源码)_
这篇文章主要介绍了JS模拟简易滚动条效果代码,可模拟出滚动条拖动显示的效果,涉及JavaScript鼠标大事的响应及页面元素运算的相关技巧,并附带demo源码供读者下载参考,需要的伴侣可以参考下
本文实例讲解并描述了JS模拟简易滚动条效果的方法。分享给大家供大家参考,具体如下:
用法Js模拟滚动条。简易模式,类似手机上常见的滚动条。
效果如下:
Js代码如下:
var scrollMoveObj = null, scrollPageY = 0, scrollY
= 0;
var scrollDivList = new Array();
// obj需要添加滚动条的对象 w滚动条宽度 className滚动条样式名称
// obj元素 必需指定高度,并设置overflow:hidden;
// 如要兼容IE6 必需给obj元素 指定 overflow:hidden;
function jsScroll(obj, w, className)
{ if(typeof(obj) == 'string') {
obj = mentById(obj);
}
//当内容未超消失在高度时,不添加滚动条
if(!obj || Height = Height ||
Height == 0) {
return;
}
BarWidth = w||6;
ow = 'hidden';
Bar = Element('div');
Child(Bar);
BarIndex
Element('div');
Child(BarIndex);
on = 'absolute';
on = 'absolute';
ame = className || '';
if(!className) {
oundColor = '#ddd';
oundColor
'#aaa';
=
= }
(obj);
scrollResetSize(obj);
//用法鼠标滚轮滚动
Div = obj;
Div = obj;
ewheel = scrollMove;
ewheel = scrollMove;
ewheel = scrollMove;
//拖动滚动条滚动
edown = function(evt){
evt = evt || event;
scrollPageY = Y;
scrollY = Top;
isScrollMove = true;
ctstart = function(){return
false};
scrollMoveObj = Div;
if(ame == '') {
oundColor
= '#888'; }
return false;
}
}
//当页面大小发生改变时,重新计算滚动条位置
ze = function(){
for(var i=0; ; i++) {
scrollResetSize(scrollDivList[i]);
}
}
//计算滚动条位置
function scrollResetSize(o) {
if(Height = Height) {
Top = 0;
y = 'none';
} else {
y = 'block';
}
var x=0, y=0;
var p = o;
while(p) {
x += Left; y += Top;
p = Parent;
}
var borderTop =
parseInt(TopWidth||0);
var borderBottom =
parseInt(BottomWidth||0);
= BarWidth + 'px';
= Height + 'px';
= y + borderTop + 'px';
= x + Width -
BarWidth + 'px';
= BarWidth +
'px';
var h = Height - (Height -
Height);
//当滚动条滑块最小20个像素
if(h 20) {
h = 20;
}
BarHeight = h;
= h + 'px';
发布评论