0
var animations = {
    slide : {enter:'slideDown('+settings.speed+')', exit:'slideUp('+settings.speed+')'},
    fade : {enter:'fadeIn('+settings.speed+')', exit:'fadeOut('+settings.speed+')'}
};

I currently have a literal object which has enter and exit animations defined within. Would it be possible to call these dynamically and how?

Basically, i am trying to find a way to dynamically call functions whilst keeping the code as small as possible.

  • check this answer mate, it should help you http://stackoverflow.com/questions/11338967/jquery-issue-on-extending-jquery-functionality/11339046#11339046 – Barlas Apaydin Jul 08 '12 at 17:32
  • 2
    There's definitely a way to do this, but for the best possible answer it would help if you show us how you would want to call these -- a "best case imaginable" scenario. The exact solution would depend on that. – Jon Jul 08 '12 at 17:37
  • Because most of the plugin is dynamic, i would think something along the lines of ``animation.enter`` and ``animation.exit`` would be enough. –  Jul 08 '12 at 17:42

1 Answers1

0

You can use this syntax: $(obj)[action](htmlStr);

Fiddle: http://jsfiddle.net/svvdw/

var settings = {
    speed: 'slow'
};

var animations = {
    slide : {enter:'slideDown', exit:'slideUp'},
    fade : {enter:'fadeIn', exit:'fadeOut'}
};

$("#test")[animations.fade.exit](settings.speed);

Bill
  • 645
  • 3
  • 11