简体   繁体   中英

Changing a dom element's position after load without jquery

hey i have some code that sets an elements style.width, height, and position but its not working and I assumed it was because it was'nt in the head, but I want to change the css on like a mouseclick event so tabs will slide out and stuff but I'd rather not use jquery is there a way to do this?

edit: ok so Iv got the width and height problem solved exact it doesnt seem to respond when I try and position it

here's my code

for (i=1; i<=tab_array.length-1; i++)
{

    if (i==3)
    {
        document.getElementById("_"+i).style.cssText='height:'+Math.floor(p(100,gheight))+'px;'+
                                 'width:' +Math.floor(p(80,gwidth ))+'px;'+
                                 'background:black;'+
                                 'postion:absolute;'+
                                 'left:'+Math.floor(p(tab_array.across,gwidth)-gwidth)+'px;'
                                 ;
    }

    else
    {
        document.getElementById("_"+i).style.cssText='height:'+Math.floor(p(80,gheight))+'px;'+
                                 'width:' +Math.floor(p(80,gwidth ))+'px;';
    }               
}

Try something like:

function E(e){
  return document.getElementById(e);
}
function changeSize(id, height, width, interval){
  var es = E(id).style, h = parseInt(height), w = parseInt(width), f = Math.floor(h/w), n = parseInt(interval), i = 0;  
  var sit = setInterval(function(){
    i++; es.cssText = 'height:'+i*f+'px; width:'+i+'px;';
    if(i === w){
      es.cssText = 'height:'+h+'px; width:'+w+'px;'; clearInterval(sit); 
    }
  }, n);
}
changeSize('box', 100, 50, 20);

For the working example, visit http://jsfiddle.net/PHPglue/X6TMv/ . I did not add easing, which jQuery handles quite effectively though. I made the code to take Strings or Numbers from height on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM