YUI 3.x Home -

YUI Library Examples: Overlay: Constrain Support

Overlay: Constrain Support

This example shows how you can use Overlay's constrained positioning support to constrain the XY position of the overlay so that it remains within another node on the page (or within the viewport).

Overlay Constrained XY Positioning

Setting Up The YUI Instance

As with the "Basic XY Positioning" example, to create an instance of an Overlay on your page, the only module you need to request is the overlay module. The overlay module will pull in the widget, widget-stack, widget-position, widget-position-align, widget-position-constrain and widget-stdmod extensions it uses.

  1. YUI({...}).use("overlay", function(Y) {
  2. // We'll write example code here, where we have a Y.Overlay class
  3. // available, which is bundled with XY positioning, align and
  4. // constrain support.
  5. });
YUI({...}).use("overlay", function(Y) {
    // We'll write example code here, where we have a Y.Overlay class 
    // available, which is bundled with XY positioning, align and 
    // constrain support.
});

Using the overlay module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.

Instantiating The Overlay

For this example, we'll instantiate an Overlay from script, as we did for the "Alignment Support" example, but use the render attribute to specify the node under which we want the Overlay to be rendered in the DOM, and to indicate that we want it rendered on construction. The render attribute is a sugar attribute for the render() method:

  1. /* Create Overlay from script */
  2. var overlay = new Y.Overlay({
  3. id:"overlay",
  4.  
  5. width:"140px",
  6. height:"120px",
  7.  
  8. headerContent: "Constrained",
  9. bodyContent: "Use the sliders to move the overlay",
  10. footerContent: '<label><input type="checkbox" id="constrain"> Constrain </label>',
  11.  
  12. align:{node:"#constrain-box", points:["cc", "cc"]},
  13. constrain:"#constrain-box",
  14.  
  15. render: "#overlay-example"
  16. });
    /* Create Overlay from script */
    var overlay = new Y.Overlay({
        id:"overlay",
 
        width:"140px",
        height:"120px",
 
        headerContent: "Constrained",
        bodyContent: "Use the sliders to move the overlay",
        footerContent: '<label><input type="checkbox" id="constrain"> Constrain </label>',
 
        align:{node:"#constrain-box", points:["cc", "cc"]},
        constrain:"#constrain-box",
 
        render: "#overlay-example"
    });

We align the overlay to the center of the #constrain-box node, which we're also using as the constraining node for the overlay. The constrain attribute accepts a node reference (either an actual Node instance, or a string selector reference), or it can simply be set to true to constrain the overlay to the Viewport.

Demonstrating Constrained Support

For this example, we set up a couple of Slider instances which can be used to set the overlay's x and y attribute values.

  1.  
  2. // Get the current XY position of the overlay
  3. // (after it's been center aligned) to set the
  4. // initial value for the sliders
  5. var overlayXY = Y.one("#overlay").getXY();
  6.  
  7. // Get the region for the constraing box
  8. var constrainRegion = Y.one("#constrain-box").get("region");
  9.  
  10. // X-Axis
  11. var sx = new Y.Slider({
  12. id:"x",
  13. length: "450px",
  14. min: constrainRegion.left - 50,
  15. max: constrainRegion.right + 50,
  16. value: overlayXY[0],
  17. render:"#overlay-example"
  18. });
  19.  
  20. // Y Axis
  21. var sy = new Y.Slider({
  22. id:"y",
  23. axis: 'y',
  24. length: "400px",
  25. min: constrainRegion.top - 50,
  26. max:constrainRegion.bottom + 50,
  27. value: overlayXY[1],
  28. render: "#overlay-example"
  29. });
  30.  
 
    // Get the current XY position of the overlay 
    // (after it's been center aligned) to set the 
    // initial value for the sliders
    var overlayXY = Y.one("#overlay").getXY();
 
    // Get the region for the constraing box
    var constrainRegion = Y.one("#constrain-box").get("region");
 
    // X-Axis
    var sx = new Y.Slider({
        id:"x",
        length: "450px",
        min: constrainRegion.left - 50,
        max: constrainRegion.right + 50,
        value: overlayXY[0],
        render:"#overlay-example"
    });
 
    // Y Axis
    var sy = new Y.Slider({
        id:"y",
        axis: 'y',
        length: "400px",
        min: constrainRegion.top - 50, 
        max:constrainRegion.bottom + 50,
        value: overlayXY[1],
        render: "#overlay-example"
    });
 

We set the min and max values of the slider instances to allow the overlay to be moved beyond the edge of the constraining region, and set the initial value of the sliders to reflect the current centered position of the overlay.

Finally, we set up valueChange listeners for the sliders, when attempt to set the corresponding axis position of the overlay:

  1. // Set the overlay's x attribute value
  2. sx.after("valueChange", function(e) {
  3. overlay.set("x", e.newVal);
  4. });
  5.  
  6. // Set the overlay's y attribute value
  7. sy.after("valueChange", function(e) {
  8. overlay.set("y", e.newVal);
  9. });
    // Set the overlay's x attribute value
    sx.after("valueChange", function(e) {
        overlay.set("x", e.newVal);
    });
 
    // Set the overlay's y attribute value
    sy.after("valueChange", function(e) {
        overlay.set("y", e.newVal);
    });

CSS: Overlay Look/Feel

As mentioned in the "Basic XY Positioning" example, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.

The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:

  1. /* Overlay Look/Feel */
  2. /* Overlay Look/Feel */
  3. .yui3-overlay-content {
  4. text-align:center;
  5. padding:2px;
  6. border:1px solid #000;
  7. background-color:#aaa;
  8. font-size:93%;
  9. }
  10.  
  11. .yui3-overlay-content .yui3-widget-hd {
  12. font-weight:bold;
  13. padding:5px;
  14. border:2px solid #aa0000;
  15. background-color:#fff;
  16. }
  17.  
  18. .yui3-overlay-content .yui3-widget-ft {
  19. padding:5px;
  20. border:2px solid #000;
  21. background-color:#ccc;
  22. }
  23.  
  24. .yui3-overlay-content .yui3-widget-bd {
  25. padding:5px;
  26. border:2px solid #0000aa;
  27. background-color:#fff;
  28. }
/* Overlay Look/Feel */
/* Overlay Look/Feel */
.yui3-overlay-content {
    text-align:center;
    padding:2px;
    border:1px solid #000;
    background-color:#aaa;
    font-size:93%;
}
 
.yui3-overlay-content .yui3-widget-hd {
    font-weight:bold;
    padding:5px;
    border:2px solid #aa0000;
    background-color:#fff;
}
 
.yui3-overlay-content .yui3-widget-ft {
    padding:5px;
    border:2px solid #000;
    background-color:#ccc;
}
 
.yui3-overlay-content .yui3-widget-bd {
    padding:5px;
    border:2px solid #0000aa;
    background-color:#fff;
}

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings