Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3284280
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:08:13+00:00 2026-05-17T20:08:13+00:00

I am trying to implement a simple fold/unforld effect for elastic div’s, i.e. when

  • 0

I am trying to implement a simple fold/unforld effect for elastic div’s, i.e. when actual dimensions of an element are not set through CSS and thus are not trivially determinable. So I need equivalents of jQuery’s width() and height() for arbitrary elements; the methods should return something that would be assignable to *.style.width/height to achieve the fold/unfold effect. The methods can be setters as well, though not necessarily as it’s trivial anyway (or is it not?)

Compatibility: IE8+ and the adequate gang of 3: Firefox, Chrome, Safari. S.O. doesn’t have an answer for this other than “use jQuery” which I don’t want to do at the moment. The thing is I don’t need to support such archaic browsers as IE6/7 and Netscape. I want my code to be just simple, maiden JavaScript for modern browsers. Dropping IE8 for IE9 is an option, too.

I think the answer may be useful to many web developers. Thanks!

Edit: I understand of course, analyzing jQuery (and dropping archaic quirks) is a good option, but I thought someone has done this already and could share with us.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:08:14+00:00Added an answer on May 17, 2026 at 8:08 pm

    Here is an answer to my own question I came up with today after playing with IE8, FF, Chrome and Safari.

    Let’s say we want to fold and unfold a div element vertically (so we’ll be talking only about the height for simplicity) with minimal possible JavaScript and no jQuery. Could be an inline error message box that you want to show and hide nicely on the page. The message box itself is elastic and you don’t know its actual dimensions. There are two general problems to be solved here:

    Problem #1: offsetHeight, clientHeight and scrollHeight properties are notoriously inconsistent across browsers. You can’t rely on them especially when margin/padding/border are set to something other than 0 and when the CSS height is something other than default (i.e. when actually folding/unfolding the element). There is getComputedStyle(), but it’s available only on sane browsers and not in IE. OTOH, IE’s currentStyle property doesn’t return computed values as you might expect (e.g. you may see “auto” as the height value). It turns out, with no margin/padding/border offsetHeight is reliable and compatible on all modern browsers.

    Problem #2: the only way (that I know of) to animate your div element is to change style.height, which, however, doesn’t include padding and border, and thus you can’t animate the box down to 0 or start your animation from 0 for elements with non-zero padding and border. (On top of that, IE doesn’t accept 0px as style.height, so the minimal size would be 1px anyway).

    The solution to both problems is to wrap your box around with another div with default style, i.e. padding/margin/border all set to 0, which is the default style for a new div. The box you want to fold and unfold should be inside and it shouldn’t have a margin setting, only padding and border if desired. This way, you will be able to (1) reliably determine the height of your elastic inner box via offsetHeight, and (2) animate the height of the outer box to and from 1 pixel.

    So, let’s say we want to animate this:

    <div id="errmsg" style="display:none">
        <div class="errmsg">
            <div class="window-close" onClick="javascript:showErrMsg('', this.parentNode.parentNode)"></div>
            <div id="errmsg.text"></div>
        </div>
    </div>
    

    The box is invisible in the beginning. The “window-close” class is a small box on the right with a cross which is used to hide the box when clicked. Calling showErrMsg() with empty string folds the message box (see below).

    My JavaScript code to animate this error message box is as follows (I’m also animating opacity which is omitted here for simplicity):

    var ANIMATE_STEPS = 10;
    var ANIMATE_DELAY = 20;
    
    
    function _obj(obj, parent)
    {
        if (typeof obj == 'string')
            obj = (parent ? parent : document).getElementById(obj);
        return obj;
    }
    
    function _setStyleHeight(obj, h)
    {
        _obj(obj).style.height = Math.round(h) + 'px';
    }
    
    function _animateVertFold(obj, cur, by, lim)
    {
        obj = _obj(obj);
        cur += by;
        if (by < 0 && cur <= 1)
        {
            obj.style.display = 'none';
            _setStyleHeight(obj, 1); // IE doesn't accept 0
        }
        else if (by > 0 && cur >= lim)
        {
            _setStyleHeight(obj, lim);
        }
        else
        {
            _setStyleHeight(obj, cur);
            setTimeout('_animateVertFold(\'' + obj.id + '\', ' +
                cur + ', ' + by + ', ' + lim + ')', ANIMATE_DELAY);
        }
    }
    
    function _expandElem(obj)
    {
        obj = _obj(obj);
        if (obj.style.display == 'none')
        {
            obj.style.overflow = 'hidden';
            _setStyleHeight(obj, 1);
            obj.style.display = 'block';
            var innerDiv = obj.getElementsByTagName('div')[0]; // better way?
            var h = innerDiv.offsetHeight;
            if (h > 0)
                _animateVertFold(obj, obj.offsetHeight, h /  ANIMATE_STEPS, h);
        }
    }
    
    function _shrinkElem(obj)
    {
        obj = _obj(obj);
        if (!obj.style.display || obj.style.display != 'none')
        {
            obj.style.display = 'block';
            obj.style.overflow = 'hidden';
            var h = obj.offsetHeight;
            _animateVertFold(obj, h, - h /  ANIMATE_STEPS, h);
        }
    }
    
    
    function showErrMsg(msg, id)
    {
        id || (id = '_errmsg');
        obj = _obj(id);
        if (msg != '')
        {
            _obj(id + '.text').innerHTML = msg;
            _expandElem(obj);
        }
        else
            _shrinkElem(obj);
    }
    

    You can call showErrMsg() and the code will take care of the rest. You will have to define the window-close class as something that mimics a GUI close-window button. And of course the “errmsg” class as a box with distinct background color, border and possibly font face as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a very simple footer notification element to slide up for
I'm new to Flex SDK and trying to implement a simple project using Doug
trying to implement a dialog-box style behaviour using a separate div section with all
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
im trying to implement simple secured client server communiction using WCF. when im launching
In trying to implement simple integration patterns with Biztalk ESB Toolkit 2.0, I'm facing
I'm trying to implement simple sliding window alogirithm on two-dimensional array in C# 3.0,
I am trying to implement simple chat application using flex. In it all my
I am trying to implement simple xhr abstraction, and am getting this warning when
I'm trying to implement a simple rollover tooltip for images on a page where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.