Hello. I have a new blog.

I've moved this blog to the following URL Kerkness.ca. Thank you for visiting, please update your bookmarks.

Saturday, April 5, 2008

Customizing the Flex ScrollBar with Skins

One of the things I love about Flex is the smooth and clean look/feel of the default components. With just a little CSS the default look and feel can be customized to fit most web site designs. But what do you do when your requirements are very different from what you can accomplish with CSS? With Flex it's possible to build custom skins and give you're application any look and feel you desire.

While the process to complete this isn't overly complex it probably helps if you come from a Flash background and also have some strong design skills.

There are two ways to build skins. 1) Use images and 2) Use actionscript classes. For my example I have combined both approaches to customize the look/feel of the default Flex scrollbar.

Summary of the solution

- Create a PNG image to use as the background for the scrollbar track.
- Create a PNG image to use as the icon for the scrollbar handle.
- Create an ActionScript class to draw a skin for the scrollbar box.
- Create a CSS Style for the scrollbar which references the new skins
- Apply the StyleName to our scrollbar

The CSS Style

.customScroll
{
/* remove the arrow skins by referencing a null class */
upArrowSkin: ClassReference(null);
downArrowSkin: ClassReference(null);

/* Embed an image to use as skin for scroll bar track and scroll bar thumbIcon */
trackSkin: Embed(source="skins/CustomSkinTrack.png");
thumbIcon: Embed(source="skins/CustomSkinIcon.png");

/* Reference action script skin for various states of the scroll bar thumb */
thumbUpSkin: ClassReference('theme.skins.CustomSkinThumb');
thumbOverSkin: ClassReference('theme.skins.CustomSkinThumb');
thumbDownSkin: ClassReference('theme.skins.CustomSkinThumb');
}


Click here to view the demo.
Click here to view the source code.

5 comments:

  1. Hi, I'm testing the example but in my project it doesn't work. The scrollbar it's outside of the border. Any suggestion?

    ReplyDelete
  2. There are more style properties available for the scrollbar than just the skins. You should be able to adjust the style properties for 'bottom, top, left, right' to adjust the distance between the scrollbar and the parent component. See http://livedocs.adobe.com/flex/201/langref/mx/controls/scrollClasses/ScrollBar.html and look at the inherited style properties.

    ReplyDelete
  3. mauricio :

    In the source for ScrollSkin.as there's a line saying :

    // Background
    this.x = -15;

    That might be causing your bar background to be offset.

    ReplyDelete
  4. I have a Tilelist in my flex application , which i am trying to set up a vertical Scrollbar , I am testing with this code. It works but the scroll will appears only if the data exceeds the Tilelist boundary,
    do you have any solution to show the scroll in all cases

    Joseph

    ReplyDelete
  5. @Anonymous: You can try setting the horizontalScrollPolicy or verticalScrollPolicy to 'on' for your container.

    ReplyDelete

Thank you for the comments.