SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    document.all.(variable???).style.top=10

    I have an array of pictures...
    pic0,pic1,pic2,pic3

    I want to change the the position of each pic using a loop.

    for(i=0;i<(picarray.length/2);i++){
    document.all."pic"+i.style.top=10}

    Wont work, although when pic0,pic1 etc is written in it works fine.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: document.all.(variable???).style.top=10

    Originally posted by Markdidj
    I have an array of pictures...
    pic0,pic1,pic2,pic3

    I want to change the the position of each pic using a loop.

    for(i=0;i<(picarray.length/2);i++){
    document.all."pic"+i.style.top=10}

    Wont work, although when pic0,pic1 etc is written in it works fine.
    1. Your first problem is that you're using document.all. That's fine if your site is IE-only. Otherwise use the standard document.getElementById(), which will target IE5+, NS6+ (all Gecko-based browsers like Mozilla, etc.), and Konqueror on Linux.

    2. Here's your fix.

    Code:
    <script language="javascript" type="text/javascript">
    var picarray = new Array("pic0", "pic1", "pic2", "pic3");
    for(i=0; i<(picarray.length/2); i++){
     document.getElementById(picarray[i]).style.top=10;
    }
    </script>
    This will move all images 10 pixels from the top of the browser window.

    --Vinnie

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Nice one vgarcia....

    Thanks.....
    That's a handy sentance.
    Should change the whole site now.....
    again!!
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you said that only pic0 and pic1 work. that's because your cutting your array in half with "length/2".

    Also if you want your script to work in any 4+ browser, you can do it like this...

    Code:
    pics = ['pic0','pic1','pic2','pic3'];
    
    for(i=0; i<pics.length; i++){
    	if(document.getElementById){
    		img = document.getElementById(pics[i]).style;
    	}else if(document.all){
    		img = document.all[pics[i]].style;
    	}else if(document.layers){
    		img = document.layers[pics[i]];
    	}
    	img.top = 10;
    }
    travis

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    podlob, his fix was for Images, not Layers.

    --Vinnie

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Thanks....This works.....

    for(i=0;i<(drmltn.length/2);i++){
    document.getElementById("drum"+i).style.top=(picy+((drmltn[i*2])*multiplier)-10)
    document.getElementById("drum"+i).style.left=(picx+((drmltn[(i*2)+1])*multiplier)-8)}
    for(i=0;i<(ddjltn.length/2);i++){
    document.getElementById("didj"+i).style.top=(picy+((ddjltn[i*2])*multiplier)-10)
    document.getElementById("didj"+i).style.left=(picx+((ddjltn[(i*2)+1])*multiplier)-8)}


    As my Array was built in a loop, I wanted to reposition in a loop

    I needed...
    document.all."drum"+i.style.top=(picy+((drmltn[i*2])*multiplier)-10)

    It didnt work, but this one understands "drum"+i.....
    document.getElementById("drum"+i).style.top=(picy+((drmltn[i*2])*multiplier)-10)

    as for the /2, how do I get a loop to go in steps of 2?
    the loop is to put buttons on a zoomable map, positions got from an array of numbers (100,150,200,50)
    so I divided the array by 2,(2), and went in steps of the loop*2, therefore the loop goes 0,2(gets y position) and the x are got from 0+1,2+1

    If I could go in steps of two that would make it so much easier!!
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •