【问题标题】:How to set a delay for L.Tooltip display?如何设置 L.Tooltip 显示的延迟?
【发布时间】:2018-04-14 16:10:06
【问题描述】:

我想知道如何设置鼠标悬停时工具提示可视化的延迟。我在文档的选项描述中没有找到这样的功能。

我的地图上有很多标记,所以当鼠标四处移动时,总是会出现一些工具提示。我的想法是设置一些延迟,例如在悬停 1 秒后,显示此特定标记的工具提示。

谢谢!

【问题讨论】:

    标签: javascript leaflet tooltip


    【解决方案1】:

    我还需要延迟功能,所以我从旧的 Tooltip 插件中获取了它并做了一个快速的解决方法,它可能可以做得更好,但我没有时间改进它。

    我扩展了绑定工具提示、打开和关闭方法,并将“showDelay”和“hideDelay”属性添加到 L.Layer 类。我也在“initTooltipInteractions”添加了一个点击事件来关闭工具提示,因为它在我的具体情况下是有意义的,但你可以把它关闭。

    只需将其添加到 javascript 文件并在传单之后加载:

     L.Layer.include({
    
         showDelay: 1200,
         hideDelay: 100,
    
         bindTooltipDelayed: function (content, options) {
    
             if (content instanceof L.Tooltip) {
                 L.setOptions(content, options);
                 this._tooltip = content;
                 content._source = this;
             } else {
                 if (!this._tooltip || options) {
                     this._tooltip = new L.Tooltip(options, this);
                 }
                 this._tooltip.setContent(content);
    
             }
    
             this._initTooltipInteractionsDelayed();
    
             if (this._tooltip.options.permanent && this._map && this._map.hasLayer(this)) {
                 this.openTooltipWithDelay();
             }
    
             return this;
         },
    
         _openTooltipDelayed: function (e) {
             var layer = e.layer || e.target;
    
             if (!this._tooltip || !this._map) {
                 return;
             }
             this.openTooltipWithDelay(layer, this._tooltip.options.sticky ? e.latlng : undefined);
         },
    
         openTooltipDelayed: function (layer, latlng) {
             if (!(layer instanceof L.Layer)) {
                 latlng = layer;
                 layer = this;
             }
             if (layer instanceof L.FeatureGroup) {
                 for (var id in this._layers) {
                     layer = this._layers[id];
                     break;
                 }
             }
             if (!latlng) {
                 latlng = layer.getCenter ? layer.getCenter() : layer.getLatLng();
             }
             if (this._tooltip && this._map) {
                 this._tooltip._source = layer;
                 this._tooltip.update();
                 this._map.openTooltip(this._tooltip, latlng);
                 if (this._tooltip.options.interactive && this._tooltip._container) {
                     addClass(this._tooltip._container, 'leaflet-clickable');
                     this.addInteractiveTarget(this._tooltip._container);
                 }
             }
    
             layer.fireEvent('mousemove', lastMouseEvent);
    
             return this;
         },
         openTooltipWithDelay: function (t, i) {
             this._delay(this.openTooltipDelayed, this, this.showDelay, t, i);
         },
         closeTooltipDelayed: function () {
             if (this._tooltip) {
                 this._tooltip._close();
                 if (this._tooltip.options.interactive && this._tooltip._container) {
                     removeClass(this._tooltip._container, 'leaflet-clickable');
                     this.removeInteractiveTarget(this._tooltip._container);
                 }
             }
             return this;
         },
         closeTooltipWithDelay: function () {
             clearTimeout(this._timeout);
             this._delay(this.closeTooltipDelayed, this, this.hideDelay);
         },
         _delay: function (func, scope, delay, t, i) {
             var me = this;
             if (this._timeout) {
                 clearTimeout(this._timeout)
             }
             this._timeout = setTimeout(function () {
                 func.call(scope, t, i);
                 delete me._timeout
             }, delay)
         },
         _initTooltipInteractionsDelayed: function (remove$$1) {
             if (!remove$$1 && this._tooltipHandlersAdded) { return; }
             var onOff = remove$$1 ? 'off' : 'on',
                events = {
                    remove: this.closeTooltipWithDelay,
                    move: this._moveTooltip
                };
             if (!this._tooltip.options.permanent) {
                 events.mouseover = this._openTooltipDelayed;
                 events.mouseout = this.closeTooltipWithDelay;
                 events.click = this.closeTooltipWithDelay;
                 if (this._tooltip.options.sticky) {
                     events.mousemove = this._moveTooltip;
                 }
                 if (L.touch) {
                     events.click = this._openTooltipDelayed;
                 }
             } else {
                 events.add = this._openTooltipDelayed;
             }
             this[onOff](events);
             this._tooltipHandlersAdded = !remove$$1;
         }
     });
    

    然后使用它:

    layer.showDelay = 800; //use 0 for no delay behavior
    layer.hideDelay = 2000; //use 0 for normal behavior
    layer.bindTooltipDelayed("text", tooltipOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多