【问题标题】:QTip2 dynamic show/hide events and slideDownQTip2动态显示/隐藏事件和slideDown
【发布时间】:2014-10-31 18:46:20
【问题描述】:

我正在尝试根据目标的属性动态更改显示和隐藏事件。例如,有时我可能想要鼠标悬停,而其他时候单击。同样,我希望能够调整效果,比如调整slideDown的时机。

我找到了一种在Is it possible to set position of a Qtip programmatically? 处动态调整位置的方法,但由于它在节目中运行,这显然行不通。

content.text 可以使用回调函数,但我不相信 show 可以。

那么,我如何动态更改显示和隐藏(最好是 slideDown 时间)?

工作代码如下,不工作的部分已注释掉。请注意,我可以更改提示大小,但如果我更改高度,工具提示将无法正确显示——直到我滚动窗口。这不是那么重要,而更改显示和隐藏事件才是。

function setupQtips()
{
    $("*[qtipiddiv]").each
    (
        function ()
        {
            $(this).qtip
            (
                {
                    overwrite: false,
                    content:
                    {
                        text: function (event, api)
                        {
                            var strId = $(this).attr('qtipiddiv');
                            return ($('#' + strId));
                        },

                        title:
                        {
                            text: function (event, api)
                            {
                                return ($(this).attr('qtiptitle'));
                            }
                        }
                    },

                    show: 
                    {
                        event: 'click',

                        effect: function (offset)
                        {
                            $(this).slideDown(100);
                        },

                        solo: true
                    },  

                    hide:
                    {
                        event: 'unfocus'
                    },

                    position:
                    {
                        viewport: $(window),

                        adjust:
                        {
                            screen: true
                        }
                    },

                    style: 
                    {
                        classes: 'qtip-rounded qtip-shadow'
                    },

                    events:
                    {
                        show: function (event, api)
                        {
                            //Position
                            var elmTarget = $(api.elements.target[0]);
                            var strPositionMy = elmTarget.attr('qtippositionmy');
                            if (strPositionMy != undefined)
                            {
                                elmTarget.qtip('option', 'position.my', strPositionMy);
                            }

                            var strPositionAt = elmTarget.attr('qtippositionat');
                            if (strPositionAt != undefined)
                            {
                                elmTarget.qtip('option', 'position.at', strPositionAt);
                            }

                            //Height / width
                            var strWidth = elmTarget.attr('qtipwidth');
                            if (strWidth != undefined)
                            {
                                elmTarget.qtip('option', 'style.width', strWidth);
                            }

                            var strHeight = elmTarget.attr('qtipheight');
                            if (strHeight != undefined)
                            {
                                elmTarget.qtip('option', 'style.height', strHeight);
                            }

                            //Tip Height / width
                            //var strTipWidth = elmTarget.attr('qtiptipwidth');
                            //if (strTipWidth != undefined)
                            //{
                            //    elmTarget.qtip('option', 'style.tip.width', strTipWidth);
                            //}

                            //var strTipHeight = elmTarget.attr('qtiptipheight');
                            //if (strTipHeight != undefined)
                            //{
                            //    elmTarget.qtip('option', 'style.tip.height', strTipHeight);
                            //    //api.set('style.tip.height', strTipHeight);
                            //}

                            //Title Button
                            var strTitleButton = elmTarget.attr('qtipbutton');
                            if (strTitleButton != undefined)
                            {
                                elmTarget.qtip('option', 'content.title.button', true);
                            }

                            //var strSlide = elmTarget.attr('qtipslide');
                            //if (strSlide != undefined)
                            //{
                            //    elmTarget.qtip('option', 'show.effect.slideDown', strSlide);
                            //}
                        }
                    }
                }
            )
        }
    );

    return;
}

【问题讨论】:

  • “要求代码的问题必须证明对正在解决的问题有最低限度的了解。包括尝试的解决方案,它们为什么不起作用,以及预期的结果。另见:Stack Overflow question checklist".

标签: jquery qtip2


【解决方案1】:

经过一闪而过的反复试验,我发现至少有三种方法可以动态设置 Qtip 选项。

  1. 在 qtip 属性之外设置变量并使用这些变量
  2. 使用回调函数(如果可用)
  3. 使用事件函数

