Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/08/2011, 05:31
Avatar de ceSharp
ceSharp
 
Fecha de Ingreso: octubre-2008
Ubicación: Madrid
Mensajes: 495
Antigüedad: 15 años, 6 meses
Puntos: 66
Respuesta: Cambiar propiedades de una capa (style) al hacer scroll

hola shiryu,
a lo mejor esto te vale. He buscado un poco por allí, un poco por alla, montado una función para mover un div por debajo de otro y al final me ha salido esto:

Código Javascript:
Ver original
  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head>
  3.     <title>Untitled Page</title>
  4.     <script type="text/javascript">
  5.     //la variable s detecta si hemos dado a la rueda hacia arriba o hacia abajo
  6.     var s;
  7. function handle(delta) {
  8.     if (delta < 0)
  9.         s = "down";
  10.     else
  11.         s = "up";
  12.     moverDiv();
  13. }
  14.  
  15. function wheel(event){
  16.     var delta = 0;
  17.     if (!event) event = window.event;
  18.     if (event.wheelDelta) {
  19.         delta = event.wheelDelta/120;
  20.         if (window.opera) delta = -delta;
  21.     } else if (event.detail) {
  22.         delta = -event.detail/3;
  23.     }
  24.     if (delta)
  25.         handle(delta);
  26. }
  27. function moverDiv()
  28. {
  29.     if(s=='down')
  30.     {
  31.         var bajar = parseInt(document.getElementById('rojo').style.top);
  32.         if(isNaN(bajar))
  33.         bajar=0;
  34.         var topeAmarillo = parseInt(document.getElementById('amarillo').style.top);
  35.         if(isNaN(topeAmarillo))
  36.         topeAmarillo=0;
  37.         //si el top de rojo es menor que el del amarillo lo bajamos 10px
  38.         if(bajar < topeAmarillo)
  39.         document.getElementById('rojo').style.top = bajar + 10;
  40.        
  41.     }
  42.     else
  43.     {
  44.         var subir = parseInt(document.getElementById('rojo').style.top);
  45.         if(isNaN(subir))
  46.         subir=0;
  47.         var topePagina=0;
  48.         //si no está al principio de la página no lo subimos 10px
  49.         if(subir!=topePagina)
  50.         document.getElementById('rojo').style.top = subir - 10;
  51.        
  52.     }
  53. }
  54.  
  55. /* Initialization code. */
  56. if (window.addEventListener)
  57.     window.addEventListener('DOMMouseScroll', wheel, false);
  58.     window.onmousewheel = document.onmousewheel = wheel;
  59.     </script>
  60. </head>
  61. <body>
  62.     <form id="form1" >
  63.     <div>
  64.     <div id="amarillo" style="z-index:2;top:180px;background-color:yellow;position:absolute;width:200px;height:100px"></div>
  65.     <div id="rojo" style="z-index:1;left:60px;background-color:Red;position:absolute;width:100px;height:200px"></div>
  66.    
  67.     </div>
  68.     </form>
  69. </body>
  70. </html>

si te vale esta página de ejemplo la única función que hay que 'tunear' sería la de moverDiv(), el resto es para detectar el movimiento de la rueda.

saludos.