JavaScript Scroller

iScroll alternative

Beispiel: ohne großes Framework

Bei meiner Suche nach einer Scroll Komponente bin ich auf alle möglichen Probleme gestoßen. In einigen führte die Anzeige von ImageMaps zu Problemen, sie brauchten zusätzlich noch jQuery und die Länge des Codes war eigentlich auch nicht passabel.

Also zur Sache, die hier gezeigte Komponente unterstützt alle aktuellen Browser. Eine Liste der unterstützten Touch Devices werde ich noch aufführen. Es wird sowohl Touch, Mouse als auch Mousewheel unterstützt. Beim verlassen des Scroll-Objektes endet das Scrollen, bei anderen Komponenten war das nicht so, das hatte mich auch gestört. Es werden keine Wrapper-DIVs benötigt, es muss nur ein Script-Datei eingebunden werden die gerade mal 262 Zeilen lang ist,... den Gummiband-Effekt des Scroll-Balkens der dann entsteht wenn man über das Ende hinaus scrollt könnte man noch realisieren.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.

Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.

At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.

Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus.

HTML:
scroll.htm HTML (4,1 kByte) 27.10.2021 23:26
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8" />
    <title>scroll</title>
  </head>
  <body>
    <div class="scroll">
      <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
      <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
      <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
      <p>Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
      <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis.</p>
      <p>At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.</p>
      <p>Consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus.</p>
    </div>
    <script src="jsScroll.js"></script>
  </body>
</html>
Javascript:
jsScroll.js JavaScript (8,63 kByte) 24.11.2021 23:53
function MyScroll() {
  this.object = null;
  this.content = null;
  this.isTouchDevice = false;
  this.start = false;
  this.scrollbar = null;
  this.scroller = null;
  this.startX = 0;
  this.startY = 0;
  this.curX = 0;
  this.curY = 0;
  this.lastMove = null;
}

MyScroll.prototype = {
  touchStart: function (event) {
    if (event.type === 'touchstart') { //touch event
      // check that only one finger is touching the screen
      if (event.touches.length === 1) {
        // get the coordinates of the touch
        this.start = true;
        this.startX = event.touches[0].pageX;
        this.startY = event.touches[0].pageY;
      } else {
        // more than one finger touched so cancel
        this.touchCancel(event);
      }
    } else {
      if (event.button === 0) { // mouse event
        this.start = true;
        this.startX = event.pageX;
        this.startY = event.pageY;
      } else { // old IE mouse event
        this.start = true;
        this.startX = event.clientX + document.body.scrollLeft;
        this.startY = event.clientY + document.body.scrollTop;
      }
    }
  },
  touchMove: function (event) {
    if (this.start) {
      var bScroll = false;
      if (event.type === 'touchmove') { // touch event
        // check that only one finger is touching the screen
        if (event.touches.length === 1) {
          this.curX = event.touches[0].pageX;
          this.curY = event.touches[0].pageY;
          event.preventDefault();
          bScroll = true;
        } else {
          // more than one finger touched so cancel
          this.touchCancel(event);
        }
      } else if (event.pageX) { // mouse event
        this.curX = event.pageX;
        this.curY = event.pageY;
        bScroll = true;
      } else { // old IE mouse event
        this.curX = event.clientX + document.body.scrollLeft;
        this.curY = event.clientY + document.body.scrollTop;
        bScroll = true;
      }
      if (bScroll) {
        var X = (this.startX-this.curX);
        var Y = (this.startY-this.curY);
        this.startX = this.curX;
        this.startY = this.curY;
        this.scroll(X, Y);
      }
    }
  },
  touchEnd: function (event) {
    if (this.start) {
      //this.touchMove(event);
      this.transition();
      this.start = false;
    }
    this.startX = 0;
    this.startY = 0;
    this.curX = 0;
    this.curY = 0;
  },
  wheel: function(event) { // mouse wheel event
    var X = 0, Y = 0;
    if (event.wheelDeltaX !== undefined) {
      X = event.wheelDeltaX / 6;
      Y = event.wheelDeltaY / 6;
    } else if(event.wheelDelta !== undefined) {
      X = Y = event.wheelDelta / 6;
    } else if (event.detail !== undefined) {
      X = Y = -event.detail * 6;
    } else {
      return;
    }
    this.scroll(0, Y);
  },
  transition: function() {
    this.content.style.top = (parseInt(this.content.style.top, 10) + this.lastMove*10) + 'px';
    this.content.style.transition = "top 1s ease-out";
  },
  transitionend: function(event) {
    if (parseInt(this.content.style.top, 10) > 0) {
      this.content.style.top = '0px';
    }
    if (Math.abs(parseInt(this.content.style.top, 10)) > (parseInt(this.content.offsetHeight, 10) - parseInt(this.object.offsetHeight, 10))) {
      this.content.style.top = - (parseInt(this.content.offsetHeight, 10) - parseInt(this.object.offsetHeight, 10));
    }
    this.content.style.transition = "none";
  },
  scroll: function(X, Y) {
    if (this.content.offsetWidth > this.object.offsetWidth) {
      this.content.style.left = parseInt(this.content.style.left, 10) - X + 'px';
    }
    if ((this.content.offsetHeight > this.object.offsetHeight) &&
        (((Math.abs(parseInt(this.content.style.top, 10)) < (parseInt(this.content.offsetHeight, 10) - parseInt(this.object.offsetHeight, 10))) && Y>0) ||
         ((parseInt(this.content.style.top, 10) < 0) && Y<0))) {
      this.lastMove = parseInt(this.content.style.top, 10);
      this.content.style.top = parseInt(this.content.style.top, 10) - Y + 'px';
      this.lastMove = parseInt(this.content.style.top, 10) - this.lastMove;
    }
    this.scroller.style.top = Math.round((this.scrollbar.offsetHeight ? this.scrollbar.offsetHeight : this.object.offsetHeight) * (Math.abs(parseInt(this.content.style.top, 10)) / parseInt(this.content.offsetHeight, 10))) + "px";
  },
  resize: function() {
    var offsetHeight = (this.scrollbar.offsetHeight ? this.scrollbar.offsetHeight : this.object.offsetHeight);
    this.content.style.width = this.object.offsetWidth-10 + "px";
    this.scroller.style.height = Math.min(Math.max(Math.round(offsetHeight * (this.object.offsetHeight / this.content.offsetHeight)), 8), offsetHeight-6) + "px";
    this.scroller.style.top = Math.round(offsetHeight * (Math.abs(parseInt(this.content.style.top, 10)) / parseInt(this.content.offsetHeight, 10))) + "px";
    if (this.content.offsetHeight <= this.object.offsetHeight) {
      this.scroller.style.display = "none";
    } else {
      this.scroller.style.display = "block";
    }
  },
  handle: function(event) {
    event || (event = window.event);
    switch (event.type) {
      case 'mousedown':
      case 'touchstart': this.touchStart(event); break;
      case 'mousemove':
      case 'touchmove': this.touchMove(event); break;
      case 'mouseup':
      case 'touchend':
      case 'mouseout':
      case 'touchcancel': this.touchEnd(event); break;
      case 'mousewheel':
      case 'DOMMouseScroll': this.wheel(event); break;
      case 'transitionend':
      case 'webkitTransitionEnd':
      case 'oTransitionEnd':
      case 'MSTransitionEnd': this.transitionend(event); break;
      case 'onorientationchange':
      case 'resize': this.resize(); break;
    }
    if (event.preventDefault) {
      event.preventDefault();
    }
  },
  bind: function(ev, el) {
    var self = this;
    if (window.addEventListener !== undefined) { // W3C
      var handler = (el || window).addEventListener(ev,
        function(event){
          self.handle(event);
        });
    } else if (window.attachEvent) { // old IE
      (el || window).attachEvent('on'+ev, function(event){self.handle(event);});
    } else { // fallback
      (el || window)['on'+ev] = function(event){self.handle(event);};
    }
  },

  unbind: function (ev, el) {
    if (window.removeEventListener !== undefined) { // W3C
      (el || this.scroller).removeEventListener(ev, this, !!bubble);
    }
  },
  init: function(scroll) {
    this.object = scroll;
    if (scroll.style.position === "") {
      scroll.style.position = "relative";
    }
    scroll.style.overflow = "hidden";
    this.content = document.createElement("div");
    while (scroll.hasChildNodes()) {
      this.content.appendChild(scroll.removeChild(scroll.firstChild));
    }
    scroll.appendChild(this.content);
    this.content.style.position = "absolute";
    this.content.style.left = '0px';
    this.content.style.top = '0px';
    //create scrollbar
    this.scrollbar = document.createElement("div");
    this.scrollbar.style.position = "absolute";
    this.scrollbar.style.width = "7px";
    this.scrollbar.style.bottom = "2px";
    this.scrollbar.style.top = "2px";
    this.scrollbar.style.right = "1px";
    this.object.appendChild(this.scrollbar);
    this.scroller = document.createElement("div");
    this.scroller.style.position = "absolute";
    this.scroller.style.backgroundColor = "#7A7A7A";
    this.scroller.style.width = "100%";
    this.scroller.style.borderRadius = "3px";
    this.scroller.style.opacity = 0.5;
    this.scrollbar.appendChild(this.scroller);
    this.resize();
    this.isTouchDevice = (window.ontouchstart !== undefined);
    if (this.isTouchDevice) {
      this.bind('touchstart', scroll);
      this.bind('touchmove', scroll);
      this.bind('touchend', scroll);
      this.bind('touchcancel', scroll);
    } else {
      this.bind('mousedown', scroll);
      this.bind('mousemove', scroll);
      this.bind('mouseup', scroll);
      this.bind('mouseout', scroll);
      this.bind('mouseover', scroll);
      this.bind('mousewheel');
      this.bind('DOMMouseScroll');
    }
    var style = document.documentElement.style;
    if (style.transition !== undefined) {
      this.bind('transitionend');
    }
    this.bind('resize');
  }
};

var i, els = document.querySelectorAll('div.scroll');
for(i=0; i<els.length; i++) {
  els[i].style.border = '1px solid #777';
  els[i].style.height = '200px';
  els[i].style.position = "relative";
  els[i].style.overflowY = 'auto';
  // change scroll direction
  /*var scroll = new MyScroll();
  scroll.init(els[i]);*/
}

Kontakt

Udo Schmal
Udo Schmal

Udo Schmal
Softwareentwickler
Ellerndiek 26
24837 Schleswig
Schleswig-Holstein
Germany




+49 4621 9785538
+49 1575 0663676
+49 4621 9785539
SMS
WhatsApp

Google Maps Profile
Instagram Profile
vCard 2.1, vCard 3.0, vCard 4.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.1.217
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Sun, 10. Nov 2024 00:22:34

Development Info

Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64

System Info

OS:
Ubuntu 22.04.5 LTS (Jammy Jellyfish)

Hardware Info

Model:
Hewlett-Packard HP Pavilion dm4 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s),  MHz