Foros del Web » Programando para Internet » Javascript »

simplificar muchos if else

Estas en el tema de simplificar muchos if else en el foro de Javascript en Foros del Web. Hola chicos tengo actualmente una duda, como se darán cuenta no programo mucho pero quisiera un consejo para esta opción. los contextualizo, lo que intento ...
  #1 (permalink)  
Antiguo 24/10/2011, 15:50
 
Fecha de Ingreso: octubre-2011
Ubicación: Medellín
Mensajes: 3
Antigüedad: 12 años, 5 meses
Puntos: 0
Pregunta simplificar muchos if else

Hola chicos
tengo actualmente una duda, como se darán cuenta no programo mucho pero quisiera un consejo para esta opción.
los contextualizo, lo que intento hacer que un menú cambie según la zona de la pagina en la que este, aunque actualmente son solo colores, es para 4 item si lo hago para 10 serian demasiadas lineas de código no?; hay manera de simplificarlo con alguna funcion o ... bueno aqui les dejo lo que he usado

window.onload = initAll;
function initAll()
{

document.getElementById("item1").style.background= "#960a00";
document.getElementById("item2").style.background= "#fff";
document.getElementById("item3").style.background= "#fff";
document.getElementById("item4").style.background= "#fff";
document.getElementById("item1").style.color="#fff ";
document.getElementById("item2").style.color="#960 a00";
document.getElementById("item3").style.color="#960 a00";
document.getElementById("item4").style.color="#960 a00";
}
function checkPosition()
{
var posx = $(window).scrollLeft();

if (posx > 0 && posx <= 1450)
{


document.getElementById("item1").style.background= "#960a00";
document.getElementById("item2").style.background= "#fff";
document.getElementById("item3").style.background= "#fff";
document.getElementById("item4").style.background= "#fff";
document.getElementById("item1").style.color="#fff ";
document.getElementById("item2").style.color="#960 a00";
document.getElementById("item3").style.color="#960 a00";
document.getElementById("item4").style.color="#960 a00";

}
else if (posx > 1450 && posx <= 3400)
{

document.getElementById("item1").style.background= "#fff";
document.getElementById("item2").style.background= "#960a00";
document.getElementById("item3").style.background= "#fff";
document.getElementById("item4").style.background= "#fff";
document.getElementById("item1").style.color="#960 a00";
document.getElementById("item2").style.color="#fff ";
document.getElementById("item3").style.color="#960 a00";
document.getElementById("item4").style.color="#960 a00";
}
else if (posx > 3400 && posx <=5440)
{

document.getElementById("item1").style.background= "#fff";
document.getElementById("item2").style.background= "#fff";
document.getElementById("item3").style.background= "#960a00";
document.getElementById("item4").style.background= "#fff";
document.getElementById("item1").style.color="#960 a00";
document.getElementById("item2").style.color="#960 a00";
document.getElementById("item3").style.color="#fff ";
document.getElementById("item4").style.color="#960 a00";
}
else if (posx > 4840 && posx <=6700)
{

document.getElementById("item1").style.background= "#fff";
document.getElementById("item2").style.background= "#fff";
document.getElementById("item3").style.background= "#fff";
document.getElementById("item4").style.background= "#960a00";
document.getElementById("item1").style.color="#960 a00";
document.getElementById("item2").style.color="#960 a00";
document.getElementById("item3").style.color="#960 a00";
document.getElementById("item4").style.color="#fff ";

}

}

$(window).scroll( checkPosition );
  #2 (permalink)  
Antiguo 25/10/2011, 06:44
Avatar de Aijoona
Colaborador
 
Fecha de Ingreso: mayo-2011
Ubicación: Buenos Aires
Mensajes: 779
Antigüedad: 12 años, 11 meses
Puntos: 343
Respuesta: simplificar muchos if else

1. Utiliza el highlight asi el código queda formateado.
2. Si usas jQuery para algunas cosas, porque no para todo?

-----------------

El condicional no vale la pena simplificarlo, pero podes encapsular la lógica utilizada:

Código Javascript:
Ver original
  1. window.onload = initAll;
  2.  
  3. function initAll() {
  4.     active('#item1');
  5. }
  6.  
  7. function active(selector) {
  8.     $('#item1, #item2, #item3, #item4').css({
  9.         'background-color': '#fff',
  10.         'color': "#960a00"
  11.     });
  12.  
  13.     $(selector).css({
  14.         'background-color': '#960a00',
  15.         'color': "#fff"
  16.     });
  17. }
  18.  
  19. function checkPosition() {
  20.     var posx = $(window).scrollLeft();
  21.  
  22.     if (posx > 0 && posx <= 1450) {
  23.         active('#item1');
  24.     } else if (posx > 1450 && posx <= 3400) {
  25.         active('#item2');
  26.     } else if (posx > 3400 && posx <= 5440) {
  27.         active('#item3');
  28.     } else if (posx > 4840 && posx <= 6700) {
  29.         active('#item4');
  30.     }
  31.  
  32. }
  33.  
  34. $(window).scroll(checkPosition);
__________________
blog | @aijoona

Etiquetas: menubar
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 06:49.