`
txf2004
  • 浏览: 7043474 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

[转载]Build a Fluid Website Layout

阅读更多

A fluid web layout uses 100% width (and height) of the browser, floating all the contained elements into certain positions. This is opposed to fix-width layout where contents remain fixed no matter what the browser size is.

This technique is popular in HTML/CSS websites, but this tut will show you how to create a similar fluid layout effect in Flash. Every element will reposition itself with ease animation when the browser resizes.

PG

Author: Makzan

My name is Makzan. I live in Hong Kong, China. I have been a flash developer for 10 years. I enjoy Web Designing and Flash developing. Recently I fall in love with flash/iPhone multiplayer game developing with socket server. You can follow me on Twitter: @makzan or visit my portfolio at FlashDen

Introduction

During the following steps we'll create ActionScript 3 classes which make our flash website fluid. All our display symbols will retain their alignment when the Flash is resized.

The ActionScript classes created in this tutorial can be easily reused in different projects.

Step 1: Background Information of Fluid Layout

As shown in the image below, all the elements which float according to the browser size will be referred to as "fluid objects".

Step 2: Fluid Object Coordinates

Each fluid object will contain alignment parameters. The parameters store the x, y, x offset, y offset value of the object to indicate how it aligns.

Assigning x and y to 0 will make the fluid object align to the top left corner. Assigning x and y to 1 will make the fluid object align to the bottom right corner. Therefore, assignign x and y value between 0 and 1 will float the object at a percentage of the width and height of the browser.

The offset X and Y offset the position of the fluid objects while they align. Offsetting useful when positioning an object whose alignment point is not at the center. The offset is also useful for making margins on the alignment.

Step 3: Create a Directory to Store the Classes

Create a directory called "FluidLayout" in the working directory. This directory will store all classes which relate to the fluid layout classes.

It's a good habit to put the ActionScript class files in directories by category. For example, fluid layout classes will be placed in the "FluidLayout" folder in this case.

Please note that all the directory names, file names and codes are case sensitive.

Step 4: New ActionScript File (FluidObject.as)

Open a new ActionScript file named "FluidObject.as". Save this ActionScript file into the "FluidLayout" directory.

The class FluidObject will store the alignment parameters of the symbols and reposition the symbols when the browser resizes.

Step 5: The Class Skeleton

Let's start coding the FluidObject.as now.

view plaincopy to clipboardprint?

  1. package FluidLayout {
  2. /* Add import classes here */
  3. public class FluidObject {
  4. /* Declare instance variables here */
  5. /* Constructor of the class */
  6. public function FluidObject(target:DisplayObject,paramObj:Object)
  7. {
  8. }
  9. /* Function that repositions the monitored object */
  10. protected function reposition():void
  11. {
  12. }
  13. /* Function that is called when the RESIZE event is fired */
  14. protected function onStageResize(e):void
  15. {
  16. }
  17. }
  18. }
	package FluidLayout {

		/* Add import classes here */

		public class FluidObject {

			/* Declare instance variables here */

			/* Constructor of the class */
			public function FluidObject(target:DisplayObject,paramObj:Object)
			{
			}

			/* Function that repositions the monitored object */
			protected function reposition():void
			{
			}

			/* Function that is called when the RESIZE event is fired */
			protected function onStageResize(e):void
			{
			}
		}
	}

Step 6: Importing Required Classes

Add the following code where you see: /* Add import classes here */

view plaincopy to clipboardprint?

  1. /* class needed on resize Event */
  2. import flash.events.Event;
  3. /* classes needed for MovieClip and DisplayObject */
  4. import flash.display.*;
	
	/* class needed on resize Event */
	import flash.events.Event;
	
	/* classes needed for MovieClip and DisplayObject */
	import flash.display.*;

Step 7: Declaring Instance Variables

There are three instance variables for this class:

  1. "_param" will store the alignment parameters.
  2. "_target" will point to the monitored symbol.
  3. "_stage" is a copy instance of the flash stage.

There is also a setter for "_param" to enable changing of the alignment parameters. Add the following code where you see: /* Declare instance variables here */

view plaincopy to clipboardprint?

  1. /* alignment parameters */
  2. protected var _param:Object;
  3. /* target object to be monitored */
  4. protected var _target:DisplayObject;
  5. /* stage instance of the flash document */
  6. protected var _stage:Stage;
  7. /* Setter for the alignment param */
  8. public function set param(value:Object):void {
  9. _param = value;
  10. this.reposition();
  11. }
			
	/* alignment parameters */
	protected var _param:Object;

	/* target object to be monitored */
	protected var _target:DisplayObject;

	/* stage instance of the flash document */
	protected var _stage:Stage;
	
	/* Setter for the alignment param */
	public function set param(value:Object):void {
		_param = value;
		this.reposition();
	}

Step 8: Implementing the Constructor

The constructor will initialize the target monitored symbol and store the given alignment parameters.

view plaincopy to clipboardprint?

  1. /* Constructor of the class */
  2. public function FluidObject(target:DisplayObject,paramObj:Object)
  3. {
  4. /* Assign the instance variables */
  5. _target = target;
  6. _param = paramObj;
  7. _stage = target.stage;
  8. /* add event handler for stage resize */
  9. _stage.addEventListener(Event.RESIZE, onStageResize);
  10. /* reposition the object with the alignment setting applied*/
  11. this.reposition();
  12. }
	/* Constructor of the class */
	public function FluidObject(target:DisplayObject,paramObj:Object)
	{
		/* Assign the instance variables */
		_target = target;
		_param = paramObj;
		_stage = target.stage;
		
		/* add event handler for stage resize */
		_stage.addEventListener(Event.RESIZE, onStageResize);
		
		/* reposition the object with the alignment setting applied*/
		this.reposition();
		
	}

Step 9: Implementing the Function: reposition()

The repositioning function is in charge of calculating the new x/y position of the monitored fluid object.

view plaincopy to clipboardprint?

  1. /* Function that reposition the monitored object */
  2. protected function reposition():void
  3. {
  4. /* get the current width and height of the flash document */
  5. var stageW = _stage.stageWidth;
  6. var stageH = _stage.stageHeight;
  7. /* update the x and y value of the monitored object */
  8. _target.x = (stageW * _param.x) + _param.offsetX;
  9. _target.y = (stageH * _param.y) + _param.offsetY;
  10. }
	/* Function that reposition the monitored object */
	protected function reposition():void
	{
		/* get the current width and height of the flash document */
		var stageW = _stage.stageWidth;
		var stageH = _stage.stageHeight;
		
		/* update the x and y value of the monitored object */
		_target.x = (stageW * _param.x) + _param.offsetX;
		_target.y = (stageH * _param.y) + _param.offsetY;
		
	}

Step 10: Implementing the Function: onStageResize

The onStageResize function is an event handler which is called when the browser resizes.

view plaincopy to clipboardprint?

  1. /* Function that is called when the RESIZE event is fired */
  2. protected function onStageResize(e):void
  3. {
  4. /* reposition the target */
  5. this.reposition();
  6. }
	/* Function that is called when the RESIZE event is fired */
	protected function onStageResize(e):void
	{
		/* reposition the target */
		this.reposition();
	}

Step 11: The Completed Class: FluidObject.as

The finished class FluidObject is finished in this step.

view plaincopy to clipboardprint?

  1. package FluidLayout {
  2. /* class needed on resize Event */
  3. import flash.events.Event;
  4. /* classes needed for MovieClip and DisplayObject */
  5. import flash.display.*;
  6. public class FluidObject {
  7. /* alignment parameters */
  8. protected var _param:Object;
  9. /* target object to be monitored */
  10. protected var _target:DisplayObject;
  11. /* stage instance of the flash document */
  12. protected var _stage:Stage;
  13. /* Setter for the alignment param */
  14. public function set param(value:Object):void {
  15. _param=value;
  16. this.reposition();
  17. }
  18. /* Constructor of the class */
  19. public function FluidObject(target:DisplayObject,paramObj:Object)
  20. {
  21. /* Assign the instance variables */
  22. _target = target;
  23. _param = paramObj;
  24. _stage = target.stage;
  25. /* add event handler for stage resize */
  26. _stage.addEventListener(Event.RESIZE, onStageResize);
  27. /* reposition the object with the alignment setting applied*/
  28. this.reposition();
  29. }
  30. /* Function that repositions the monitored object */
  31. protected function reposition():void
  32. {
  33. /* get the current width and height of the flash document */
  34. var stageW = _stage.stageWidth;
  35. var stageH = _stage.stageHeight;
  36. /* update the x and y value of the monitored object */
  37. _target.x = (stageW * _param.x) + _param.offsetX;
  38. _target.y = (stageH * _param.y) + _param.offsetY;
  39. }
  40. /* Function that is called when the RESIZE event is fired */
  41. protected function onStageResize(e):void
  42. {
  43. /* reposition the target */
  44. this.reposition();
  45. }
  46. }
  47. }
package FluidLayout {
	
	/* class needed on resize Event */
	import flash.events.Event;
	
	/* classes needed for MovieClip and DisplayObject */
	import flash.display.*;

	public class FluidObject {
		
		/* alignment parameters */
		protected var _param:Object;
		
		/* target object to be monitored */
		protected var _target:DisplayObject;
		
		/* stage instance of the flash document */
		protected var _stage:Stage;
		
		/* Setter for the alignment param */
		public function set param(value:Object):void {
			_param=value;
			this.reposition();
		}
		
		/* Constructor of the class */
		public function FluidObject(target:DisplayObject,paramObj:Object)
		{
			/* Assign the instance variables */
			_target = target;
			_param = paramObj;
			_stage = target.stage;
			
			/* add event handler for stage resize */
			_stage.addEventListener(Event.RESIZE, onStageResize);
			
			/* reposition the object with the alignment setting applied*/
			this.reposition();
			
		}
		
		/* Function that repositions the monitored object */
		protected function reposition():void
		{
			/* get the current width and height of the flash document */
			var stageW = _stage.stageWidth;
			var stageH = _stage.stageHeight;
			
			/* update the x and y value of the monitored object */
			_target.x = (stageW * _param.x) + _param.offsetX;
			_target.y = (stageH * _param.y) + _param.offsetY;
			
		}
		
		/* Function that is called when the RESIZE event is fired */
		protected function onStageResize(e):void
		{
			/* reposition the target */
			this.reposition();
		}

	}

}

Step 12: Time to Create a Flash File

Begin a new Flash Document with ActionScript 3.0 supported and call it "website.fla". Then set the Document class as "Website".

If a dialog box pops up with the message: "A definition for the document class could not be found in the classpath,..." just click "OK" to bypass it. We're going to create that class after drawing the graphic symbols.

The background image will be dark in this tutorial (I've put together my own space-like image using Photoshop). Therefore the background color of the flash document needs to set to black. Click Modify > Document to open the Document Properties dialog and change the background color.

Step 13: Draw the Title Symbol

There will be 5 flash symbols on the stage:

  • background
  • title
  • menu
  • middle content
  • footer

Let's make the title first. The aim of this tutorial is to create floating symbols in the fluid layout rather then creating fancy website components. The symbols will only contain a text field indicating the purpose only.

For the title symbol, there's a semi-transparent background. In order to fit different widths of the browser, the width of the background need to be large enough.

After having finished drawing the symbol, click Modify > Convert to Symbol (F8). Click the "Advanced" button to show detailed settings for the symbol.

Click "Export for ActionScript" to enable the ActionScript to access this symbol. Then find the "Class" field in the dialog and set the value to "Title" for the title symbol. This means that we've assigned a new Class called "Title" to this symbol. We can use this symbol later in the ActionScript.

Remember to name your symbol for easy recognition before you click OK. If a dialog box pops up with the message "A definition for this class could not be found in the classpath,..." again, just click "OK" to bypass it. Since we'll not add any behavior to the symbol, we'll let Flash create an empty class for us.

Step 14: Draw the Other Symbols

Delete the "title" symbol instance on stage because it will be created by ActionScript later.

We'll use the same method to draw "background", "menu", "middle content" and "footer". The class name of these symbols will be Background, Menu, Middle and Footer accordingly.

The background image can be downloaded from the source files. Other symbols are text-field only.

Step 15: Code the Document Class ActionScript

Create an ActionScript file and named as "Website.as"; this class file should be saved in the same directory as the website.fla file.

This class must also share the same name as that set in the Document Class (refer to Step 12). For example, the Document Class "Website" refers to "Website.as" in the same directory. This ActionScript class will be loaded right after the flash is loaded.

Here is the skeleton of the Document Class:

view plaincopy to clipboardprint?

  1. package {
  2. import flash.display.*;
  3. import FluidLayout.*;
  4. public class Website extends MovieClip{
  5. public function Website()
  6. {
  7. }
  8. }
  9. }
package {

	import flash.display.*;
	import FluidLayout.*;
	
	public class Website extends MovieClip{
	
		public function Website()
		{
		}	
	}
}

Step 16: Implementing the Constructor

view plaincopy to clipboardprint?

  1. package {
  2. import flash.display.*;
  3. import FluidLayout.*;
  4. public class Website extends MovieClip{
  5. public function Website()
  6. {
  7. /* Set the Scale Mode of the Stage */
  8. stage.scaleMode = StageScaleMode.NO_SCALE;
  9. stage.align = StageAlign.TOP_LEFT;
  10. /* Add the symbols to stage */
  11. var bg = new Background();
  12. addChild(bg);
  13. var title = new Title();
  14. addChild(title);
  15. var menu = new Menu();
  16. addChild(menu);
  17. var middle = new Middle();
  18. addChild(middle);
  19. var footer = new Footer();
  20. addChild(footer);
  21. /* Apply the alignment to the background */
  22. var bgParam = {
  23. x:0,
  24. y:0,
  25. offsetX: 0,
  26. offsetY: 0
  27. }
  28. new FluidObject(bg,bgParam);
  29. /* Apply the alignment to the title */
  30. var titleParam = {
  31. x:0,
  32. y:0,
  33. offsetX:0,
  34. offsetY:0
  35. }
  36. new FluidObject(title,titleParam);
  37. /* Apply the alignment to the menu */
  38. var menuParam = {
  39. x:1,
  40. y:0,
  41. offsetX: -menu.width - 20,
  42. offsetY: 20
  43. }
  44. new FluidObject(menu,menuParam);
  45. /* Apply the alignment to the content */
  46. var middleParam = {
  47. x:0.5,
  48. y:0.5,
  49. offsetX: -middle.width/2,
  50. offsetY: -middle.height/2
  51. }
  52. new FluidObject(middle,middleParam);
  53. /* Apply the alignment to the footer */
  54. var footerParam = {
  55. x:1,
  56. y:1,
  57. offsetX: -footer.width - 10,
  58. offsetY: -footer.height -10
  59. }
  60. new FluidObject(footer,footerParam);
  61. }
  62. }
  63. }
package {

	import flash.display.*;
	import FluidLayout.*;
	
	public class Website extends MovieClip{
		
		public function Website()
		{
			/* Set the Scale Mode of the Stage */
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			/* Add the symbols to stage */
			var bg = new Background();
			addChild(bg);
			
			var title = new Title();
			addChild(title);
		
			var menu = new Menu();
			addChild(menu);
		
			var middle = new Middle();
			addChild(middle);
		
			var footer = new Footer();
			addChild(footer);
			
			/* Apply the alignment to the background */
			var bgParam = {
				x:0,
				y:0,
				offsetX: 0,
				offsetY: 0
			}
			new FluidObject(bg,bgParam);
				
			/* Apply the alignment to the title */
			var titleParam = {
				x:0,
				y:0,
				offsetX:0,
				offsetY:0
			}
			new FluidObject(title,titleParam);
		
			/* Apply the alignment to the menu */
			var menuParam = {
				x:1,
				y:0,
				offsetX: -menu.width - 20,
				offsetY: 20
			}
			new FluidObject(menu,menuParam);
		
			/* Apply the alignment to the content */
			var middleParam = {
				x:0.5,
				y:0.5,
				offsetX: -middle.width/2,
				offsetY: -middle.height/2
			}
			new FluidObject(middle,middleParam);
		
			/* Apply the alignment to the footer */
			var footerParam = {
				x:1,
				y:1,
				offsetX: -footer.width - 10,
				offsetY: -footer.height -10
			}
			new FluidObject(footer,footerParam);
		}
	}
}

Step 17: Ensure All Assets are Ready

Open website.fla in Flash and check again before texting the movie.

There's no need to place the symbols on the stage because the Website.as will create symbol instances from library by using their class names. The linkage class names of the symbols needs to be correct in order for the script to use them. The linkage class name can be checked in the library panel.

Step 18: Ready to View the Result

Click Control > Text Movie or Ctrl(Cmd) + Enter to test the flash website.

Try resizing the window and check if all objects are repositioning to the correct alignment.

Step 19: Further Work

Each FluidObject now needs to have specific x,y,offsetX and offsetY property values. A new Class will be created in the coming steps to simplify the future code when adding new fluid objects.

Step 20: SimpleFluidObject Class

Open a new ActionScript file named "SimpleFluidObject.as". Save this file inside the "FluidLayout" directory because this is part of the FluidLayout package.

This file extends FluidObject class so that it will provides simple alignment by using names like TOP, MIDDLE, BOTTOM_RIGHT instead of specifying the x,y properties.

Here is the skeleton of the class:

view plaincopy to clipboardprint?

  1. package FluidLayout {
  2. import flash.events.Event;
  3. import flash.display.*;
  4. public class SimpleFluidObject extends FluidObject{
  5. public function SimpleFluidObject(target:DisplayObject,paramObj:Object)
  6. {
  7. }
  8. }
  9. }
package FluidLayout {
	
	import flash.events.Event;
	import flash.display.*;

	public class SimpleFluidObject extends FluidObject{

		public function SimpleFluidObject(target:DisplayObject,paramObj:Object)
		{
		}
	}
}

Step 21: Implementing the Constructor

view plaincopy to clipboardprint?

  1. package FluidLayout {
  2. import flash.events.Event;
  3. import flash.display.*;
  4. public class SimpleFluidObject extends FluidObject{
  5. public function SimpleFluidObject(target:DisplayObject,paramObj:Object)
  6. {
  7. /* Tell parent class to init the constructor */
  8. super(target,paramObj);
  9. /* assign alignment and margin value by the constructor parameters */
  10. var alignment = paramObj.alignment;
  11. var margin = paramObj.margin;
  12. /* Preset the alignment and margin value if need */
  13. if (alignment == undefined) alignment = "MIDDLE";
  14. if (margin == undefined) margin = 0;
  15. /* convert the alignment (eg. "TOP", "BOTTOM_RIGHT") to x, y, offsetX and offsetY */
  16. var params = new Object();
  17. switch (alignment){
  18. case "TOP_LEFT":
  19. params = {
  20. x:0,
  21. y:0,
  22. offsetX: margin,
  23. offsetY: margin
  24. };
  25. break;
  26. case "TOP":
  27. params = {
  28. x:.5,
  29. y:0,
  30. offsetX: -target.width/2,
  31. offsetY: margin
  32. };
  33. break;
  34. case "TOP_RIGHT":
  35. params = {
  36. x:1,
  37. y:0,
  38. offsetX: -target.width - margin,
  39. offsetY: margin
  40. };
  41. break;
  42. case "LEFT":
  43. params = {
  44. x:0,
  45. y:.5,
  46. offsetX: margin,
  47. offsetY: -target.height/2
  48. };
  49. break;
  50. case "MIDDLE":
  51. params = {
  52. x:.5,
  53. y:.5,
  54. offsetX: -target.width/2 - margin/2,
  55. offsetY: -target.height/2 - margin/2
  56. };
  57. break;
  58. case "RIGHT":
  59. params = {
  60. x:1,
  61. y:.5,
  62. offsetX: -target.width - margin,
  63. offsetY: -target.height/2
  64. };
  65. break;
  66. case "BOTTOM_LEFT":
  67. params = {
  68. x:0,
  69. y:1,
  70. offsetX: margin,
  71. offsetY: -target.height - margin
  72. };
  73. break;
  74. case "BOTTOM":
  75. params = {
  76. x:.5,
  77. y:1,
  78. offsetX: -target.width/2,
  79. offsetY: -target.height - margin
  80. };
  81. break;
  82. case "BOTTOM_RIGHT":
  83. params = {
  84. x:1,
  85. y:1,
  86. offsetX: -target.width - margin,
  87. offsetY: -target.height - margin
  88. };
  89. break;
  90. }
  91. _param = params;
  92. /* reposition the fluid object to the right position */
  93. this.reposition();
  94. }
  95. }
  96. }
package FluidLayout {
	
	import flash.events.Event;
	import flash.display.*;

	public class SimpleFluidObject extends FluidObject{

		public function SimpleFluidObject(target:DisplayObject,paramObj:Object)
		{
			/* Tell parent class to init the constructor */
			super(target,paramObj);
			
			/* assign alignment and margin value by the constructor parameters */
			var alignment = paramObj.alignment;
			var margin = paramObj.margin;
			
			/* Preset the alignment and margin value if need */
			if (alignment == undefined) alignment = "MIDDLE";
			if (margin == undefined) margin = 0;
			
			/* convert the alignment (eg. "TOP", "BOTTOM_RIGHT") to x, y, offsetX and offsetY */
			var params = new Object();
			switch (alignment){
				case "TOP_LEFT":
				params = {
					x:0,
					y:0,
					offsetX: margin,
					offsetY: margin
					};
				break;
				case "TOP":
				params = {
					x:.5,
					y:0,
					offsetX: -target.width/2,
					offsetY: margin
					};
				break;
				case "TOP_RIGHT":
				params = {
					x:1,
					y:0,
					offsetX: -target.width - margin,
					offsetY: margin
					};
				break;
				case "LEFT":
				params = {
					x:0,
					y:.5,
					offsetX: margin,
					offsetY: -target.height/2
					};
				break;
				case "MIDDLE":
				params = {
					x:.5,
					y:.5,
					offsetX: -target.width/2 - margin/2,
					offsetY: -target.height/2 - margin/2
					};
				break;
				case "RIGHT":
				params = {
					x:1,
					y:.5,
					offsetX: -target.width - margin,
					offsetY: -target.height/2
					};
				break;
				case "BOTTOM_LEFT":
				params = {
					x:0,
					y:1,
					offsetX: margin,
					offsetY: -target.height - margin
					};
				break;
				case "BOTTOM":
				params = {
					x:.5,
					y:1,
					offsetX: -target.width/2,
					offsetY: -target.height - margin
					};
				break;
				case "BOTTOM_RIGHT":
				params = {
					x:1,
					y:1,
					offsetX: -target.width - margin,
					offsetY: -target.height - margin
					};
				break;
			}
			_param = params;			
			

			/* reposition the fluid object to the right position */
			this.reposition();
		}
	}
}

Step 22: New Usage of the Fluid Objects

Refer to the Website.as file and try using the new alignment method to align the fluid objects.

The Old Method to apply alignment to Title:

view plaincopy to clipboardprint?

  1. /* Apply the alignment to the title */
  2. var titleParam = {
  3. x:0,
  4. y:0,
  5. offsetX:0,
  6. offsetY:0
  7. }
  8. new FluidObject(title,titleParam);
/* Apply the alignment to the title */
var titleParam = {
	x:0,
	y:0,
	offsetX:0,
	offsetY:0
}
new FluidObject(title,titleParam);

The New Method to apply alignment to Title:

view plaincopy to clipboardprint?

  1. var titleParam = {
  2. alignment: "TOP_LEFT",
  3. margin: 0
  4. }
  5. new SimpleFluidObject(title,titleParam);
var titleParam = {
	alignment: "TOP_LEFT",
	margin: 0
}
new SimpleFluidObject(title,titleParam);

Available alignments:

  • TOP_LEFT, TOP, TOP_RIGHT
  • LEFT, MIDDLE, RIGHT
  • BOTTOM_LEFT, BOTTOM, BOTTOM_RIGHT

Step 23: The Published HTML

Now the fluid alignment works on the "Test Movie" in Flash IDE, but there is one key point to make this work on browser.

Open website.fla. Go to File > Publish Settings and ensure HTML is enabled. Click the HTML tab and change the dimension to "Percent". Ensure the percent is set to 100 on both width and height.

Click "Publish" to publish the website as "website.swf" and "website.html" files.

Now open the "website.html" file with your favorite text editor and add the following code in the header. Adding the code right after the tag would be a good choice.

These CSS styles eliminate the gap between the top left side of the HTML and the SWF file.

view plaincopy to clipboardprint?

  1. <style> </li> <li>body{ </li> <li>margin:0; </li> <li>padding:0; </li> <li>} </li> <li></style>
	<style>
	body{
		margin:0;
		padding:0;
	}
	</style>

Step 24: Advanced Technique: Adding Easing

An easing effect can be applied when the browser resizes so that the objects will move to the correct position with an ease out effect.

Open "FluidObject.as". Add the following lines after "import flash.display.*;". These lines will import the tweening animation class to give the code ability to ease the objects.

view plaincopy to clipboardprint?

  1. /* classes needed for Easing Animation */
  2. import fl.transitions.Tween;
  3. import fl.transitions.easing.*;
	/* classes needed for Easing Animation */
	import fl.transitions.Tween;
	import fl.transitions.easing.*;

Then find the following lines in the "FluidObject.as" file. They are within the "reposition" function.

  1. _target.x=stageW*_param.x+_param.offsetX;
  2. _target.y=stageH*_param.y+_param.offsetY;
	_target.x=stageW*_param.x+_param.offsetX;
	_target.y=stageH*_param.y+_param.offsetY;

Replace them with the following code:

view plaincopy to clipboardprint?

  1. /* set the duration of the easing animation (seconds) */
  2. var duration = 0.5;
  3. /* declare the new X/Y value */
  4. var newX = _target.x;
  5. var newY = _target.y;
  6. /* calculate the new X value based on the stage Width */
  7. if (_param.x != undefined){
  8. newX = (stageW * _param.x) + _param.offsetX;
  9. }
  10. /* calculate the new Y value based on the stage Height */
  11. if (_param.y != undefined){
  12. newY = (stageH * _param.y) + _param.offsetY;
  13. }
  14. /* Tell flash to tween the target object to the new X/Y position */
  15. new Tween(_target, "x",Strong.easeOut,_target.x,newX,duration,true);
  16. new Tween(_target, "y",Strong.easeOut,_target.y,newY,duration,true);
	/* set the duration of the easing animation (seconds) */
	var duration = 0.5;
	
	/* declare the new X/Y value */
	var newX = _target.x;
	var newY = _target.y;
	
	/* calculate the new X value based on the stage Width */
	if (_param.x != undefined){
		newX = (stageW * _param.x) + _param.offsetX;
	}
	
	/* calculate the new Y value based on the stage Height */
	if (_param.y != undefined){
		newY = (stageH * _param.y) + _param.offsetY;
	}
	
	/* Tell flash to tween the target object to the new X/Y position */
	new Tween(_target, "x",Strong.easeOut,_target.x,newX,duration,true);
	new Tween(_target, "y",Strong.easeOut,_target.y,newY,duration,true);

Test the movie, the objects will be easing when the browser resizes (see example below).

Conclusion

We've just created two classes which are in charge of the floating fluid objects. We also created an example to align different objects on stage by using the classes. This example is only a sample case; you can use your imagination to play with the alignments. For example, a symbol may be interactive and its alignment may change from top to bottom when the user clicks on it.

The file structure should be the same as below after you finish this tutorial. Specifically, the FluidObject.as and SimpleFluidObject.as should be in the "FluidLayout" directory in order to work.

Enjoy the Fluid Layout!

分享到:
评论

相关推荐

    Obi Fluid 3.2

    Please note that fluid simulation is a very intensive task. Be aware of its performance implications before using it. Available for Windows, Mac, iOS and Android. Links Obi Website Community and ...

    An Introduction to Fluid Dynamics (Batchelor)

    many interesting and important photographs of fluid flows are included in order to help the students who do not have an opportunity of observing flow phenomena in a laboratory. The book also contains...

    The Finite Volume Method in Computational Fluid Dynamics

    Readers will discover a thorough explanation of the FVM numerics and algorithms used for the simulation of incompressible and compressible fluid flows, along with a detailed examination of the ...

    Numerical Simulation in Fluid Dynamics-A Practical Introduction

    《流体动力学数值模拟——实用入门》是一本深入探讨流体力学数值模拟的书籍,旨在为读者提供一个实用的入门指南。这本书涵盖了流体动力学的基础理论、数值方法以及实际应用,对于学习和理解流体流动的计算方法具有极...

    An_introduction_to_computational_fluid_dynamics.pdf

    Basic introduction to computational fluid ... Appendix A Accuracy of a Flow Simulation 240 Appendix B Non-uniform Grids 243 Appendix C Calculation of Source Terms 245 References 247 Index 255

    Fluid控件及示例

    "Fluid控件及示例"这一标题和描述暗示了我们讨论的主题集中在 Fluid 控件上,这可能是一种专为 Windows Mobile 平台设计的用户界面组件。Fluid 控件通常指的是那些能够提供平滑过渡效果、动态响应用户交互的UI元素,...

    unity流体效果插件--Obi Fluid

    《Unity流体效果插件——Obi Fluid深度解析》 Unity3D作为一款强大的游戏开发引擎,其在游戏设计中的应用日益广泛。其中,对于真实感强、动态效果逼真的流体模拟,Unity提供了多种解决方案,而Obi Fluid无疑是其中...

    扣图Vertus Fluid Mask3.3.18破解版

    Vertus Fluid Mask 是一款非常强大的智能抠图软件, 在智能技术流行的今天,Vertus Fluid Mask的智能抠图技术也让其人气满满。Vertus Fluid Mask采用了模拟人眼和人脑的方法,来实现高级的、准确而且快速的抠图功能...

    EPLAN Fluid快速入门.pdf

    EPLAN Fluid是一款专注于流体工程设计的软件,隶属于EPLAN Electric P8平台,主要应用于液压和气动系统的工程设计。EPLAN Fluid的快速入门指南旨在帮助新用户快速掌握软件的基本操作和核心功能,以便能够高效地进行...

    Obi Fluid流体插件以及使用文档

    Obi Fluid是一款在Unity引擎中广泛使用的高级流体模拟插件,它利用粒子系统来创建逼真的液态效果。这款插件为游戏开发者、视觉艺术家和模拟专家提供了强大的工具,可以创造出令人惊叹的水、烟雾、泡沫等各种流体动态...

    流体模拟Fluid Simulation

    流体模拟(Fluid Simulation)是计算机图形学中的一个重要领域,主要涉及如何在数字环境中逼真地模拟液态物质的行为。这种技术广泛应用于电影特效、游戏开发、科学研究等多个领域,为观众带来栩栩如生的视觉体验。在...

    A_mathematical_intro_to_fluid_mech_chorin.pdf

    根据提供的文件信息,“A_mathematical_intro_to_fluid_mech_chorin.pdf”这份文档主要涉及流体动力学的基础数学理论。以下是对该文件中提到的一些关键知识点的详细阐述: ### 作者与编者信息 - **作者**:...

    vertus fluid mask 3.3.15汉化版

    【Vertus Fluid Mask 3.3.15汉化版】是一款备受赞誉的图像处理工具,专注于提供高级的智能抠图功能。在当前数字化时代,高效且精准的图像分割技术对于设计者、摄影师以及视觉艺术家来说至关重要。Vertus Fluid Mask...

    Obi Fluid v4.1.7z

    《Unity3D中的Obi Fluid:模拟真实水流的利器》 在Unity3D这个强大的游戏开发引擎中,实现逼真的水流效果是一项挑战。而Obi Fluid正是为了解决这一问题而设计的插件,它提供了高级的流体模拟功能,使得开发者能够...

    FLUID编程手册

    Chapter 11 使用 FLUID 编程 FLUID(Fast Light User-Interface Designer)是一款用于设计图形用户界面(GUIs)的工具,尤其适用于使用FLTK(Fast Light Toolkit)库的应用程序。FLTK是一个轻量级、跨平台的C++ GUI...

    An Introduction to Fluid Dynamics

    An Introduction to Fluid Dynamics

    Vertus Fluid Mask 3

    **Vertus Fluid Mask 3** 是一款专为图像编辑和设计者打造的高效抠图工具,尤其在Photoshop(ps)环境中作为插件使用。它以其独特的液体分割技术而闻名,能够提供精确、自然的选区,使得图像的背景替换或对象提取变...

    Effect of Nanoroughness on Wall Slip of a Squeezed Fluid Film

    Effect of Nanoroughness on Wall Slip of a Squeezed Fluid Film,孙红新,吴承伟,Wall slip for a fluid flowing near a randomly nanorough surface can be described by a hydrodynamic equation subjected to ...

    ANSYS Fluid Dynamics Verification Manual.pdf

    《ANSYS流体动力学验证手册》是ANSYS公司发布的一份重要的技术文档,主要针对ANSYS Fluid Dynamics软件进行详细的技术验证与说明。该手册涵盖了ANSYS流体动力学解决方案在Release 19.0版本中的核心功能、验证案例...

Global site tag (gtag.js) - Google Analytics