spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / dhtml / column23
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Logo

Scrollbars for Navigator 4 LAYERs, Part I
SPECIAL EDITION; the director's cut



Important NOTE:

DO NOT attempt to insert the scrollbars into your page by saving a DHTML Lab page with scrollbars.
DO NOT use view-source.
Take the time to type the few lines necessary, or cut-and-paste from the screen.

The Hard-Coded HTML

<LAYER ID=elCont TOP=30 LEFT=30 WIDTH=400 BGCOLOR=black CLIP=400,200>
  <LAYER ID=elMain SRC="raven.html" onLoad="initScroll()" TOP=2 LEFT=4
   WIDTH=376 BGCOLOR=white CLIP=-2,0,394,196></LAYER>
  <LAYER ID=elScroll LEFT=382 TOP=2 WIDTH=16 CLIP=16,196 VISIBILITY=HIDE>
    <IMG SRC="butTu.gif" WIDTH=16 HEIGHT=16 BORDER=0 HSPACE=0 VSPACE=0>
    <LAYER ID=elBGcol LEFT=0 TOP=16 CLIP=16,250 BACKGROUND="barBgBl.gif"></LAYER>
    <LAYER ID=elBG LEFT=0 TOP=16 CLIP=16,250 BACKGROUND="barBg.gif"></LAYER>
    <LAYER ID=elThumb LEFT=0 TOP=16 WIDTH=16 CLIP=16,100 BACKGROUND="tmbBg.gif">
      <IMG SRC="tmbTop.gif" WIDTH=16 HEIGHT=2 BORDER=0 HSPACE=0 VSPACE=0 ALIGN=RIGHT>
      <LAYER ID=elThumbBot LEFT=0 TOP=98 WIDTH=16 CLIP=16,2>
        <IMG SRC="tmbBot.gif" WIDTH=16 HEIGHT=2 BORDER=0 HSPACE=0 VSPACE=0 ALIGN=RIGHT>
      </LAYER>
    </LAYER>
    <LAYER ID=elButtonBot LEFT=0 TOP=180 WIDTH=16 CLIP=16,16>
      <IMG SRC="butBu.gif" WIDTH=16 HEIGHT=16 HSPACE=0 BORDER=0>
    </LAYER>
  </LAYER>
</LAYER>

The Images

All the images used and the complete script are available for download in zip format.

The Script

The complete script and the images used are available for download in zip format.

origWidth = innerWidth;
origHeight = innerHeight;

function reDo() {
    if (innerWidth != origWidth || innerHeight != origHeight)
        location.reload();
}

onresize = reDo;

arImLoad = ["barBg","barBgBl","bg","bgBl",
            "butBd","butBu","butTd","butTu",
            "tmbBg","tmbBgL","tmbBot","tmbTop"];

arImList = [];
for (counter in arImLoad) {
    arImList[counter] = new Image();
    arImList[counter].src = arImLoad[counter] + ".gif";
}

docIncr = 8;
barWidth = 16;
thumbMinHeight = 10;

origInt = 500;
repeatInt = 50;

initTimer = null;
scrollTimer = null;
bgTimer = null;
thumbTimer = null;

curY = null;
butImage = null;

pageLoaded = false;
onload = initScroll;

function initScroll(){
    if (!pageLoaded) {pageLoaded=true;return}        
    makeScroll();
}

function makeScroll(){
    elMain =  document.elCont.document.elMain;
    elScroll = document.elCont.document.elScroll;

    if (elMain.origTop) elMain.top = elMain.origTop;
    elMain.origTop = elMain.top;
    elMain.clip.top = 0;

    if (elMain.origHeight) elMain.clip.height = elMain.origHeight;
    elMain.origHeight = elMain.clip.height;

    docHeight = elMain.document.height;
    lyrHeight = elMain.clip.height;

    if (docHeight <= lyrHeight){
        elScroll.visibility = "hide";
        return;
    }

    elThumb = elScroll.document.elThumb;
    elThumb.top = barWidth;
    elThumbBot = elThumb.document.elThumbBot;

    elvBarHeight = elScroll.clip.height - (barWidth*2);
    thumbHeight = Math.max((lyrHeight*elvBarHeight)/docHeight,thumbMinHeight)
    elThumb.clip.height = thumbHeight;
    elThumbBot.top = thumbHeight - elThumbBot.clip.height;
    thumbMaxTop = (elvBarHeight + barWidth) - thumbHeight;

    docToTravel = docHeight - lyrHeight;
    scrToTravel = elvBarHeight - thumbHeight;
    scrPixels = scrToTravel/docToTravel;
    docPixels = docToTravel/scrToTravel;

    upImage = elScroll.document.images[0];
    elButtonBot = elScroll.document.elButtonBot;
    downImage = elButtonBot.document.images[0];

    upImage.direction = 0;
    downImage.direction = 1;
    upImage.onmousedown = butDown;        
    downImage.onmousedown = butDown;

    elThumb.captureEvents(Event.MOUSEDOWN); 
    elThumb.onmousedown = thumbDown;

    elBG = elScroll.document.elBG;
    elBGcol = elScroll.document.elBGcol;
    elBG.captureEvents(Event.MOUSEDOWN);
    elBG.onmousedown = bgDown;

    elScroll.visibility="show";
}

function clearTimers() {
    clearTimeout(initTimer);
    clearInterval(scrollTimer);
    initTimer = scrollTimer = null;
    clearTimeout(bgTimer);
    clearInterval(thumbTimer);
    bgTimer = thumbTimer = null;
}

function butDown(e){
    butImage = e.target;
    dir = butImage.direction
    butImage.origSrc = butImage.src;
    butImage.src = (dir) ? "butBd.gif" : "butTd.gif";
    butImage.onmouseout = butOut;

    captureEvents(Event.MOUSEUP)
    onmouseup = butUp;

    butMove(dir);
    if(e.type=="mousedown") initTimer = setTimeout("scrollTimer = setInterval(butMove,repeatInt,dir)",origInt);
    else scrollTimer = setInterval(butMove,repeatInt,dir);
    return false;
}

function butMove(dir) {
    if ((dir && elMain.clip.top==docToTravel) || (!dir && elMain.clip.top==0)) {clearTimers();return}
    elMain.top = (dir) ? Math.max(elMain.top-docIncr,elMain.origTop-docToTravel) : Math.min(elMain.top+docIncr,elMain.origTop);
    elMain.clip.top = (dir) ? Math.min(elMain.clip.top+docIncr,docToTravel) : Math.max(elMain.clip.top-docIncr,0) ;
    elMain.clip.height = elMain.origHeight;
    elThumb.top = Math.min(Math.max(barWidth + (scrPixels*elMain.clip.top),barWidth),thumbMaxTop);
}

function butOut(){
    clearTimers();
    butImage.src = butImage.origSrc;
    butImage.onmouseover = butDown;
}

function butUp(){
    clearTimers();
    butImage.src = butImage.origSrc;
    butImage.onmouseout = null;
    butImage.onmouseover = null;
    releaseEvents(Event.MOUSEUP);
    return false;
}

function thumbDown(e) {
    curY = e.pageY;
    captureEvents(Event.MOUSEMOVE|Event.MOUSEUP); 
    onmousemove = thumbMove;
    onmouseup = thumbUp;
    return false;
}

function thumbMove(e) {
    difY = e.pageY - curY;

    elThumb.top = Math.min(Math.max(elThumb.top+difY,barWidth),thumbMaxTop);
    docAlign();
    curY = e.pageY;
}

function thumbUp() {
    releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP); 
}

function docAlign(){
    thumbDiff = elThumb.top - barWidth;
    elMain.top = elMain.origTop - (thumbDiff*docPixels);
    elMain.clip.top = (thumbDiff*docPixels);
    elMain.clip.height = elMain.origHeight;
}

function bgDown(e){
    dir = (e.pageY < elThumb.pageY) ? -1 : 1;
    captureEvents(Event.MOUSEUP);
    onmouseup = bgUp;
    bgMove(e);
    ev = e;
    bgTimer = setTimeout("thumbTimer = setInterval(bgMove,repeatInt,ev)",origInt);
    return false;
}

function bgMove(e){
    if (e.pageY > elThumb.pageY && e.pageY <= elThumb.pageY+elThumb.clip.height) {bgUp();return}

    elThumb.top = Math.min(Math.max(elThumb.top + elThumb.clip.height * dir,barWidth),thumbMaxTop);
    elBGcol.top = (dir==1) ? elThumb.top : barWidth;
    elBGcol.clip.bottom = (dir==1) ? scrToTravel : elThumb.top-barWidth;
    elBGcol.moveAbove(elBG);
    docAlign();
}

function bgUp(){
    clearTimers();
    releaseEvents(Event.MOUSEUP);
    elBG.moveAbove(elBGcol);
    return false;
}

function loadnew(num){
    switch (num) {
        case 0:
            newFile = "oscar.html";
            break;
        case 1:
            newFile = "raven.html";
            break;
        case 2:
            newFile = "usher.html";
            break;
    }
    
    elMain.load(newFile,376)
}

// end


Produced by Peter Belesis and

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

All Rights Reserved. Legal Notices.
Created: Jan 12, 1999
Revised: Jan 12, 1999

URL: http://www.webreference.com/dhtml/column23/allCode.html