Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Twitter Bootstrap popover hide animation

I would like to implement my own popover hide animation. Currently, I am modifying bootstrap.js directly.

$.fn.popover = function (option) {
            return this.each(function () {
                var $this = $(this)
                  , data = $this.data('popover')
                  , options = typeof option == 'object' && option
                if (!data) $this.data('popover', (data = new Popover(this, options)))

                //original code
                // if (typeof option == 'string') data[option]()
                //custom code
                if (typeof option == 'string') {
                    if (option === 'hide') {
                        //my customize animation here
                    }
                    else {
                        data[option]();
                    }

                }

            })
        }

I would like to know if there are anyway so I can achieve a dynamic animation when I init the popover

$('#target').popover({
    hide: function () {
        //my own animation
    }
});
like image 211
Kuroro Avatar asked Dec 09 '25 22:12

Kuroro


1 Answers

Nice question-brain-teaser! You can definitely do it. Take a look how you can extend plugin without messing with original source code:

$.fn.popover = function (orig) {
  return function(options) {
    return this.each(function() {
      orig.call($(this), options);
      if (typeof options.hide == 'function') {
        $(this).data('bs.popover').hide = function() {
          options.hide.call(this.$tip, this);
          orig.Constructor.prototype.hide.call(this);
        };
      }
    });
  }
}($.fn.popover);

Here we go! We extended default popover functionality with our own. Now lets use it:

$('.three').popover({
  placement: 'bottom',
  hide: function() {
    $(this).animate({marginTop: -100}, function() {
      $(this).css({marginTop: ''});
    });
  }
});

Above popover will have custom animation effect while hiding.

Of course if you don't provide hide option you will have default behavior.

Demo: http://jsfiddle.net/VHDwa/71/

like image 107
dfsq Avatar answered Dec 11 '25 10:12

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!