April 20, 2011

Dynamically Position Elements

by blackbird

Have you ever had to position elements in flash or in a javascript application to look something like iOS’s home-screen?
This script will help you achieve that with minimal customization, it even has scrollable paging.

[code lang="js" inline="yes"]var vspacing = hspacing; //vertical spacing in between each icon (set to same as hspacing)

var columns=4; //number of icons in a row

var rows =4; //number of rows per "page"

//within some kind of loop assuming count is the iterator (base 0)

icon.x=count*(icon.width+hspacing)-(Math.floor(count/columns)*(columns*(icon.width+hspacing)))+(Math.floor(count/(columns*rows))*(columns*(icon.width+hspacing)));

icon.y=(Math.floor(count/columns))*(icon.height+vspacing)-((Math.floor(count/(columns*rows)))*(rows*(icon.height+vspacing)));[/code]