JAVASCRIPT   155
jsScroller
Guest on 20th August 2022 01:02:12 AM


  1. //Written by Nathan Faubion: http://n-son.com
  2. //Use this or edit how you want, just give me
  3. //some credit!
  4.  
  5. function jsScroller (o, w, h) {
  6.         var self = this;
  7.         var list = o.getElementsByTagName("div");
  8.         for (var i = 0; i < list.length; i++) {
  9.                 if (list[i].className.indexOf("Scroller-Container") > -1) {
  10.                         o = list[i];
  11.                 }
  12.         }
  13.        
  14.         //Private methods
  15.         this._setPos = function (x, y) {
  16.                 if (x < this.viewableWidth - this.totalWidth)
  17.                         x = this.viewableWidth - this.totalWidth;
  18.                 if (x > 0) x = 0;
  19.                 if (y < this.viewableHeight - this.totalHeight)
  20.                         y = this.viewableHeight - this.totalHeight;
  21.                 if (y > 0) y = 0;
  22.                 this._x = x;
  23.                 this._y = y;
  24.                 with (o.style) {
  25.                         left = this._x +"px";
  26.                         top  = this._y +"px";
  27.                 }
  28.         };
  29.        
  30.         //Public Methods
  31.         this.reset = function () {
  32.                 this.content = o;
  33.                 this.totalHeight = o.offsetHeight;
  34.                 this.totalWidth  = o.offsetWidth;
  35.                 this._x = 0;
  36.                 this._y = 0;
  37.                 with (o.style) {
  38.                         left = "0px";
  39.                         top  = "0px";
  40.                 }
  41.         };
  42.         this.scrollBy = function (x, y) {
  43.                 this._setPos(this._x + x, this._y + y);
  44.         };
  45.         this.scrollTo = function (x, y) {
  46.                 this._setPos(-x, -y);
  47.         };
  48.         this.stopScroll = function () {
  49.                 if (this.scrollTimer) window.clearInterval(this.scrollTimer);
  50.         };
  51.         this.startScroll = function (x, y) {
  52.                 this.stopScroll();
  53.                 this.scrollTimer = window.setInterval(
  54.                         function(){ self.scrollBy(x, y); }, 40
  55.                 );
  56.         };
  57.         this.swapContent = function (c, w, h) {
  58.                 o = c;
  59.                 var list = o.getElementsByTagName("div");
  60.                 for (var i = 0; i < list.length; i++) {
  61.                         if (list[i].className.indexOf("Scroller-Container") > -1) {
  62.                                 o = list[i];
  63.                         }
  64.                 }
  65.                 if (w) this.viewableWidth  = w;
  66.                 if (h) this.viewableHeight = h;
  67.                 this.reset();
  68.         };
  69.        
  70.         //variables
  71.         this.content = o;
  72.         this.viewableWidth  = w;
  73.         this.viewableHeight = h;
  74.         this.totalWidth  = o.offsetWidth;
  75.         this.totalHeight = o.offsetHeight;
  76.         this.scrollTimer = null;
  77.         this.reset();
  78. };

Raw Paste

Login or Register to edit or fork this paste. It's free.