javascript - Set div height to default in a jQuery animation - Stack Overflow

I have created a drop-down-menu, the html for the drop-down part basically looks like this:<div clas

I have created a drop-down-menu, the html for the drop-down part basically looks like this:

<div class="menu-item">
  <!-- Menu title -->
  <div class="drop-down">
    <!-- Content -->
  </div>
</div>

I want to animate this using jQuery-Code (with the easing-plugin), but the following Code does not work:

$(".menu-item").mouseenter(activate);
$(".menu-item").mouseleave(deactivate);

function deactivate()
{
  var dropdown = $(this).find("div.drop-down");
  dropdown.stop().animate(
    {height:     '0px'},
    {queue:       false,
     duration:    600,
     easing:      'easeOut'
    }
  );
}

function activate()
{
  var dropdown = $(this).find("div.drop-down");
  dropdown.stop().animate(
    {height:     'auto'},
    {queue:       false,
     duration:    600,
     easing:      'easeOut'
    }
  );
}

The message in the error console is: "Warning: Error in parsing value for 'height'. Declaration dropped."

If I use "height: '100px'" or somthing similar in the activate-Function it works as expected. But for maintainability reasons i want the height to be calculated autmatically, so the drop-down adapts its size to its content.

How can this be achieved?

Greetings, Jost

I have created a drop-down-menu, the html for the drop-down part basically looks like this:

<div class="menu-item">
  <!-- Menu title -->
  <div class="drop-down">
    <!-- Content -->
  </div>
</div>

I want to animate this using jQuery-Code (with the easing-plugin), but the following Code does not work:

$(".menu-item").mouseenter(activate);
$(".menu-item").mouseleave(deactivate);

function deactivate()
{
  var dropdown = $(this).find("div.drop-down");
  dropdown.stop().animate(
    {height:     '0px'},
    {queue:       false,
     duration:    600,
     easing:      'easeOut'
    }
  );
}

function activate()
{
  var dropdown = $(this).find("div.drop-down");
  dropdown.stop().animate(
    {height:     'auto'},
    {queue:       false,
     duration:    600,
     easing:      'easeOut'
    }
  );
}

The message in the error console is: "Warning: Error in parsing value for 'height'. Declaration dropped."

If I use "height: '100px'" or somthing similar in the activate-Function it works as expected. But for maintainability reasons i want the height to be calculated autmatically, so the drop-down adapts its size to its content.

How can this be achieved?

Greetings, Jost

Share Improve this question asked May 13, 2011 at 16:36 JostJost 5,8609 gold badges44 silver badges73 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I would try to use slideUp() and slideDown() for this animation. Note that those functions accept easing functions.

Other option, if for some reason you need to use animate for this, you might want to do something like this in your activate function:

function activate(){
  var dropdown = $(this).find("div.drop-down");

  dropdown.css('height','auto')
          .hide()
          .stop()
          .animate(
             {height:     'auto'},
             {queue:       false,
              duration:    600,
              easing:      'easeOut'
          });
}

One solution could be store the height value in the deactivate method and use it when activating. I do not think that jQuery supports animating a dimension property to a string value.

var menu_handler = (function(){
  var orig_height = 0;
  return {
    deactivate : function deactivate () {
      var dropdown = $(this).find("div.drop-down");
      orig_height = dropdown.height();
      dropdown.stop().animate(
        {height:     '0px'},
        {queue:       false,
         duration:    600,
         easing:      'easeOut'
        }
      );
    },
    activate : function activate () {
      var dropdown = $(this).find("div.drop-down");
      dropdown.stop().animate(
        {height:     orig_height},
        {queue:       false,
         duration:    600,
         easing:      'easeOut'
        }
      );
    }
  };
}

$(".menu-item").mouseenter(menu_handler.activate));
$(".menu-item").mouseleave(menu_handler.deactivate));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745441139a4627837.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信