JavaScript Scroller

iScroll alternative mit Internet Explorer Unterstützung

Beispiel: ohne großes Framework

Bei meiner Suche nach einer Scroll Komponente bin ich auf alle möglichen Probleme gestoßen die Meisten unterstützen keine älteren Internet Explorer, wieso eigentlich nicht, so riesig sind die Unterschiede doch gar nicht. 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 und alle mir noch zur Verfügung stehenden Internet Explorer und das sind alle ab Version 5.5! 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! Da das gleiten durch CSS3 transition realisiert ist wird es natürlich nicht von den alten Internet Explorer unterstützt!

HTML:
scroll.htm HTML (4,12 kByte) 05.10.2014 23:30

<html>
  <head>
    <title>scroll</title>
    <script type="text/javascript" src="jsScroll.js"></script>
  </head>
  <body>
    <div class="scroll" style="border:1px solid #777; height:200px;">
      <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>
  </body>
</html>
Javascript:
jsScroll.js JavaScript (9,66 kByte) 04.03.2014 18:34
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.WebkitTransition = "top 1s ease-out";
    this.content.style.MozTransition = "top 1s ease-out";
    this.content.style.MsTransition = "top 1s ease-out";
    this.content.style.OTransition = "top 1s ease-out";
    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.WebkitTransition = "none";
    this.content.style.MozTransition = "none";
    this.content.style.MsTransition = "none";
    this.content.style.OTransition = "none";
    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.scroller.style.MozOpacity = 0.5;
    this.scroller.style.filter = "alpha(opacity=" + (0.5*100) + ")";
    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');
    } else if (style.webkitTransition !== undefined) {
      this.bind('webkitTransitionEnd');
    } else if (style.otransition != undefined) {
      this.bind('otransitionend');
    } else if (style.MSTransition != undefined) {
      this.bind('MSTransitionEnd');
    }
    this.bind('resize');
  }
};

function initScroll() {
  var i, tags = document.getElementsByTagName('div');
  for(i=0; i<tags.length; i++) {
    var sClass = " "+tags[i].className+" ";
    if (sClass.indexOf(" scroll ") !== -1) {
      var scroll = new MyScroll();
      scroll.init(tags[i]);
    }
  }
}

if (window.addEventListener !== undefined) { // W3C
  window.addEventListener('load', function() {initScroll();});
} else if (window.attachEvent) { // old IE
  window.attachEvent('onload', function() {initScroll();});
} else { // fallback
  window.onload = function() {initScroll();};
}

Autor: , veröffentlicht: , letzte Änderung:

Kontakt

Copyright / License of sources

Copyright (c) 2007-2017, Udo Schmal <udo.schmal@t-online.de>

Permission to use, copy, modify, and/or distribute the software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Service Infos

CMS Info
UDOs Webserver

0.3.1.24

All in one Webserver

Udo Schmal

Sa, 21 Okt 2017 00:30:10
Development Info
Lazarus LCL 1.9.0.0

Free Pascal FPC 3.1.1

OS:Win64, CPU:x86_64
Hardware Info
Precision WorkStation T3500

Intel(R) Xeon(R) CPU W3530 @ 2.80GHz

x86_64, 1 physical CPU(s), 4 Core(s), 8 logical CPU(s), 2800 MHz