How to Animate Visibility of Expand Boxes in AngularJS TreeView

Created: 11 March 2016

Usually for all tree items that have children in TreeView directive for AngularJS, an expand box is shown before the item label. If you want, you can make expand boxes to be hidden all the time, but it is better if they show up only when needed.

In this article, we will explain how to show expand boxes in the TreeView only when mouse cursor hovers over the tree view space. Otherwise, they will remain hidden. To further enhance this feature, we will add a CSS animation that changes the visibility of expand boxes depending on whether they will appear or disappear.

TreeView directive is part of IntegralUI Studio for Weba suite of UI Components for development of web apps

How to Change Visibility of Expand Boxes

Whether expand boxes are present in the TreeView is determined by the showExpandBoxes property. This property by default is set to true, meaning that before each item that has children, an expand box is displayed. If we set this property value to false, the expand boxes will become hidden.

To change the visibility of expand boxes when mouse cursor enters or leaves the tree view space, we will use the mouseenter and mouseleave events. Whenever mouse cursor enters into the TreeView space, the expand boxes will appear, and whenever it leaves, they will disappear.

Our animation consists on changes to the opacity attribute of expand box element. We only need two states: 0% at start of animation, and 100% when animation is completed. Depending on animation type, the opacity value is changed from 0 to 1, or vice versa.

The 'leave' animation also has a time delay of 1 second, this makes expand boxes to remain visible for a while, prior making them invisible.

Animations also are synchronized with our previous code, so that whenever the mouse cursor enters the tree view space, the showExpandBoxes is set to true. Then the 'enter' animation will execute, making them gradually to appear. On other hand, when mouse cursor leaves the tree view space, the 'leave' animation is executed and expand boxes gradually will disappear. At the end, this followed by changing the showExpandBoxes to false, in mouseleave event handler, but only after short delay.

Conclusion

Changing the visibility of expand boxes in AngularJS TreeView is straightforward, you only need to set one property value, and they will either appear or disappear. Using CSS animations makes this process more visually appealing to the user.

Tab content switching

Newsletter

Sign-up to our newsletter and you will receive news on upcoming events, latest articles, samples and special offers.

Name:

Email: *

*By checking this box, I agree to receive a newsletter from Lidor Systems in accordance with the Privacy Policy. I understand that I can unsubscribe from these communications at any time by clicking on the unsubscribe link in all emails.