<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0045)http://www.think365.com/mycode/js/scroll-pic/ -->
<HTML><HEAD><TITLE>自定义滚动条(javascript+图片,非css,滚动跳高度自适应)</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<STYLE type=text/css>HTML {
 WIDTH: 100%; HEIGHT: 100%; BACKGROUND-COLOR: #000000
}
BODY {
 PADDING-RIGHT: 0px; BACKGROUND-POSITION: 164px 0px; PADDING-LEFT: 0px; FONT-SIZE: 9pt; BACKGROUND-IMAGE: url(images/bg_scroll.gif); PADDING-BOTTOM: 0px; MARGIN: 0px; WIDTH: 180px; COLOR: #000000; PADDING-TOP: 0px; BACKGROUND-REPEAT: repeat-y; HEIGHT: 373px; BACKGROUND-COLOR: #cccccc; TEXT-DECORATION: none
}
#scrollerContent {
 POSITION: absolute
}
#drag {
 MARGIN-LEFT: 1px; WIDTH: 9px; BACKGROUND-COLOR: #000000
}
</STYLE>

<SCRIPT>
var upH = 7;//向上的箭头的高度
var upW = 10; //向上的箭头的宽度
var downH = 7;//向下的箭头的高度
var downW = 10;//向下的箭头的宽度
var dragH = 24; //滚动条的高度
var dragW = 10; //滚动条的宽度
var scrollH =351; //滚动体的高度
var speed =9; //滚动的速度
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;
var mouseY;
var mouseX;
var clickUp = false;
var clickDown = false;
var clickDrag = false;
var clickAbove = false;
var clickBelow = false;
var timer = setTimeout("",500);
var upL;
var upT;
var downL;
var downT;
var dragL;
var dragT;
var rulerL;
var rulerT;
var contentT;
var contentH;
var contentClipH;
var scrollLength;
var startY;
function down(e){
if((document.layers && e.which!=1) || (document.all && event.button!=1)) return true;
getMouse(e);
startY = (mouseY - dragT);
if(mouseX >= upL && (mouseX <= (upL + upW)) && mouseY >= upT && (mouseY <= (upT + upH))){
clickUp = true;
return scrollUp();
}
else if(mouseX >= downL && (mouseX <= (downL + downW)) && mouseY >= downT && (mouseY <= (downT + downH))){
clickDown = true;
return scrollDown();
}
else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH))){
clickDrag = true;
return false;
}
else if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= rulerT && (mouseY <= (rulerT + scrollH))){
if(mouseY < dragT){
clickAbove = true;
clickUp = true;
return scrollUp();
}
else{
clickBelow = true;
clickDown = true;
return scrollDown();
}
}
else{
return true;
}
}
function move(e){
if(clickDrag && contentH > contentClipH){
getMouse(e);
dragT = (mouseY - startY);
if(dragT < (rulerT))
dragT = rulerT;
if(dragT > (rulerT + scrollH - dragH))
dragT = (rulerT + scrollH - dragH);
contentT = ((dragT - rulerT)*(1/scrollLength));
contentT = eval('-' + contentT);
moveTo();
if(ie4)
return false;
}
}
function up(){
clearTimeout(timer);
clickUp = false;
clickDown = false;
clickDrag = false;
clickAbove = false;
clickBelow = false;
return true;
}
function getT(){
if(ie4)
contentT = document.all.scrollerContent.style.pixelTop;
else if(nn4)
contentT = document.scrollerContentClip.document.scrollerContent.top;
else if(dom)
contentT = parseInt(document.getElementById("scrollerContent").style.top);
}
function getMouse(e){
if(ie4){
mouseY = event.clientY + document.body.scrollTop;
mouseX = event.clientX + document.body.scrollLeft;
}
else if(nn4 || dom){
mouseY = e.pageY;
mouseX = e.pageX;
}
}
function moveTo(){
if(ie4){
document.all.scrollerContent.style.top = contentT;
document.all.ruler.style.top = dragT;
document.all.drag.style.top = dragT;
}
else if(nn4){
document.scrollerContentClip.document.scrollerContent.top = contentT;
document.ruler.top = dragT;
document.drag.top = dragT;
}
else if(dom){
document.getElementById("scrollerContent").style.top = contentT + "px";
document.getElementById("drag").style.top = dragT + "px";
document.getElementById("ruler").style.top = dragT + "px";

}
}
function scrollUp(){
getT();
if(clickAbove){
if(dragT <= (mouseY-(dragH/2)))
return up();
}
if(clickUp){
if(contentT < 0){
dragT = dragT - (speed*scrollLength);
if(dragT < (rulerT))
dragT = rulerT;
contentT = contentT + speed;
if(contentT > 0)
contentT = 0;
moveTo();
timer = setTimeout("scrollUp()",25);
}
}
return false;
}
function scrollDown(){
getT();
if(clickBelow){
if(dragT >= (mouseY-(dragH/2)))
return up();
}
if(clickDown){
if(contentT > -(contentH - contentClipH)){
dragT = dragT + (speed*scrollLength);
if(dragT > (rulerT + scrollH - dragH))
dragT = (rulerT + scrollH - dragH);
contentT = contentT - speed;
if(contentT < -(contentH - contentClipH))
contentT = -(contentH - contentClipH);
moveTo();
timer = setTimeout("scrollDown()",25);
}
}
return false;
}
function reloadPage(){
location.reload();
}
function eventLoader(){
if(ie4){
upL = document.all.up.style.pixelLeft;
upT = document.all.up.style.pixelTop;
downL = document.all.down.style.pixelLeft;
downT = document.all.down.style.pixelTop;
dragL = document.all.drag.style.pixelLeft;
dragT = document.all.drag.style.pixelTop;
rulerT = document.all.ruler.style.pixelTop;
contentH = parseInt(document.all.scrollerContent.scrollHeight);
contentClipH = parseInt(document.all.scrollerContentClip.style.height);
}
else if(nn4){
upL = document.up.left;
upT = document.up.top;
downL = document.down.left;
downT = document.down.top;
dragL = document.drag.left;
dragT = document.drag.top;
rulerT = document.ruler.top;
contentH = document.scrollerContentClip.document.scrollerContent.clip.bottom;
contentClipH = document.scrollerContentClip.clip.bottom;
}
else if(dom){
upL = parseInt(document.getElementById("up").style.left);
upT = parseInt(document.getElementById("up").style.top);
downL = parseInt(document.getElementById("down").style.left);
downT = parseInt(document.getElementById("down").style.top);
dragL = parseInt(document.getElementById("drag").style.left);
dragT = parseInt(document.getElementById("drag").style.top);
rulerT = parseInt(document.getElementById("ruler").style.top);
contentH = parseInt(document.getElementById("scrollerContent").offsetHeight);
contentClipH = parseInt(document.getElementById("scrollerContentClip").offsetHeight);
document.getElementById("scrollerContent").style.top = 0 + "px";

}
scrollLength = ((scrollH-dragH)/(contentH-contentClipH));
if(nn4){
document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
window.onresize = reloadPage;
}
document.onmousedown = down;
document.onmousemove = move;
document.onmouseup = up;
}

