运行代码框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<头>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<标题></标题>
<样式类型=“文本/css”>
* { 边距:0;填充:0; }
正文 { 边距:20px 0 400px 20px;字体:12px Arial; }
h1 { 字体大小:14px; }
ol,ul { 边距:20px;行高:160%; }
ul li { 颜色:#f00; }
.a1 { 宽度:300px;高度:190 像素;边框:1px实心#f00;溢出:自动; }
.a2 { 宽度:600px;高度:350px;边距:100px 0 100px 80px;边框:1px实心#369;背景:#eee url( );
}
.dumascroll_bar {
位置:绝对;
顶部:0;
右:0;
z 索引:9;
宽度:14px;
高度:100%;
光标:默认;
-moz-用户选择:无;
-khtml-用户选择:无;
用户选择:无;
背景重复:重复-y;
背景位置:-42px 0;
浮动:左;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a,.dumascroll_handle,.dumascroll_handle_a,.dumascroll_arrow_down,.dumascroll_arrow_down_a {
位置:绝对;
左:0;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a,.dumascroll_arrow_down,.dumascroll_arrow_down_a {
宽度:100%;
高度:14px;
颜色:#fff;
文本对齐:居中;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a { 顶部:0; }
.dumascroll_arrow_down,.dumascroll_arrow_down_a { 底部:0; }
.dumascroll_handle,.dumascroll_handle_a { 宽度:100%;背景重复:重复-y; }
.dumascroll_arrow_up { 背景位置:0 0; }
.dumascroll_arrow_up_a { 背景位置:-14px 0; }
.dumascroll_handle { 背景位置:-28px 0; }
.dumascroll_handle_a { 背景位置:-56px 0; }
.dumascroll_arrow_down { 背景位置:-70px 0; }
.dumascroll_arrow_down_a { 背景位置:-84px 0; }
</风格>
<脚本>
var 杜马 = {
$:function(o){ if(document.getElementById(o)) {return document.getElementById(o);} },
getStyle:function(o) { return o.currentStyle||document.defaultView.getCompulatedStyle(o,null); },
获取偏移量:函数(o){
var t = o.offsetTop,h = o.offsetHeight;
while(o = o.offsetParent) { t += o.offsetTop; }
返回 { 顶部:t,高度:h };
},
绑定:函数(o,eType,fn){
if(o.addEventListener) { o.addEventListener(eType,fn,false); }
else if(o.attachEvent) { o.attachEvent("on" + eType,fn); }
else { o["on" + eType] = fn; }
},
停止事件:函数(e){
e = e ||窗口.事件;
e.stopPropagation && (e.preventDefault(),e.stopPropagation()) || (e.cancelBubble = true,e.returnValue = false);
},
setCookie:function(c_name,value,expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + 过期天数);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
},
getCookie:函数(c_name) {
if(document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
如果(c_start!= -1){
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";",c_start);
if(c_end == -1) { c_end = document.cookie.length; }
返回 unescape(document.cookie.substring(c_start,c_end));
}
}
返回 ””;
}
};
duma.BeautifyScrollBar = 函数(obj,arrowUpCss,arrowUpActiveCss,handleCss,handleActiveCss,arrowDownCss,arrowDownActiveCss) {
this.arrowUpInterval;
this.arrowDownInterval;
this.barMouseDownInterval;
这个.relY;
这个.id = obj;
this.obj = 杜马.$(this.id);
this.setObjCss(); // 预先设置父容器的css定位才能进行后续的属性设置
this.obj.innerHTML = '<div id="' + obj + '区域" class="dumascroll_area">' + this.obj.innerHTML + '</div><div id="' + obj + 'Bar" class="dumascroll_bar"><div class="dumascroll_arrow_up"></div><div class="dumascroll_handle"></div><div class="dumascroll_arrow_down"></div></div>';
this.area = duma.$(obj + "面积");
this.bar = duma.$(obj + "Bar");
this.barPos;
this.arrowUp = this.bar.getElementsByTagName("div")[0];
this.arrowUpCss = arrowUpCss;
this.arrowUpActiveCss = arrowUpActiveCss;
this.handle = this.bar.getElementsByTagName("div")[1];
this.handleCss =handleCss;
this.handleActiveCss =handleActiveCss;
this.arrowDown = this.bar.getElementsByTagName("div")[2];
this.arrowDownCss = arrowDownCss;
this.arrowDownActiveCss = arrowDownActiveCss;
this.handleMinHeight = 15;
this.arrowUpHeight = parseInt(duma.getStyle(this.arrowUp).height);
this.arrowDownHeight = parseInt(duma.getStyle(this.arrowDown).height);
this.areaScrollHeight = this.area.scrollHeight;
this.handleHeight = parseInt(this.area.offsetHeight/this.area.scrollHeight * (this.bar.offsetHeight - this.arrowUpHeight - this.arrowDownHeight));
}
duma.BeautifyScrollBar.prototype = {
setObjCss:函数() {
duma.getStyle(this.obj).position == "static" ? this.obj.style.position = "相对" : duma.getStyle(this.obj).backgroundColor == "透明" ? this.obj.style.backgroundColor = "#fff" : null; //若容器本来就没有设置位置,则初始化为相对;若容器原来未设置背景色,则初始化为白色;
},
sethandle:function() { //当内容超多时设置拖拽条子的最小高度
this.handle.style.top = this.arrowUpHeight + "px";
if(this.handleHeight > this.handleMinHeight) {
this.handleHeight < this.bar.offsetHeight - this.arrowUpHeight - this.arrowDownHeight ? this.handle.style.height = this.handleHeight + "px" : this.handle.style.display = "none";
}
else { this.handleHeight = this.handleMinHeight; this.handle.style.height = this.handleMinHeight + "px"; }
},
setBarPos:function() { //将当前滚动的距离值存入cookie
this.barPos = this.area.scrollTop + "";
duma.setCookie(this.id + "CookieName",this.barPos,1);
},
getBarPos:函数() {
this.barPos = duma.getCookie(this.id + "CookieName");
if(this.barPos!=null && this.barPos!="") {
this.area.scrollTop = this.barPos;
this.areaScroll();
}
},
clearArrowUpInterval:function() {clearInterval(this.arrowUpInterval); },
clearArrowDownInterval:function() {clearInterval(this.arrowDownInterval); },
clearBarMouseDownInterval:function() {clearInterval(this.barMouseDownInterval); },
区域滚动:函数(){
this.handle.style.display!=“无”? this.handle.style.top = this.area.scrollTop/(this.area.scrollHeight - this.area.offsetHeight) * (this.bar.offsetHeight - this.handleHeight - this.arrowUpHeight - this.arrowDownHeight) + this. arrowUpHeight + "px" : null;
},
areakeydown:function(e) { //支持键盘上下按键
var that = this;
document.onkeydown = 函数(事件) {
var e = 事件 ||窗口事件,
ek = e.keyCode || e.其中;
if(ek == 40) { that.area.scrollTop += 25 }
else if(ek == 38) { that.area.scrollTop -= 25 }
if(that.area.scrollTop > 0 && that.area.scrollTop < that.area.scrollHeight - that.area.offsetHeight){ duma.stopEvent(e); } }
that.setBarPos();
}
},
处理移动:函数(e){
var e = e ||窗口.事件;
this.area.scrollTop = (e.clientY - this.relY - this.arrowUpHeight)/(this.bar.offsetHeight - this.handleHeight - this.arrowUpHeight - this.arrowDownHeight)*(this.area.scrollHeight - this.area .offsetHeight);
this.setBarPos();
},
处理MouseDown:函数(e){
var that = this,e = e ||窗口.事件;
that.relY = e.clientY - that.handle.offsetTop;
that.handle.setCapture ? that.handle.setCapture() : null;
that.handle.className = that.handleActiveCss;
document.onmousemove = function(event) { that.handleMove(event); };
document.onmouseup = 函数() {
that.handle.className = that.handleCss;
that.handle.releaseCapture ? that.handle.releaseCapture() : null;
document.onmousemove = null;
};
},
barScroll: 函数(e) {
var e = e ||窗口.事件,eDir; //设置滚轮事件,e.wheelDelta与e.detail分别兼容IE、W3C,根据返回值的正负来判断滚动方向
if(e.wheelDelta) { eDir = e.wheelDelta/120; }
else if(e.detail) { eDir = -e.detail/3; }
eDir > 0 ? this.area.scrollTop -= 80 : this.area.scrollTop += 80; //步长设置80像素比较接近窗口滚动条的滚动速度
if(this.area.scrollTop > 0 && this.area.scrollTop < this.area.scrollHeight - this.area.offsetHeight){ duma.stopEvent(e); } }
this.setBarPos();
},
barDown:函数(e) {
var e = e || window.event,that = this,
eY = e.clientY,
mStep = this.bar.offsetHeight,
documentScrollTop = document.documentElement.scrollTop ||文档.body.scrollTop,
hOffset = duma.getOffset(this.handle),
bOffset = duma.getOffset(this.bar);
if(documentScrollTop + eY < hOffset.top) { this.barMouseDownInterval = setInterval(function(e){
that.area.scrollTop -= that.area.offsetHeight;
if(that.area.scrollTop <= (eY + documentScrollTop - bOffset.top - that.arrowUpHeight)/(that.bar.offsetHeight - that.arrowUpHeight - that.arrowDownHeight) * that.area.scrollHeight) { that.clearBarMouseDownInterval( ); }
that.setBarPos();
},80); }
else if(documentScrollTop + eY > hOffset.top + hOffset.height) { this.barMouseDownInterval = setInterval(function(){
that.area.scrollTop += that.area.offsetHeight;
if(that.area.scrollTop >= (eY + documentScrollTop - bOffset.top - that.arrowUpHeight - hOffset.height)/(that.bar.offsetHeight - that.arrowUpHeight - that.arrowDownHeight) * that.area.scrollHeight) { that.clearBarMouseDownInterval(); }
that.setBarPos();
},80); }
杜马.stopEvent(e);
},
arrowUpMouseDown:函数(e) {
var that = this;
this.arrowUpInterval = setInterval(function(){ that.area.scrollTop -= 25; that.setBarPos(); },10);
this.arrowUp.className = this.arrowUpActiveCss;
杜马.stopEvent(e);
},
arrowUpMouseUp:function() { this.clearArrowUpInterval(); this.arrowUp.className = this.arrowUpCss; },
arrowUpMouseOut:function() { this.clearArrowUpInterval(); this.arrowUp.className = this.arrowUpCss; },
arrowDownMouseDown:函数(e){
var that = this;
this.arrowDownInterval = setInterval(function(){ that.area.scrollTop += 25; that.setBarPos(); },10);
this.arrowDown.className = this.arrowDownActiveCss;
杜马.stopEvent(e);
},
arrowDownMouseUp:function() { this.clearArrowDownInterval(); this.arrowDown.className = this.arrowDownCss; },
arrowDownMouseOut:function() { this.clearArrowDownInterval(); this.arrowDown.className = this.arrowDownCss; },
运行:函数(){
var that = this;
this.sethandle();
this.areaScroll();
this.getBarPos();
this.area.onscroll = function(){that.areaScroll()};
this.area.onmouseover = this.bar.onmouseover = function(event){that.areakeydown(event)};
this.area.onmouseout = this.bar.onmouseout = function(){document.onkeydown = null};
this.handle.onmousedown = function(event){that.handleMouseDown(event)};
this.bar.onmousedown = function(event){that.barDown(event)};
this.bar.onmouseup = function(){that.clearBarMouseDownInterval()};
this.bar.onmouseout = function(){that.clearBarMouseDownInterval()};
this.arrowUp.onmousedown = function(event){that.arrowUpMouseDown(event)};
this.arrowUp.onmouseup = function(){that.arrowUpMouseUp()};
this.arrowUp.onmouseout = function(){that.arrowUpMouseOut()};
this.arrowDown.onmousedown = function(event){that.arrowDownMouseDown(event)};
this.arrowDown.onmouseup = function(){that.arrowDownMouseUp()};
this.arrowDown.onmouseout = function(){that.arrowDownMouseOut()};
duma.bind(this.obj,"鼠标滚轮",function(event){that.barScroll(event)});
duma.bind(this.obj,"DOMMouseScroll",function(event){that.barScroll(event)});
}
}
duma.BeautifyScrollBar.init = function() {
var o = document.getElementsByTagName("div"),
oLen = o.长度,
dumascrollClass = /bdumascrollb/,
oArr = [],
oArrLen = oArr.length;
for(var i=0; i<oLen; i++) {
if(dumascrollClass.test(o[i].className)) {
oArr.push("dumaScrollAreaId_" + i);
oArrLen = oArr.length;
o[i].id = oArr[oArrLen - 1];
}
}
for(var j=0; j<oArrLen; j++) {
新 duma.BeautifyScrollBar(oArr[j],"dumascroll_arrow_up","dumascroll_arrow_up_a","dumascroll_handle","dumascroll_handle_a","dumascroll_arrow_down","dumascroll_arrow_down_a").run();
}
}
duma.bind(窗口,"加载",duma.BeautifyScrollBar.init);
</脚本>
</头>
<正文>
<div class="a dumascroll b" style="width:300px; height:190px; padding:5px; border:1pxsolid #f00; line-height:400%; Overflow:hidden;">
我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />我是第一个被渲染的,真不幸<br />
</div>
<div class="a dumfaf" style="width:400px; height:100px; margin:20px; border:2px inset #eee; Overflow:auto;">
我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />
我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的课,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的课,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的课,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />被渲染,我就不用你的类,拿我怎么样?哈!<br /> />我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />我还是普通的,我不希望被渲染,我就不用你的类,拿我怎么样?哈!<br />
</div>
<div class="a dumascroll" style="width:780px; height:350px; margin:20px; border:1pxsolid #369;background:#eee url( http://bbs.blueidea.com/thread-2936181- 1-3.html " target="">以往版本</a></li>
</ol>
<h2>未来版本可继续扩展的功能有:</h2>
<ul>
<li>增加空格键的“下方向”快速滚动支持(更好地模拟windows滚动条用户体验)</li>
<li>增加页面body的渲染支持</li>
<li>增加textarea的渲染支持</li>
<li>增加身高div的渲染支持(目前代码轮廓时有滚动的bug,被增加的div的滚动高度算法到这个版本时将大幅修改)</li>
<li>增加同时出现横向滚动条的支持(当有这方面需求时可以扩展)</li>
</ul>
<p>大家期待着寻找新bug并提出宝贵建议,相信下一个版本会做的更好,谢谢(QQ:149561420,gTalk: [email protected] )。</p>
</正文>
</html>
图片自定义滚动条3.0(Duma“自动渲染版”,也许世间万物都会变,但真挚的爱,却永远留存在心中!)
未来版本可继续扩展的功能有: