Simon Hibbard's Web Apps Blog

Posts tagged “slide down

DivGrow jQuery Plugin – Easily make your DIV containers expandable & collapsable (with animation)

I had the need for a number of divs on a page that would show a certain amount of content, then grow (animate) with a ‘show more’ button, and shrink back with a ‘show less’ button.

There were solutions out there, but the ones I found were based on the number of characters in the text and had issues with certain content inside them such as images or links etc.

I just wanted to use pixel height values. So I wrote a jQuery plugin to do so, you can find it here. It is tiny, the compressed version just 920 bytes (not even 1K!).

The plugin will crop the container div to the initial height value and add a link to the bottom to expand it. When clicked the container wil expand to the actual height of the container and provide a click link to collapse it. The expand and collapse action are animated.

Cropped DIV

Expanded DIV

 

Very simple to use. Just call the function on the jQuery selector for a containing div element and pass its initial size, then for each div container set the rel value of the size you want it to expand to.

If you want divs with different starting heights, just give them different class names and apply the divgrow function for each class with different starting heights.

It is used on this live site – http://www.wasdell.co.uk/services.aspx

and here http://www.itzyogi.com/therapies.aspx

Download the latest release from the jQuery Plugin Page

Usage:

1) Inlcude the JS file into your page (after jQuery – requires 1.4.2+):

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&#8221; type=”text/javascript”></script>

<script src=”JS/jquery.divgrow.min.js” type=”text/javascript”></script>

2) Apply the divgrow function to any div element(s) you want to grow using the usual jQuery selector syntax. initialHeight sets the height of the containers before they are expanded. Here any div of class “more-block” will have this feature applied and the starting height is 147. Use different classes and apply seperatly if you need different starting heights.

$().ready(function() {
$(‘div.more-block’).divgrow({ initialHeight: 147 });
});

3) Create your container with a class or id reference that the plugin was bound to (i.e. “more-block” in this example)

<div class=”more-block”>
<h2>Corns and Callus (Hyperkeratosis)</h2>
<p>
These occur in areas of the foot that are experiencing excess pressure and/or friction. The body seeks to defend itself by producing extra layers of skin as a form of protection. However as the process continues the accumulation of extra skin exceeds that which is lost naturally so the skin in these vulnerable areas builds up and becomes compacted as the original cause of the process continues. The protective mechanism becomes the problem if left untreated and the cause not addressed.<br />
Callus denotes a general area of hard dead skin and a corn is a much more concentrated collection which often goes deep rather than wide. Corns can produce pain completely out of proportion to their size often because of impingement upon nerves or sometimes infection.<br />
Treatment of the symptoms is the first step to stopping the pain and discomfort and this is usually addressed without much discomfort by a foot health professional. Treating the cause can be as simple as discarding an ill fitting pair of shoes or may entail a more detailed assessment of individual foot function.<br />
Callus is not always caused by mechanical factors but can be a symptom of a systemic disease such as Psoriasis.<br />
There are some useful devices to assist in protecting vulnerable areas from corns and callus – see our recommended <a href=”Products.aspx”>products page</a>.
</p>
</div>

—————————————————–

Thats it.

Optional arguments:

initialHeight: 100 – (default)
moreText: “+ Show More” – (default)
lessText: “- Show Less” – (default)
showBrackets: true – (default)

e.g.

$(‘div.more-block’).divgrow({ initialHeight: 147, moreText: “give me more”, lessText: “thats enough”, showBrackets: false });

The zip file includes a how to instruction on use.

Advertisements