0

I have a few nearly identical CSS animation functions for a JavaScript "class". The one I am posting below is for height, but I also have some for width, left, etc. It seems fairly redundant to me to have different functions for what is basically the same task, can I replace height in the function below with some kind of CSS property argument?

WindowItem.prototype.animateHeight = function(inElement, inTarget) {
    var selfCall = true;
    var currHeight = parseInt(inElement.style.height, 10);

    if (this.isExpanded) {
        if (currHeight < inTarget) {
            currHeight += ((inTarget-currHeight)>>3)+1;
            if (currHeight >= inTarget) {
                currHeight = inTarget;
                selfCall = false;
            }
        }
    }
    else {
        if (currHeight > inTarget) {
            currHeight -= ((currHeight-inTarget)>>3)+1;
            if (currHeight <= inTarget) {
                currHeight = inTarget;
                selfCall = false;
            }
        }
    }
    inElement.style.height = currHeight+"px";

    if (selfCall) {
        var self = this;
        setTimeout(function() {
            self.animateHeight(inElement, inTarget);
        }, 33);
    }
}

Edit: I probably should have also posted how I am calling this, I am not sure what to be passing the function to specify height in this example: this.animateHeight(this.imgWindow, 0);

4
  • 2
    cssProperty="height" and inElement.style[cssProperty]? Commented Apr 16, 2013 at 5:10
  • I just edited my post, I don't quite understand what to do with your code, could you explain please? Commented Apr 16, 2013 at 5:12
  • 3
    if you want to use this one function for any property given, you can use a parameter this.animateProperty(this.imgWindow, 0, "height");, where WindowItem.prototype.animateHeight = function(inElement, inTarget, cssProperty) ... that's what i meant. unless I misunderstood the question Commented Apr 16, 2013 at 5:15
  • @user2264587 Just write this as a short answer, so asimes can accept it and future visitors can benefit from this too. Commented Apr 16, 2013 at 7:24

1 Answer 1

1

as I wrote in my comment:

if you want to use this one function for any property given, you can use a parameter

this.animateProperty(this.imgWindow, 0, "height");

where

WindowItem.prototype.animateHeight = function(inElement, inTarget, cssProperty)
...

and instead of

inElement.style.height

use

inElement.style[cssProperty]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.