Twitter Bootstrap Popover/Tooltip Bug with Mobile?

后端 未结 6 1144
执念已碎
执念已碎 2021-02-19 05:24

I am working with Twitter Bootstrap and ran into something I could not fix when testing on iPad and iPhone. On mobile (at least those devices) you need to click to engage the t

6条回答
  •  旧时难觅i
    2021-02-19 06:09

    Bootstap-tooltip v3.3.7

    Actual: tooltip on hover doesn't work with touch devices in our project

    Solution: Subscribe to tooltip's show event and call mouseenter

    $body = $('body');
    
    $body.tooltip({selector: '.js-tooltip'});
    
    // fix for touch device.
    if (Modernizr.touch) { // to detect you can use https://modernizr.com
      var hideTooltip = function(e) {
        tooltipClicked = !!$(e.target).closest('.tooltip').length;
        if (tooltipClicked) { return; }
    
        $('.js-tooltip').tooltip('hide');
      }
      var emulateClickOnTooltip = function(e) {
        tooltipsVisible = !!$('.tooltip.in').length;
        if (tooltipsVisible) { return; }
    
        $(e.target).mouseenter();
      }
      var onTooltipShow = function(e) {
        tooltipClicked = !!$(e.target).closest('.tooltip').length;
        if (tooltipClicked) { return; }
    
        $body.on('touchend', hideTooltip); 
      }
      var onTooltipHide = function() {
        $body.off('touchend', hideTooltip);
      }
    
      $body
        .on('touchend', '.js-tooltip', emulateClickOnTooltip)
        .on('show.bs.tooltip', onTooltipShow)
        .on('hide.bs.tooltip', onTooltipHide);
    }
    

提交回复
热议问题