Introduction

We’re pleased to introduce a new ZK Addon – Anchornav.
This component synchronizes the scrolling position on a page or within ZK containers (Div, Window, etc.) with hyperlinks and buttons. It allows you both to navigate to desired ZK components in a page and highlight the current navigation link based on the current scroll position.

Demo

This video demonstrates how the Anchornav component activates a specific link when scrolling to the corresponding section of the page. In reverse, a corresponding component is scrolled into view when the link is clicked.

Required Configuration

Version

  • ZK CE: 8.0.0 or later
  • ZK PE and EE: (Optional)

Example

<zk xmlns:ca="client/attribute">
  <anchornav name="a1">
     <a ca:data-anchornav-target="$win1"> First Window </a>
     <a ca:data-anchornav-target="$win2"> Second Window </a>
  </anchornav>
  <window id="win1" title="1. First Window">
    Hello world.
  </window>
  <window id="win2" title="2. Second Window">
    Welcome to ZK world.
  </window>
</zk>

By default, the Anchornav component will watch the scroll position of a page.
Other scrollable containers can be associated with a named Anchornav by setting the client-attribute:
ca:data-anchornav-scroll.

For example:

<div ca:data-anchornav-scroll="a1">
     <!-- other components -->
</div>

A or Button components can be used as links and targets are specified by the ZK client-attribute ca:data-anchornav-target=[selector].
For example, in Line 3 and Line 4, $win1 / $win2 select widgets by zk component id, win1 / win2.

The JQuery-based selector syntax (#domId / .class / elementName) is extended by ZK specific selectors as seen above using $componentId or @componentName.

Anchornav Properties

As of now Anchornav supports the following properties:

  • setName(String name)
    Set the name of Anchornav, it is only required when we want to spy scrolling in ZK containers instead of the whole page scrollbar.
  • setPositionFixed(Boolean positionFixed)
    Set whether to enable position fixed when anchornav is out of current view. When it is set to true, Anchornav will stay (float) on the same position of the page. (Default: true)

In order to layout your navigation links you can use arbitrary ZK components, such as vlayout, Window

For example:

<anchornav name="anchor1">
   <vlayout>
     <a ca:data-anchornav-target="..."> ... </a>
     <button ca:data-anchornav-target="..."> ... </button>
   </vlayout>
</anchornav>

Customization

An active link will receive the css class – z-anchornav-link-active – available for custom styling:

.z-anchornav-link-active {
  background-color: yellow;
}

I hope you find this component interesting. Please let us know how you like it!

This component is currently in its Beta. If you are interested in using this component please contact us at info@zkoss.org.

Downloads

The runnable demo project is available on Github.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply