www.webdeveloper.com
Results 1 to 4 of 4

Thread: best way to shuffle through z - indexes?

  1. #1
    Join Date
    Apr 2006
    Posts
    29

    best way to shuffle through z - indexes?

    Hi,
    I have three images piled on top of each other and their z indexs are unique 1 , 2 , 3

    If I want to put top card (3) to the bottom of the pack whats the best way do that? If I set its z index to 1 that now means i have two items on index 1.

    How to make it so that what was 1 is now 2 and what was 2 is now 3?

    .... hope that makes sense.

    Thanks
    Aidan

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,667
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <style type="text/css">
    /*<![CDATA[*/
    
    #pack {
      position:relative;width:200px;height:200px;
    }
    
    #pack IMG{
      position:absolute;left:0px;top:0px;width:200px;height:200px;
    }
    
    /*]]>*/
    </style></head>
    
    <body>
    <input type="button" name="" value="Top Card to Back" onmouseup="BackOfPack('pack',3,1);"/>
    
    <div id="pack" >
     <img src="http://www.vicsjavascripts.org/StdImages/1.gif" style="z-Index:1"/>
     <img src="http://www.vicsjavascripts.org/StdImages/2.gif" style="z-Index:2" />
     <img src="http://www.vicsjavascripts.org/StdImages/3.gif" style="z-Index:3" />
    </div>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    
    function BackOfPack(id,bz,nz){
     var imgs=document.getElementById(id).getElementsByTagName('IMG');
     for (var z0=0;z0<imgs.length;z0++){
      imgs[z0].style.zIndex=imgs[z0].style.zIndex==bz?nz:imgs[z0].style.zIndex*1+1;
     }
    }
    
    /*]]>*/
    </script></body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Apr 2006
    Posts
    29
    Hello Vic,

    Is that an if statment you have going on there?

    Code:
    imgs[z0].style.zIndex=imgs[z0].style.zIndex==bz?nz:imgs[z0].style.zIndex*1+1;
    Any chance you could write that in the standard way?

    Are incrementing the index by 1 for each or something?

    Thanks
    Aidan

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    A variant with a random permutation of indexes :

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title></title>
    <style type="text/css">
    #pack {position:relative;width:200px;height:200px;}
    #pack IMG{position:absolute;left:0px;top:0px;width:200px;height:200px;}
    </style>
    </head>
    <body>
    <input type="button" name="" value="Schuffle zIndex" onmouseup="schuffle('pack');"/>
    
    <div id="pack" >
     <img src="http://www.vicsjavascripts.org/StdImages/1.gif" style="z-Index:1"/>
     <img src="http://www.vicsjavascripts.org/StdImages/2.gif" style="z-Index:2" />
     <img src="http://www.vicsjavascripts.org/StdImages/3.gif" style="z-Index:3" />
    </div>
    <script type="text/javascript">
    var ndx=[1,2,3];
    function schuffle(id){var i=0,ndxCln=ndx.slice(0),ims=document.getElementById(id).getElementsByTagName('IMG');
        while(ndxCln.length) ims[i++].style.zIndex=ndxCln.splice(Math.floor(Math.random()*ndxCln.length),1)[0];
    }
    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles