官术网_书友最值得收藏!

Maintaining components' proportions when their containers are resized

Having components that maintain their proportions when their containers are resized is important, especially, when dealing with form elements.

In the following screenshot, there are three panels that resize themselves as their container is resized:

Maintaining components' proportions when their containers are resized

How to do it...

  1. Create the panels that will be displayed:
    panel1=new Ext.Panel({
    title: 'Panel 1',
    html: 'Width=container width\' - 50 px',
    // Width=container width' - 50 px.
    anchor: '-50'
    });
    panel2=new Ext.Panel({
    title: 'Panel 2',
    html: 'Width=75% of container',
    // Width=75% of container.
    anchor: '75%'
    });
    panel3=new Ext.Panel({
    title: 'Panel 3',
    html: 'Width=50% of container<br/>Height=container\'s - 150 px',
    // Width=50% of container,
    // Height=container's - 150 px.
    anchor: '50%, -150'
    });
    
  2. Now, create the container window. When this window is resized, the panels will maintain their proportions:
    var container=new Ext.Window({
    title: 'Anchor Layout',
    width: 600,
    height: 350,
    defaults: { bodyStyle: 'padding:10px' },
    // The anchored items will automatically resize
    // to maintain the same relative dimensions.
    layout: 'anchor',
    items: [panel1, panel2, panel3]
    });
    container.show();
    

How it works...

The relative anchoring is achieved by specifying an AnchorLayout for the layout configuration option of the container. If the container is resized, all anchored items are automatically rendered according to their anchor rules.

There's more...

The container using the AnchorLayout can supply an anchoring-specific config property, anchorSize. By default, AnchorLayout will calculate anchor measurements based on the size of the container itself. However, if anchorSize is specified, the layout will use it as a virtual container for the purposes of calculating anchor measurements based on it instead. This will allow the container to be sized independent of the anchoring logic if necessary.

It's also possible to supply the horizontal and vertical anchor values through the anchor config option. They can refer to the percentage an item should take up within its container, the offset from the right and bottom edges, or the container's sides. For example,'-50 75%' would render the width offset from the container's right edge by 50 pixels and 75% of the container's height.

主站蜘蛛池模板: 大悟县| 泾阳县| 洪雅县| 阳高县| 衡东县| 黔江区| 泸定县| 和硕县| 扎赉特旗| 岳池县| 鄂州市| 汝城县| 永城市| 固镇县| 长泰县| 泰和县| 景德镇市| 青阳县| 闸北区| 会宁县| 盐池县| 佛冈县| 南开区| 彭山县| 磴口县| 略阳县| 宜昌市| 迁安市| 都昌县| 岑巩县| 布尔津县| 衢州市| 万山特区| 永清县| 南丰县| 红原县| 迁安市| 房产| 乾安县| 亳州市| 佛山市|