jquery ui tooltip manual open /close

后端 未结 5 475
囚心锁ツ
囚心锁ツ 2020-12-24 07:48

is there a way to manually open close the jquery ui tooltip? I just want it to react to a click event toggling on/off. You can unbind all mouse events and it will rebind t

相关标签:
5条回答
  • 2020-12-24 08:19

    The tooltip have a disable option. Well i used it and here is the code:

    $('a').tooltip({
        disabled: true    
    }).click(function(){    
        if($(this).tooltip('option', 'disabled'))
            $(this).tooltip('option', {disabled: false}).tooltip('open');
        else
            $(this).tooltip('option', {disabled: true}).tooltip('close');
    }).hover(function(){
        $(this).tooltip('option', {disabled: true}).tooltip('close');
    }, function(){
        $(this).tooltip('option', {disabled: true}).tooltip('close');
    });
    
    0 讨论(0)
  • 2020-12-24 08:27

    If you want to just unbind the events and woudn't like to make your own custom tooltip.

    $("#some-id").tooltip(tooltip_settings)
                 .on('mouseout focusout', function(event) {
                      event.stopImmediatePropagation();
                 });
    
    $("#some-id").attr("title", "Message");
    $("#some-id").tooltip("open");
    

    mouseout blocks the tooltop disappearing by moving the mouse cursor

    focusout blocks the tooltop disappearing by keyboard navigation

    0 讨论(0)
  • 2020-12-24 08:27

    Related to my other comment, I looked into the original code and achieved manual open/close by extending the widget and adding a autoHide option with version JQuery-UI v1.10.3. Basically I just remove the mouse listeners that were added in _create and the internal _open call.

    Edit: Separated autoHide and autoShow as two separate flags as suggested by @MscG

    Demo Here: http://jsfiddle.net/BfSz3/

    (function( $ ) {
      $.widget( "custom.tooltipX", $.ui.tooltip, {
        options: {
          autoHide:true,
          autoShow: true
        },
    
        _create: function() {
          this._super();
          if(!this.options.autoShow){
            this._off(this.element, "mouseover focusin");
          }
        },
    
        _open: function( event, target, content ) {
          this._superApply(arguments);
    
          if(!this.options.autoHide){
            this._off(target, "mouseleave focusout");
          }
        }
      });
    
    }( jQuery ) );
    

    Now when you initialize you can set the tooltip to manually show or hide by setting autoHide : false:

     $(someDOM).tooltipX({ autoHide:false }); 
    

    And just directly perform standard open/close calls in your code as needed elsewhere

     $(someDOM).tooltipX("open"); // displays tooltip
     $(someDOM).tooltipX("close"); // closes tooltip
    

    A simple hotfix, until I have the time to do official pull request, this will have to do.

    0 讨论(0)
  • 2020-12-24 08:32

    Some compilation from other SO questions.

    Example Show tooltip on hint click, and hide tooltip on elsevere click

    $(document).on('click', '.hint', function(){ //init new tooltip on click
       $(this).tooltip({
          position: { my: 'left+15 center', at: 'center right' },
          show: false,
          hide: false
       }).tooltip('open'); // show new tooltip
    }).on('click', function(event){ // click everywhere
       if(!$(event.target).hasClass('hint'))
         $(".hint").each(function(){
            var $element = $(this);
            if($element.data('ui-tooltip')) { // remove tooltip only from initialized elements
               $element.tooltip('destroy');
            }
         })
    });
    
    $('.hint').on('mouseout focusout', function(event) { // prevent auto hide tooltip 
        event.stopImmediatePropagation();
    });
    
    0 讨论(0)
  • 2020-12-24 08:35

    jltwoo, can I suggest to use two different boolean switches to enable auto-open and auto-close? With this change your code will look like this:

    (function( $ ) {
      $.widget( "custom.tooltipX", $.ui.tooltip, {
        options: {
            autoShow: true,
            autoHide: true
        },
    
        _create: function() {
          this._super();
          if(!this.options.autoShow){
            this._off(this.element, "mouseover focusin");
          }
        },
    
        _open: function( event, target, content ) {
          this._superApply(arguments);
    
          if(!this.options.autoHide){
            this._off(target, "mouseleave focusout");
          }
        }
      });
    
    }( jQuery ) );
    

    In this way, initializing the tooltip as:

    $(someDOM).tooltipX({ autoHide:false });
    

    it shows by itself when the mouse is over the element but you have to manually close it.

    If you want to manually control both open and close actions, you can simply use:

    $(someDOM).tooltipX({ autoShow:false, autoHide:false });
    
    0 讨论(0)
提交回复
热议问题