Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/07/2016, 09:33
ElionTDA
 
Fecha de Ingreso: agosto-2015
Ubicación: Burgos, España
Mensajes: 17
Antigüedad: 9 años, 7 meses
Puntos: 0
Problema con javascript. getElementById("blabla").style."algo".

Buenas, me surge un problema al hacer un cambio en el css haciendo uso de js, y no entiendo el porque.

El objeto que quiero cambiar tiene este css:
#img1_1{
background-image: url("img/img1_1.png");
background-size: 200px 200px;
background-repeat: no-repeat;
width: 200px;
height: 200px;
position: absolute;
z-index: 1;
margin-left: 0px;
left: 0px;
bottom: 0%;
}

Y la función donde lo cambio:
function blablab(elemento, inicio = 0, fin = 800){
var objeto;
var aux;

objeto = document.getElementById(elemento);

objeto.style.transition = "all 1s";
objeto.style.transitionTimingFunction = "linear";

if(objeto.style.left == ""){
aux = 0;
}else{
aux = objeto.style.left;
aux = aux.substr(0,aux.length - 2);
aux = parseInt(aux);
}


if(aux >= fin){
dir = "izq";
}
if(aux <= inicio){
dir = "der";
}

if(dir == "der"){
resp = aux + 90;
} else if (dir == "izq"){
resp = aux - 90;
}

objeto.style.left = resp + "px";
}

He hecho una chapuza, para que no me diera error, que es ese:
if(objeto.style.left == ""){
aux = 0;
}else{
aux = objeto.style.left;
aux = aux.substr(0,aux.length - 2);
aux = parseInt(aux);
}

Pero no entiendo por que la primera vez, que tiene en el left del css 0px, no me lo detecta la primera pasada, pero a partir de cambiarlo con js, las siguientes si me lo detecta.

Si alguien puede ayudarme se agradecería mucho.

Soy un newbie en todo esto de js.

Un saludo.