Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Desplazamiento horizontal por un DIV

Estas en el tema de Desplazamiento horizontal por un DIV en el foro de Javascript en Foros del Web. Buenas a todos, tengo una dudilla que seguro muchos sabréis como resolverla. Quiero conseguir el efecto de moverme horizontalmente por un DIV como lo haríamos ...
  #1 (permalink)  
Antiguo 10/05/2016, 02:11
Avatar de homeres  
Fecha de Ingreso: agosto-2015
Ubicación: Murcia
Mensajes: 45
Antigüedad: 8 años, 7 meses
Puntos: 3
Pregunta Desplazamiento horizontal por un DIV

Buenas a todos,

tengo una dudilla que seguro muchos sabréis como resolverla.

Quiero conseguir el efecto de moverme horizontalmente por un DIV como lo haríamos desde un móvil, me explico, en el móvil pulsaríamos con el dedo y arrastraríamos hacia derecha o hacia izquierda para desplazarnos horizontalmente, pues quiero hacer eso mismo pero con el ratón, ¿me echáis un cable por favor?

Un saludo!
__________________
Web developer - I+D Hefame Informática
  #2 (permalink)  
Antiguo 11/05/2016, 12:42
Avatar de 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.
  #3 (permalink)  
Antiguo 12/05/2016, 03:51
Avatar de homeres  
Fecha de Ingreso: agosto-2015
Ubicación: Murcia
Mensajes: 45
Antigüedad: 8 años, 7 meses
Puntos: 3
Respuesta: Desplazamiento horizontal por un DIV

Cita:
Iniciado por marlanga Ver Mensaje
[url]https://jsfiddle.net/marlanga/aqdfL4gb/2[/url]

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.
¡Justo lo que necesitaba! ¡Lo has clavado!
__________________
Web developer - I+D Hefame Informática
  #4 (permalink)  
Antiguo 12/05/2016, 04:08
Avatar de homeres  
Fecha de Ingreso: agosto-2015
Ubicación: Murcia
Mensajes: 45
Antigüedad: 8 años, 7 meses
Puntos: 3
Respuesta: Desplazamiento horizontal por un DIV

Solo una cosa, he probado el código pero no me funciona el javascript, no están saltando los listener... a que puede deberse?

EDITO: Faltaba el onLoad del body.... una tonteria jeje ya funciona todo, gracias de nuevo marlanga te has coronao!
__________________
Web developer - I+D Hefame Informática

Última edición por homeres; 12/05/2016 a las 04:29

Etiquetas: desplazamiento, horizontal
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:54.