Custom Scrollbars in WebKit

Custom Scrollbars in WebKit

Barras de scroll personalizadas en WebKit

The aim of this post is to review the necessary styles to custom the scroll in WebKit, on the basis of its blog tutorial http://www.webkit.org/blog/363/styling-scrolls/ but customizing each of the scroll parts step by step. The example given in http://trac.webkit.org/export/41842/trunk/LayoutTests/scrolls/overflow-scroll-combinations.html is difficult to understand given that, in the same styles page the different variations are mixed and it seems that a hard work with CSS is needed to custom the scroll.

The first thing I am going to do is to prepare a ‘div’ with scrolls to separate those styles from the styles relating to the ones of its customization. We’ll be able to apply the styles to any element with scroll, the page, an ‘iframe’, text area or any other element with ‘overflow’.

To point out that an object must use a custom scroll, we must associate the scrollbar pseudo-element with it, which will hide the scroll by default and will use the one defined in the css. The height and width will be the horizontal and vertical scroll ones, respectively.

We have just hidden the scroll by default. Next I am going to collect the most important parts of the article from the WebKit blog so we can follow with the examples. The list of the pseudo-elements for customizing the different pieces of the scroll is:

  • scrollbar
  • scrollbar-button
  • scrollbar-thumb
  • scrollbar-track
  • scrollbar-track-piece
  • scrollbar-corner
  • resizer

Each of this scroll pieces can be customized with edges, shadows, background images, etc. The next pseudo-classes can be used to apply to the scroll pseudo-elements:

  • :horizontal: applies to the pieces of the horizontal scroll.
  • :vertical: applies to the pieces of the vertical scroll.
  • :decrement: applies to the button and to the track pieces of the upper and left scroll.
  • :increment: applies to the button and to the track pieces of the lower and right scroll.
  • :start: it shows if the button and the track pieces go before the scrollbar.
  • :end: it shows if the button and the track pieces parts go after the scrollbar.
  • :double-button: it is used for detecting if a button goes next to another one at one of the scroll ends.
  • :single-button: it is used for detecting if a button goes alone at one of the scroll ends.
  • :no-button: it is applied to the track pieces to show if these run to the edge of the scrollbar, as when there are no buttons.
  • :corner-present: it is applied to all the scroll pieces and it shows if the corner of the scroll is present.
  • window-inactive: it is applied to all the scroll pieces and it shows if the scroll window  is active.

In addition:

  • The pseudo-classes :enabled, :disabled, :hover and :active also work with the different scroll pieces.
  • The ‘none’ value of the ‘display’ property can be used to hide pieces of the scroll.
  • Margins can be used all along the scroll axis and these can be negative, so that the track can be inflated to cover the buttons partially.

If we want to use the Chrome Developer Tools to apply the styles to the different scroll parts, we will see we can not select them. So the best thing to do will be to create those styles over a ‘div’ in order to review them with the Developer Tools and then we will move them to the scroll part where we want them to be applied. First, I custom the track and thumb of the scroll:

If I am going to set out some different styles for the parts of the horizontal and vertical scroll, I will have to use the pseudo-classes: horizontal and vertical. For example, if I want to set out a thumb identical to the one used in Mac, I have to use images, so I will need different images for the horizontal and vertical thumb. On the demo of the WebKit’s blog we can see how different images are used to custom the scroll, but there is something I did not like, which is the JavaScript use to do a preload of the images in order to avoid the blinking when you change the image on the state change. Instead of using different images for each state I do think it would be better to use sprites in order to avoid the JavaScript use and having all the scroll customization in the CSS. We can achieve this by simply changing the coordinates of the image as in the next example. Maybe the tutorial’s date has to be taken into account and the fact that the border-image property was not fully implemented as also the working of the border-image and webkit-border-image differ on some aspects.

Now, I am going to follow with the example without images, adding the scrollbar-button style. To do this, I add the background images to each of the buttons so these show an arrow. Next, I style the resize and the scrollbar-corner.

Seeing the css code of the WebKit’s blog tutorial, the customization of the scroll on WebKit may seem hard and with a big amount of CSS intervention, but we can see that, in fact it is a very simple task. At this point, the only thing left to do in order to improve our scroll is to customize each of the states of scroll enabled, :disabled, :hover and :active and to vary, if we want it, the options of the different scroll elements with their corresponding pseudo-classes.

Tags:
©2024 PoseLab SL. All rights reserved.