我确信还有其他方法可以实现这一点,但下面的代码适用于我需要的所有情况,并演示了所有三种方法。我需要至少使用两个,因为fadeIn 方法不允许我只使用一个变量。我也不能只在 intFade 未定义时才调用 fadeIn 方法。

所以,希望这个答案对其他人有所帮助。

  function setupQtips()
  {
      $("*[qtipiddiv]").each
      (
          function ()
          {
              //Title
              var elmTarget = $(this);
              var strTitle = elmTarget.attr('qtiptitle');
              if (strTitle == undefined)
              {
                  strTitle = false;
              }

              //Title Button
              var binTitleButton = elmTarget.attr('qtipbutton');
              if (binTitleButton == undefined)
              {
                  binTitleButton = false;
              }

              //Show
              var strShow = elmTarget.attr('qtipshow');
              if (strShow == undefined)
              {
                  strShow = 'click';
              }

              if (strShow == 'false')
              {
                  strShow = false;
              }

              //Hide
              var strHide = elmTarget.attr('qtiphide');
              if (strHide == undefined)
              {
                  strHide = 'unfocus';
              }

              if (strHide == 'false')
              {
                  strHide = false;
              }

              //Modal
              var binModal = elmTarget.attr('qtipmodal');
              var binBlur = false;
              if (binModal == undefined)
              {
                  binModal = false;
              }
              else if (strHide == 'unfocus')
              {
                  binBlur = true;
              }

              //Tip Height / width
              var intTipWidth = elmTarget.attr('qtiptipwidth');
              if (intTipWidth == undefined)
              {
                  intTipWidth = 6;
              }

              var intTipHeight = elmTarget.attr('qtiptipheight');
              if (intTipHeight == undefined)
              {
                  intTipHeight = 6;
              }

              //Style
              var strStyle = elmTarget.attr('qtipstyle');
              if (strStyle == undefined)
              {
                  strStyle = '';
              }

              //____________________________________________________
              //Set qtip
              $(this).qtip
              (
                  {
                      overwrite: false,
                      content:
                      {
                          text: function (event, api)
                          {
                              var strId = $(this).attr('qtipiddiv');
                              return ($('#' + strId));
                          },

                          title:
                          {
                              text: strTitle,

                              button: binTitleButton
                          }
                      },

                      show: 
                      {
                          event: strShow,

                          effect: function (offset)
                          {
                              var strFade = offset.target.attr('qtipfade');
                              if (strFade == undefined)
                              {
                                  $(this).fadeIn(0);
                              }
                              else
                              {
                                  var intFade = parseInt(strFade);
                                  $(this).fadeIn(intFade);
                              }
                          },

                          solo: true,
                          modal:
                          {
                              on: binModal,
                              blur: binBlur
                          }
                      },  

                      hide:
                      {
                          event: strHide
                      },

                      position:
                      {
                          viewport: $(window),

                          adjust:
                          {
                              screen: true
                          }
                      },

                      style: 
                      {
                          classes: 'qtip-rounded qtip-shadow ' + strStyle,
                          tip:
                          {
                              width: intTipWidth,
                              height: intTipHeight
                          }
                      },

                      events:
                      {
                          show: function (event, api)
                          {
                              //Position
                              var elmTarget = $(api.elements.target[0]);
                              var strPositionMy = elmTarget.attr('qtippositionmy');
                              if (strPositionMy != undefined)
                              {
                                  elmTarget.qtip('option', 'position.my', strPositionMy);
                              }

                              var strPositionAt = elmTarget.attr('qtippositionat');
                              if (strPositionAt != undefined)
                              {
                                  elmTarget.qtip('option', 'position.at', strPositionAt);
                              }

                              //Height / width
                              var strWidth = elmTarget.attr('qtipwidth');
                              if (strWidth != undefined)
                              {
                                  elmTarget.qtip('option', 'style.width', strWidth);
                              }

                              var strHeight = elmTarget.attr('qtipheight');
                              if (strHeight != undefined)
                              {
                                  elmTarget.qtip('option', 'style.height', strHeight);
                              }
                          }
                      }
                  }
              )
          }
      );

      return;
  }

【讨论】:

    【解决方案2】:
            $('.tip').qtip({
                position: {
                    effect: false
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2014-03-09
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多