9

I am using zepto library for my mobile web site. I have recently learnt that zepto does not have slideDown() plugin like jquery. I would like to implement the same for zepto.

I have tried one on jsfiddle (http://jsfiddle.net/goje87/keHMp/1/). Here it does not animate while showing the element. It just flashes down. How do I bring in the animation?

PS: I cannot provide a fixed height because I would be applying this plugin to the elements whose height property would not be known.

Thanks in advace!!

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
Goje87
  • 2,839
  • 7
  • 28
  • 48

4 Answers4

19

Demo: http://jsfiddle.net/6zkSX/5

JavaScript:

(function ($) {
  $.fn.slideDown = function (duration) {    
    // get old position to restore it then
    var position = this.css('position');

    // show element if it is hidden (it is needed if display is none)
    this.show();

    // place it so it displays as usually but hidden
    this.css({
      position: 'absolute',
      visibility: 'hidden'
    });

    // get naturally height
    var height = this.height();

    // set initial css for animation
    this.css({
      position: position,
      visibility: 'visible',
      overflow: 'hidden',
      height: 0
    });

    // animate to gotten height
    this.animate({
      height: height
    }, duration);
  };
})(Zepto);

$(function () {
  $('.slide-trigger').on('click', function () {
    $('.slide').slideDown(2000);
  });
});​
​
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
5

This worked for me:

https://github.com/Ilycite/zepto-slide-transition

The Zepto Slide Transition plugin add to Zepto.js the functions bellow :

slideDown();

slideUp();

slideToggle();

Community
  • 1
  • 1
rauff
  • 173
  • 4
  • 10
1

Speransky's answer was helpful, and I'm offering a simplified alternative for a common drop-down navigation list, and separated into slideUp and slideDown on jsfiddle: http://jsfiddle.net/kUG3U/1/

$.fn.slideDown = function (duration) {
    // show element if it is hidden (it is needed if display is none)
    this.show();

    // get naturally height
    var height = this.height();

    // set initial css for animation
    this.css({
        height: 0
    });

    // animate to gotten height
    this.animate({
        height: height
    }, duration);
};
Frank Yin
  • 1,920
  • 1
  • 17
  • 12
  • 1
    Just a small fix: If the element has vertical padding, after the first call the height is incorrectly calculated, resulting in a bigger object. To prevent this, use: `var height = this.height() - parseInt(this.css('padding-top')) - parseInt(this.css('padding-bottom'));` – andreszs Mar 08 '15 at 23:56
1

This would work for what you need: https://github.com/NinjaBCN/zepto-slide-transition

kennersky
  • 21
  • 3