【问题标题】:How to customize Twitter Bootstrap popover hide animation如何自定义 Twitter Bootstrap 弹出框隐藏动画
【发布时间】:2014-02-11 11:43:31
【问题描述】:

我想实现我自己的弹出框隐藏动画。目前,我正在直接修改 bootstrap.js。

$.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]();
                    }

                }

            })
        }

我想知道是否有这样我可以在初始化弹出框时实现动态动画

$('#target').popover({
    hide: function () {
        //my own animation
    }
});

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    很好的问题脑筋急转弯!你绝对可以做到。看看如何在不弄乱原始源代码的情况下扩展插件:

    $.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);
    

    我们来了!我们用自己的扩展了默认的弹出框功能。现在让我们使用它:

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

    popover上方隐藏时会有自定义动画效果。

    当然,如果您不提供hide 选项,您将拥有默认行为。

    演示: http://jsfiddle.net/VHDwa/71/

    【讨论】:

    • 您好@dfsq,您能否在显示弹出窗口时提供动画代码,我自己尝试过,但在控制台中出现错误“未捕获的类型错误:无法读取未定义的属性'原型'。”。也是第一次,它没有动画。视频链接是:youtu.be/DGJ7BPubVgo 谢谢期待。
    • 我已在您的网络联系我表单中向您发送了我的一段代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2013-02-20
    • 2012-08-21
    • 2014-08-11
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    相关资源
    最近更新 更多