Tooltip - 图1

Tooltip

Tooltips display informative text when users hover over, or tap an target element.

Tooltip can be positioned around any element with any HTML content inside.

Tooltip Layout

Tooltip is a JavaScript-only component, it doesn’t have any HTML layout.

Tooltip App Methods

We need to create/initialize the Tooltip. Let’s look at related App methods to work with Tooltip:

app.tooltip.create(parameters)- create Tooltip instance

  • parameters - object. Object with tooltip parameters

Method returns created Tooltip instance

app.tooltip.destroy(targetEl)- destroy Tooltip instance

  • targetEl - HTMLElement or string (with CSS Selector) or object (instance). Tooltip element or Tooltip instance to destroy.

app.tooltip.get(targetEl)- get Tooltip instance by its target HTML element

  • targetEl - HTMLElement or string (with CSS Selector). Tooltip target element.

Method returns Tooltip instance

app.tooltip.show(targetEl)- show Tooltip

  • targetEl - HTMLElement or string (with CSS Selector). Tooltip target element.

Method returns Tooltip instance

app.tooltip.hide(targetEl)- hide Tooltip

  • el - HTMLElement or string (with CSS Selector). Tooltip target element.

Method returns Tooltip instance

app.tooltip.setText(targetEl, text)- change Tooltip text

  • el - HTMLElement or string (with CSS Selector). Tooltip target element.
  • text - string - new text to set in specified Tooltip.

Method returns Tooltip instance

Tooltip Parameters

Now let’s look at list of available parameters we need to create Tooltip:

ParameterTypeDefaultDescription
targetElHTMLElement
string
Tooltip target element. Tooltip will be shown around this element. HTMLElement or string with CSS selector of tooltip target element
textstringTooltip text or HTML content
cssClassstringAdditional css class will be added to Tooltip element. Can be used for additional tooltip styling
renderfunction (tooltip)Function to render tooltip element, must return full tooltip HTML layout string
onobjectObject with events handlers. For example:
  1. var tooltip = app.tooltip.create({
  2. targetEl: ‘.some-link’,
  3. on: {
  4. show: function () {
  5. console.log(‘Tooltip became visible’)
  6. }
  7. }
  8. })

Tooltip Methods & Properties

So to create Tooltip we have to call:

  1. var tooltip = app.tooltip.create({ /* parameters */ })

After that we have its initialized instance (like tooltip variable in example above) with useful methods and properties:

Properties
tooltip.appLink to global app instance
tooltip.targetElTooltip target HTML element
tooltip.$targetElDom7 instance with tooltip target HTML element
tooltip.elTooltip itself HTML element
tooltip.$elDom7 instance with tooltip HTML element
tooltip.textTooltip text/content
tooltip.openedBoolean property indicating whether it is opened/visible or not
tooltip.paramsTooltip parameters
Methods
tooltip.show(targetEl)Show tooltip around targetEl element. If targetEl is not specified, then it will use targetEl passed in parameters on initialization
tooltip.hide()Hide tooltip
tooltip.setText(text)Change tooltip text or HTML content to the new one
tooltip.destroy()Destroys tooltip instance
tooltip.on(event, handler)Add event handler
tooltip.once(event, handler)Add event handler that will be removed after it was fired
tooltip.off(event, handler)Remove event handler
tooltip.off(event)Remove all handlers for specified event
tooltip.emit(event, …args)Fire event on instance

Tooltip Events

Tooltip will fire the following DOM events on tooltip element and events on app and tooltip instance:

DOM Events

EventTargetDescription
tooltip:showTooltip Element
Tooltip Target
Event will be triggered when Tooltip becomes visible
tooltip:hideTooltip Element
Tooltip Target
Event will be triggered when Tooltip becomes hidden
tooltip:beforedestroyTooltip Element
Tooltip Target
Event will be triggered right before Tooltip instance will be destroyed

App and Tooltip Instance Events

Tooltip instance emits events on both self instance and app instance. App instance events has same names prefixed with tooltip.

EventArgumentsTargetDescription
show(tooltip)tooltipEvent will be triggered when Tooltip becomes visible. As an argument event handler receives Tooltip instance
tooltipShow(tooltip)app
hide(tooltip)tooltipEvent will be triggered when Tooltip becomes hidden. As an argument event handler receives Toolitp instance
tooltipHide(tooltip)app
beforeDestroy(tooltip)tooltipEvent will be triggered right before Tooltip instance will be destroyed. As an argument event handler receives Tooltip instance
tooltipBeforeDestroy(tooltip)app

Tooltip Auto Initialization

If you don’t need to use Tooltip API and your Tooltip target element is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding tooltip-init class to target element, and specifying tooltip text in its data-tooltip attribute:

  1. <!-- Add tooltip-init class and specify tooltip text in data-tooltip attribute -->
  2. <a href="/profile/" class="link tooltip-init profile-link" data-tooltip="Profile settings">
  3. <i class="profile-icon"></i>
  4. </a>

In this case if you need to access created Tooltip instance you can use the app.tooltip.get app method:

  1. var tooltip = app.tooltip.get('.profile-link');
  2. // change tooltip text
  3. tooltip.setText('Profile');

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-tooltip-bg-color: rgba(0, 0, 0, 0.87);
  3. --f7-tooltip-text-color: #fff;
  4. --f7-tooltip-border-radius: 4px;
  5. --f7-tooltip-padding: 8px 16px;
  6. --f7-tooltip-font-size: 14px;
  7. --f7-tooltip-font-weight: 500;
  8. --f7-tooltip-desktop-padding: 6px 8px;
  9. --f7-tooltip-desktop-font-size: 12px;
  10. }

Examples

  1. <!-- navbar with link with tooltip -->
  2. <div class="navbar">
  3. <div class="navbar-inner sliding">
  4. <div class="title">Tooltip</div>
  5. <div class="right">
  6. <a href="#" class="link navbar-tooltip">
  7. <i class="icon f7-icons if-not-md">info</i>
  8. <i class="icon material-icons if-md">info_outline</i>
  9. </a>
  10. </div>
  11. </div>
  12. </div>
  13. ...
  14. <!-- icons with tooltip in text -->
  15. <div class="block block-strong">
  16. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec lacinia augue urna, in tincidunt augue hendrerit ut. In nulla massa, facilisis non consectetur a, tempus semper ex. Proin eget volutpat nisl. Integer lacinia maximus nunc molestie viverra. <i class="icon f7-icons color-blue icon-tooltip if-not-md">info_fill</i> <i class="icon material-icons color-blue icon-tooltip if-md">info</i> Etiam ullamcorper ultricies ipsum, ut congue tortor rutrum at. Vestibulum rutrum risus a orci dictum, in placerat leo finibus...
  17. ...
  18. </div>
  19. ...
  20. <!-- button with auto init tooltip -->
  21. <a class="button button-round button-outline button-small tooltip-init" data-tooltip="Button tooltip text">Button with Tooltip</a>
  1. // icons tooltip. One tooltip for all icons with "icon-tooltip" class
  2. var iconTooltip = app.tooltip.create({
  3. targetEl: '.icon-tooltip',
  4. text: 'Tooltip text',
  5. });
  6. // navbar link tooltip
  7. var navbarTooltip = app.tooltip.create({
  8. targetEl: '.navbar-tooltip',
  9. text: 'One more tooltip<br>with more text<br><em>and custom formatting</em>'
  10. });