0

maybe it stupid question.

example`

                nActive.find(".left").stop(true, true).animate({
                    top: -windowH
                }, 1000).promise().done(function() {
                    nestedparallax.eq(np).addClass("n-active");
                    nestedparallax.eq(np).removeClass("n-showD");
                    nestedparallax.eq(current).removeClass("n-active");
                    setTimeout(function() {
                        flag = false;
                    }, 0);
                });

I have animation like this`

how can i change "top" vith variable

for example var position = top it will write in animation top and if position = left it will take left on animation function.

Thank you.

Aram Mkrtchyan
  • 2,690
  • 4
  • 31
  • 47

2 Answers2

3

You can use object square bracket notation:

var oAnim = {}, position = 'top';
oAnim[position] = -windowH
nActive.find(".left").stop(true, true).animate(oAnim, 1000).promise(/*...*/);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

You need to calculate it before:

 var position = $('.left').offset();
 var left = position.left;
 var top = position.top;

  nActive.find(".left").stop(true, true).animate({
      top: (left < 0) ? top : left // this is an example of tertiary condition
  }, 1000).promise().done(function() {
      nestedparallax.eq(np).addClass("n-active");
      nestedparallax.eq(np).removeClass("n-showD");
      nestedparallax.eq(current).removeClass("n-active");
      setTimeout(function() {
           flag = false;
      }, 0);
  });

Maybe we can help better if you explain what you need better.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69