运行代码框
<!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 ">
<cabeça>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<título></título>
<style type="texto/css">
* {margem:0; preenchimento:0; }
corpo {margem:20px 0 400px 20px; fonte:12px Arial; }
h1 {tamanho da fonte:14px; }
ol,ul {margem:20px; altura da linha: 160%; }
ul li { cor:#f00; }
.a1 {largura:300px; altura:190px; borda:1px sólido #f00; estouro:automático; }
.a2 {largura:600px; altura:350px; margem:100px 0 100px 80px; borda:1px sólido #369; background:#eee url( );
}
.dumascroll_bar {
posição:absoluta;
topo:0;
certo:0;
índice z:9;
largura:14px;
altura:100%;
cursor: padrão;
-moz-user-select: nenhum;
-khtml-user-select:none;
seleção de usuário: nenhum;
repetição de fundo: repetição-y;
posição de fundo:-42px 0;
flutuar:esquerda;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a,.dumascroll_handle,.dumascroll_handle_a,.dumascroll_arrow_down,.dumascroll_arrow_down_a {
posição:absoluta;
esquerda:0;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a,.dumascroll_arrow_down,.dumascroll_arrow_down_a {
largura:100%;
altura:14px;
cor:#fff;
alinhamento de texto:centro;
}
.dumascroll_arrow_up,.dumascroll_arrow_up_a { topo:0; }
.dumascroll_arrow_down,.dumascroll_arrow_down_a { inferior:0; }
.dumascroll_handle,.dumascroll_handle_a {largura:100%; repetição de fundo: repetição-y; }
.dumascroll_arrow_up {posição de fundo:0 0; }
.dumascroll_arrow_up_a {posição de fundo:-14px 0; }
.dumascroll_handle {posição de fundo:-28px 0; }
.dumascroll_handle_a {posição de fundo:-56px 0; }
.dumascroll_arrow_down {posição de fundo:-70px 0; }
.dumascroll_arrow_down_a {posição de fundo:-84px 0; }
</estilo>
<roteiro>
var duma = {
$:function(o){ if(document.getElementById(o)) {return document.getElementById(o);} },
getStyle:function(o) { return o.currentStyle||document.defaultView.getComputedStyle(o,null); },
getOffset:função(o) {
var t = o.offsetTop,h = o.offsetHeight;
while(o = o.offsetParent) { t += o.offsetTop; }
return { topo:t, altura:h };
},
vincular:função(o,eType,fn) {
if(o.addEventListener) { o.addEventListener(eType,fn,false); }
senão if(o.attachEvent) { o.attachEvent("on" + eType,fn); }
else { o["on" + eType] = fn; }
},
stopEvent:função(e) {
e = e || janela.evento;
e.stopPropagation && (e.preventDefault(),e.stopPropagation()) || (e.cancelBubble = verdadeiro,e.returnValue = falso);
},
setCookie:function(c_name,valor,expiredays) {
var exdate = new Data();
exdate.setDate(exdate.getDate() + expiradias);
document.cookie = c_name + "=" + escape(valor) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
},
getCookie:function(c_name){
if(document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (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; }
retornar unescape(document.cookie.substring(c_start,c_end));
}
}
retornar "";
}
};
duma.BeautifyScrollBar = função (obj,arrowUpCss,arrowUpActiveCss,handleCss,handleActiveCss,arrowDownCss,arrowDownActiveCss) {
this.arrowUpInterval;
this.arrowDownInterval;
this.barMouseDownInterval;
este.relY;
isto.id = obj;
isto.obj = duma.$(este.id);
this.setObjCss(); //预先设置父容器的css定位才能让接下来的属性设置起作用
this.obj.innerHTML = '<div id="' + obj + 'Area" class="dumascroll_area">' + this.obj.innerHTML + '</div><div id="' + obj + 'Barra" 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 + "Área");
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:função(){
duma.getStyle(this.obj).position == "estático"? this.obj.style.position = "relativo": duma.getStyle(this.obj).backgroundColor == "transparente"? this.obj.style.backgroundColor = "#fff": null; //若容器本来就没有设posição,则初始化为relativa;若容器原来未设置背景色,则初始化为白色;
},
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";
}
senão { this.handleHeight = this.handleMinHeight; this.handle.style.height = this.handleMinHeight + "px"; }
},
setBarPos:function() { //将当前滚动的距离值存入cookie
this.barPos = this.area.scrollTop + "";
duma.setCookie(this.id + "NomedoCookie",this.barPos,1);
},
getBarPos:função() {
this.barPos = duma.getCookie(this.id + "NomedoCookie");
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); },
areaScroll:função() {
this.handle.style.display != "nenhum" ? this.handle.style.top = this.area.scrollTop/(this.area.scrollHeight - this.area.offsetHeight) * (this.bar.offsetHeight - this.handleHeight - this.arrowUpHeight - this.arrowDownHeight) + isto. arrowUpHeight + "px": null;
},
areakeydown:function(e) { //支持键盘上下按键
var isso = isso;
document.onkeydown=função(evento){
var e = evento || janela.evento,
ek = e.keyCode || e.qual;
if(ek == 40) { that.area.scrollTop += 25 }
senão 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();
}
},
handleMove:função(e) {
var e = e || janela.evento;
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();
},
handleMouseDown:função(e) {
var isso = isso,e = e || janela.evento;
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=função(){
that.handle.className = that.handleCss;
that.handle.releaseCapture ? that.handle.releaseCapture(): null;
documento.onmousemove=nulo;
};
},
barScroll:função(e) {
var e = e || janela.event,eDir; //设置滚轮事件,e.wheelDelta与e.detail分别兼容IE、W3C,根据返回值的正负来判断滚动方向
if(e.wheelDelta) { eDir = e.wheelDelta/120; }
senão if(e.detail) { eDir = -e.detail/3; }
eDir > 0 ? this.area.scrollTop -= 80: this.area.scrollTop += 80; //步长设80像素比较接近janela滚动条的滚动速度
if(this.area.scrollTop > 0 && this.area.scrollTop < this.area.scrollHeight - this.area.offsetHeight){ duma.stopEvent(e); }
this.setBarPos();
},
barDown:função(e) {
var e = e || janela.event, que = isto,
eY = e.clienteY,
mStep = this.bar.offsetHeight,
documentScrollTop = document.documentElement.scrollTop || documento.body.scrollTop,
hOffset = duma.getOffset(this.handle),
bOffset = duma.getOffset(this.bar);
if(documentScrollTop + eY <hOffset.top) { this.barMouseDownInterval = setInterval(function(e){
aquela.area.scrollTop -= aquela.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); }
senão if(documentScrollTop + eY > hOffset.top + hOffset.height) { this.barMouseDownInterval = setInterval(function(){
aquela.area.scrollTop += aquela.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); }
duma.stopEvent(e);
},
arrowUpMouseDown:function(e) {
var isso = isso;
this.arrowUpInterval = setInterval(function(){ that.area.scrollTop -= 25; that.setBarPos(); },10);
this.arrowUp.className = this.arrowUpActiveCss;
duma.stopEvent(e);
},
arrowUpMouseUp:function() { this.clearArrowUpInterval(); this.arrowUp.className = this.arrowUpCss; },
arrowUpMouseOut:function() { this.clearArrowUpInterval(); this.arrowUp.className = this.arrowUpCss; },
arrowDownMouseDown:função(e) {
var isso = isso;
this.arrowDownInterval = setInterval(function(){ that.area.scrollTop += 25; that.setBarPos(); },10);
this.arrowDown.className = this.arrowDownActiveCss;
duma.stopEvent(e);
},
arrowDownMouseUp:function() { this.clearArrowDownInterval(); this.arrowDown.className = this.arrowDownCss; },
arrowDownMouseOut:function() { this.clearArrowDownInterval(); this.arrowDown.className = this.arrowDownCss; },
executar:função(){
var isso = isso;
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(evento){that.arrowUpMouseDown(evento)};
this.arrowUp.onmouseup = function(){that.arrowUpMouseUp()};
this.arrowUp.onmouseout = function(){that.arrowUpMouseOut()};
this.arrowDown.onmousedown = function(evento){that.arrowDownMouseDown(evento)};
this.arrowDown.onmouseup = function(){that.arrowDownMouseUp()};
this.arrowDown.onmouseout = function(){that.arrowDownMouseOut()};
duma.bind(this.obj,"mousewheel",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.comprimento,
dumascrollClass = /bdumascrollb/,
oArr = [],
oArrLen = oArr.comprimento;
for(var i=0; i<oLen; i++) {
if(dumascrollClass.test(o[i].className)) {
oArr.push("dumaScrollAreaId_" + i);
oArrLen = oArr.comprimento;
o[i].id = oArr[oArrLen - 1];
}
}
for(var j=0; j<oArrLen; j++) {
new 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(janela,"carregar",duma.BeautifyScrollBar.init);
</script>
</head>
<corpo>
<div class="a dumascroll b" style="width:300px; height:190px; padding:5px; border:1px solid #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="largura:400px; altura:100px; margem:20px; borda:2px inserção #eee; overflow:auto;">
我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />
我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的classe,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!/ >我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的classe,拿我怎么样?哈哈哈!<br />我还是普通的,我不希望被渲染,我就不用你的class,拿我怎么样?哈哈哈!<br />
</div>
<div class="a dumascroll" style="width:780px; height:350px; margin:20px; border:1px solid #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>
</body>
</html>
图片自定义滚动条3.0(Duma“自动渲染版”,也许世间万物都会变,但是真挚的爱,却永远留存在心中!)
将来版本可继续扩展的功能有: