Ver Mensaje Individual
  #2 (permalink)  
Antiguo 11/05/2016, 12:42
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: Desplazamiento horizontal por un DIV

https://jsfiddle.net/marlanga/aqdfL4gb/2

Código Javascript:
Ver original
  1. var Scroller = function(id) {
  2.     this.parent = document.getElementById(id);
  3.   this.child = this.parent.firstChild;
  4.  
  5.   this.clicked = false;
  6.   this.x = 0;
  7.  
  8.   var up = function(evt) {
  9.     if (this.clicked) {
  10.         this.clicked = false;
  11.     }
  12.   }
  13.  
  14.   var down = function(evt) {
  15.     this.clicked = true;
  16.     this.x = evt.x;
  17.   }
  18.  
  19.   var move = function(evt) {
  20.     if (this.clicked) {
  21.         var dx = this.x - evt.x;
  22.       this.x = evt.x;
  23.       this.parent.scrollLeft += dx;
  24.     }
  25.   }
  26.  
  27.   this.child.addEventListener('mousedown', down.bind(this));
  28.   this.child.addEventListener('mouseup', up.bind(this));
  29.   this.parent.addEventListener('mouseout', up.bind(this));
  30.   this.child.addEventListener('mousemove', move.bind(this));
  31. }
  32.  
  33. var scroll = new Scroller('slider');

Código HTML:
Ver original
  1. <div id="slider"><div></div></div>

Código CSS:
Ver original
  1. div#slider {width:400px; height:100px; border: 2px solid black; overflow:hidden;}
  2. div#slider > div {width:2000px; height:100px;background: linear-gradient(to right, #ffffff 0%,#ff0000 100%);}

Me he probado y me he gustado.