</SCRIPT>

<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY onload=eventLoader()><SPAN id=drag
style="LEFT: 164px; POSITION: absolute; TOP: 8px"></SPAN><SPAN id=ruler
style="TOP: 8px"></SPAN><SPAN id=up
style="LEFT: 164px; POSITION: absolute; TOP: 0px"><IMG height=7 alt=""
src="自定义滚动条(javascript+图片,非css,滚动跳高度自适应).files/scroll_r1_c1.gif" width=10
border=0></SPAN> <SPAN id=down
style="LEFT: 164px; POSITION: absolute; TOP: 360px"><IMG height=6 alt=""
src="自定义滚动条(javascript+图片,非css,滚动跳高度自适应).files/scroll_r5_c1.gif" width=10
border=0></SPAN> <SPAN id=scrollerContentClip
style="BORDER-RIGHT: #cc6600 0px solid; BORDER-TOP: #cc6600 0px solid; LEFT: 0px; VISIBILITY: visible; OVERFLOW: hidden; BORDER-LEFT: #cc6600 0px solid; WIDTH: 164px; CLIP: rect(0px 164px 365px 0px); BORDER-BOTTOM: #cc6600 0px solid; POSITION: absolute; TOP: 0px; HEIGHT: 365px"><SPAN
id=scrollerContent style="WIDTH: 164px; HEIGHT: 365px">
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P>
<H1>自定义样式的滚动条</H1>
<P>javascript+图片实现,滚动条的高度随着内容区的多少而自动变化(类似于默认的滚动条),当内容没有超出规定区域时,滚动条隐藏不显示。</P></SPAN></SPAN>
<SCRIPT language=JavaScript type=text/javascript>
contentH = parseInt(document.getElementById("scrollerContent").offsetHeight);
dragH=parseInt((351/contentH)*351);
if(contentH>365){
document.getElementById("drag").style.height = dragH + "px";}
if(contentH<=365){
document.getElementById("drag").style.display="none";
document.getElementById("ruler").style.display="none";

}
//alert(contentH);
//alert(dragH);
</SCRIPT>
</BODY></HTML>

posted on 2007-03-19 13:29  mbskys  阅读(304)  评论(0)    收藏  举报