Book Image

MooTools 1.2 Beginner's Guide

Book Image

MooTools 1.2 Beginner's Guide

Overview of this book

MooTools is a simple-to-use JavaScript library, ideal for people with basic JavaScript skills who want to elevate their web applications to a superior level. If you're a newcomer to MooTools looking to build dynamic, rich, and user-interactive web site applications this beginner's guide with its easy-to-follow step-by-step instructions is all you need to rapidly get to grips with MooTools.
Table of Contents (14 chapters)
MooTools 1.2 Beginner's Guide
Credits
About the Authors
About the Reviewer
Preface

Time for action—toggling the visibility of a div


Imagine you have a div that you would like to hide or show. The tween() method will allow you to do this very easily and with very few lines of code. Let's just jump right to it.

  1. First, we set up the HTML. We use an<a> tag as the trigger. When clicked it will either hide or show the #panel div depending on its visibility.

    <a href="#">Toggle div</a>
    <div id="panel">
    <!-- Div content -->
    </div>
    
  2. We give the #panel div some simple styles by changing its background color to gray and giving it a black border. This step is optional but it makes it easier to see the transition.

    #panel {
    background-color:#ccc;
    border:1px solid #000;
    }
    
  3. Moving on to MooTools. First, we declare a variable that holds the<a> element. This makes it easier to reference in our code. We also immediately set the opacity of the #panel div to 0.

    // target is the div we hide or show
    var target = $('panel');
    // Hide div immediately
    target.setStyle...