//written by nathan faubion: http://n-son.com //use this or edit how you want, just give me //some credit! function jsscrollbar (o, s, a, ev) { var self = this; this.reset = function () { //arguments that were passed this._parent = o; this._src = s; this.auto = a ? a : false; this.eventhandler = ev ? ev : function () {}; //component objects this._up = this._findcomponent("scrollbar-up", this._parent); this._down = this._findcomponent("scrollbar-down", this._parent); this._ytrack = this._findcomponent("scrollbar-track", this._parent); this._yhandle = this._findcomponent("scrollbar-handle", this._ytrack); //height and position properties this._tracktop = findoffsettop(this._ytrack); this._trackheight = this._ytrack.offsetheight; this._handleheight = this._yhandle.offsetheight; this._x = 0; this._y = 0; //misc. variables this._scrolldist = 5; this._scrolltimer = null; this._selectfunc = null; this._grabpoint = null; this._temptarget = null; this._tempdistx = 0; this._tempdisty = 0; this._disabled = false; this._ratio = (this._src.totalheight - this._src.viewableheight)/(this._trackheight - this._handleheight); this._yhandle.ondragstart = function () {return false;}; this._yhandle.onmousedown = function () {return false;}; this._addevent(this._src.content, "mousewheel", this._scrollbarwheel); this._removeevent(this._parent, "mousedown", this._scrollbarclick); this._addevent(this._parent, "mousedown", this._scrollbarclick); this._src.reset(); with (this._yhandle.style) { top = "0px"; left = "0px"; } this._movecontent(); if (this._src.totalheight < this._src.viewableheight) { this._disabled = true; this._yhandle.style.visibility = "hidden"; if (this.auto) this._parent.style.visibility = "hidden"; } else { this._disabled = false; this._yhandle.style.visibility = "visible"; this._parent.style.visibility = "visible"; } }; this._addevent = function (o, t, f) { if (o.addeventlistener) { o.addeventlistener(t, f, false); if(t=="mousewheel"){ o.addeventlistener("dommousescroll", f, false); } } else if (o.attachevent) {o.attachevent('on'+ t, f);} else {o['on'+ t] = f;} }; this._removeevent = function (o, t, f) { if (o.removeeventlistener) o.removeeventlistener(t, f, false); else if (o.detachevent) o.detachevent('on'+ t, f); else o['on'+ t] = null; }; this._findcomponent = function (c, o) { var kids = o.childnodes; for (var i = 0; i < kids.length; i++) { if (kids[i].classname && kids[i].classname == c) { return kids[i]; } } }; //thank you, quirksmode function findoffsettop (o) { var t = 0; if (o.offsetparent) { while (o.offsetparent) { t += o.offsettop; o = o.offsetparent; } } return t; }; this._scrollbarclick = function (e) { if (self._disabled) return false; e = e ? e : event; if (!e.target) e.target = e.srcelement; if (e.target.classname.indexof("scrollbar-up") > -1) self._scrollup(e); else if (e.target.classname.indexof("scrollbar-down") > -1) self._scrolldown(e); else if (e.target.classname.indexof("scrollbar-track") > -1) self._scrolltrack(e); else if (e.target.classname.indexof("scrollbar-handle") > -1) self._scrollhandle(e); self._temptarget = e.target; self._selectfunc = document.onselectstart; document.onselectstart = function () {return false;}; self.eventhandler(e.target, "mousedown"); self._addevent(document, "mouseup", self._stopscroll, false); return false; }; this._scrollbardrag = function (e) { e = e ? e : event; var t = parseint(self._yhandle.style.top); var v = e.clienty + document.body.scrolltop - self._tracktop; with (self._yhandle.style) { if (v >= self._trackheight - self._handleheight + self._grabpoint) top = self._trackheight - self._handleheight +"px"; else if (v <= self._grabpoint) top = "0px"; else top = v - self._grabpoint +"px"; self._y = parseint(top); } self._movecontent(); }; this._scrollbarwheel = function (e) { if (!e) e = window.event; var dir = 0; if (e.wheeldelta >= 120) dir = -1; if (e.wheeldelta <= -120) dir = 1; if (e.detail >= 3) dir = 1; //for firefox if (e.detail <= -3) dir = -1; //for firefox //alert(dir); self.scrollby(0, dir * 20); if(window.event) e.returnvalue = false; else e.preventdefault();//for firefox }; this._startscroll = function (x, y) { this._tempdistx = x; this._tempdisty = y; this._scrolltimer = window.setinterval(function () { self.scrollby(self._tempdistx, self._tempdisty); }, 40); }; this._stopscroll = function () { self._removeevent(document, "mousemove", self._scrollbardrag, false); self._removeevent(document, "mouseup", self._stopscroll, false); if (self._selectfunc) document.onselectstart = self._selectfunc; else document.onselectstart = function () { return true; }; if (self._scrolltimer) window.clearinterval(self._scrolltimer); self.eventhandler (self._temptarget, "mouseup"); }; this._scrollup = function (e) {this._startscroll(0, -this._scrolldist);}; this._scrolldown = function (e) {this._startscroll(0, this._scrolldist);}; this._scrolltrack = function (e) { var cury = e.clienty + document.body.scrolltop; this._scroll(0, cury - this._tracktop - this._handleheight/2); }; this._scrollhandle = function (e) { var cury = e.clienty + document.body.scrolltop; this._grabpoint = cury - findoffsettop(this._yhandle); this._addevent(document, "mousemove", this._scrollbardrag, false); }; this._scroll = function (x, y) { if (y > this._trackheight - this._handleheight) y = this._trackheight - this._handleheight; if (y < 0) y = 0; this._yhandle.style.top = y +"px"; this._y = y; this._movecontent(); }; this._movecontent = function () { this._src.scrollto(0, math.round(this._y * this._ratio)); }; this.scrollby = function (x, y) { this._scroll(0, (-this._src._y + y)/this._ratio); }; this.scrollto = function (x, y) { this._scroll(0, y/this._ratio); }; this.swapcontent = function (o, w, h) { this._removeevent(this._src.content, "mousewheel", this._scrollbarwheel, false); this._src.swapcontent(o, w, h); this.reset(); }; this.reset(); };