Floating Action Button / FAB - 图1

Floating Action Button

Floating action button (FAB) is used for a promoted action. They are distinguished by a circled icon floating above the UI and have motion behaviors that include morphing, launching, and a transferring anchor point.

FAB Layout

Layout of floating action button is very simple. Just put it as the direct child of page or view:

  1. <!-- Page-->
  2. <div class="page">
  3. <!-- Navbar-->
  4. <div class="navbar">
  5. <div class="navbar-inner">
  6. <div class="center">Floating Action Button</div>
  7. </div>
  8. </div>
  9. <!-- Floating Action Button -->
  10. <div class="fab fab-right-bottom">
  11. <a href="#">
  12. <i class="icon f7-icons">add</i>
  13. </a>
  14. </div>
  15. <!-- Another Floating Action Button -->
  16. <div class="fab fab-left-bottom">
  17. <a href="#">
  18. <i class="icon f7-icons">add</i>
  19. </a>
  20. </div>
  21. <!-- Scrollable Page Content-->
  22. <div class="page-content">
  23. <div class="block">
  24. Lorem ipsum dolor sit amet, ....
  25. </div>
  26. </div>
  27. </div>

FAB position is configured via additional fab-[horizontal]-[vertical] class. The following clases are avaialble:

  • fab-right-bottom
  • fab-center-bottom
  • fab-left-bottom
  • fab-right-center
  • fab-center-center
  • fab-left-center
  • fab-right-top
  • fab-center-top
  • fab-left-top

FAB Colors

FAB supports all default colors. To change its color just add color-[color] class to FAB element.

  1. <!-- Red FAB -->
  2. <div class="fab fab-left-top color-red"></div>
  3. <!-- Green FAB -->
  4. <div class="fab fab-right-bottom color-green"></div>

Extended FAB

The extended FAB is wider, and it includes a text label. To make extended FAB we need to add additional fab-extended class to FAB element and put its extra text in <div class="fab-text"> element inside:

  1. <!-- Additional fab-extended class -->
  2. <div class="fab fab-extended fab-center-bottom">
  3. <a href="#">
  4. <i class="icon f7-icons">add</i>
  5. <!-- Element with FAB text -->
  6. <div class="fab-text">Create</div>
  7. </a>
  8. </div>

Speed Dial

The floating action button can fling out related actions upon press. The button should remain on screen after the menu is invoked. Tapping in the same spot should either activate the most commonly used action or close the open menu.

In this case we need to add additional element with buttons:

  1. <div class="fab fab-right-bottom">
  2. <a href="#">
  3. <!-- First icon is visible when Speed Dial actions are closed -->
  4. <i class="icon f7-icons">add</i>
  5. <!-- Second icon is visible when Speed Dial actions are opened -->
  6. <i class="icon f7-icons">close</i>
  7. </a>
  8. <!-- Speed Dial action buttons -->
  9. <div class="fab-buttons fab-buttons-bottom">
  10. <a href="#">1</a>
  11. <a href="#">2</a>
  12. <a href="#">3</a>
  13. </div>
  14. </div>

Speed dial buttons container position is configured via additional class:

  • fab-buttons-top - buttons will appear on top of FAB
  • fab-buttons-right - buttons will appear on right of FAB
  • fab-buttons-bottom - buttons will appear on bottom of FAB
  • fab-buttons-left - buttons will appear on left of FAB
  • fab-buttons-center - buttons will appear around of FAB

  • Note that Speed Dial actions buttons will appear in reversed order

  • You shouldn’t use more than 6 Speed Dial actions

  • You should use at least 3 Speed Dial actions

Speed Dial With Labels

Speed dial buttons also support additional text labels. In this case layout will be a bit extended:

  1. <div class="fab-buttons fab-buttons-bottom">
  2. <!-- additional "fab-label-button" class on fab button -->
  3. <a href="#" class="fab-label-button">
  4. <!-- original button content -->
  5. <span>1</span>
  6. <!-- button label -->
  7. <span class="fab-label">Action 1</span>
  8. </a>
  9. <a href="#" class="fab-label-button">
  10. <span>2</span>
  11. <span class="fab-label">Action 2</span>
  12. </a>
  13. ...
  14. </div>

FAB Morph

There is also ability for FAB to morph to any visible element on page.

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <!-- Toolbar has additional required "fab-morph-target" class -->
  4. <div class="toolbar toolbar-bottom fab-morph-target">
  5. ...
  6. </div>
  7. <!-- FAB will morph to toolbar -->
  8. <div class="fab fab-morph" data-morph-to=".toolbar">
  9. <i class="icon f7-icons">add</i>
  10. </div>
  11. <div class="page-content">
  12. ...
  13. </div>
  14. </div>

Where

  • data-morph-to - additional attribute on FAB where target element’s CSS selector must be specified. In this example it points to Toolbar
  • fab-morph-target - additional required class on FAB morph target, in this example it is added to Toolbar

FAB App Methods

We can use following app methods to control FABs:

app.fab.open(fabEl, targetEl)Open FAB speed dial actions/buttons, or morph it to specified target
  • fabEl - HTMLElement or string (with CSS Selector) of required FAB
  • targetEl - HTMLElement or string (with CSS Selector) of FAB morph target. Optional
app.fab.close(fabEl)Close FAB speed dial actions/buttons, or morph it back from specified target
  • fabEl - HTMLElement or string (with CSS Selector) of required FAB
app.fab.toggle(fabEl)Toggle FAB speed dial actions/buttons
  • fabEl - HTMLElement or string (with CSS Selector) of required FAB

FAB Events

FAB will fire the following DOM events on FAB element:

EventTargetDescription
fab:openFAB Element<div class=”fab”>Event will be triggered on FAB open or when it morphs to target element
fab:closeFAB Element<div class=”fab”>Event will be triggered on FAB close or when it morphs back from target element

Control FAB With Links

It is possible to open and close required FAB (if you have it in DOM) using special classes and data attributes on links:

  • To open FAB speed dial actions we need to add **fab-open** class to any HTML element (prefered to link)

  • To close FAB speed dial or to morph FAB back from target we need to add **fab-close** class to any HTML element (prefered to link)

  • If you have more than one FAB in DOM, you need to specify appropriate FAB via additional **data-fab=".some-fab"** attribute on this HTML element

CSS Variables

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

Note that commented variables are not specified by default and their values is what they fallback to in this case.

  1. :root {
  2. --f7-fab-text-color: #fff;
  3. --f7-fab-extended-text-font-size: 14px;
  4. --f7-fab-extended-text-padding: 0 20px;
  5. --f7-fab-label-bg-color: #fff;
  6. --f7-fab-label-text-color: #333;
  7. --f7-fab-label-border-radius: 4px;
  8. --f7-fab-label-padding: 4px 12px;
  9. --f7-fab-button-size: 40px;
  10. /* --f7-fab-pressed-bg-color: var(--f7-theme-color-shade); */
  11. }
  12. .ios {
  13. --f7-fab-size: 50px;
  14. --f7-fab-box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);
  15. --f7-fab-margin: 15px;
  16. --f7-fab-extended-size: 50px;
  17. --f7-fab-extended-text-font-weight: 400;
  18. --f7-fab-extended-text-letter-spacing: 0;
  19. --f7-fab-label-box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.4);
  20. --f7-fab-label-font-size: inherit;
  21. }
  22. .md {
  23. --f7-fab-size: 56px;
  24. --f7-fab-box-shadow: var(--f7-elevation-6);
  25. --f7-fab-margin: 16px;
  26. --f7-fab-extended-size: 48px;
  27. --f7-fab-extended-text-font-weight: 500;
  28. --f7-fab-extended-text-letter-spacing: 0.03em;
  29. --f7-fab-label-box-shadow: var(--f7-elevation-3);
  30. --f7-fab-label-font-size: inherit;
  31. }
  32. .aurora {
  33. --f7-fab-size: 40px;
  34. --f7-fab-box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);
  35. --f7-fab-margin: 15px;
  36. --f7-fab-extended-size: 38px;
  37. --f7-fab-extended-text-font-weight: 500;
  38. --f7-fab-extended-text-letter-spacing: 0;
  39. --f7-fab-label-box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.4);
  40. --f7-fab-label-font-size: 12px;
  41. }

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="title">Floating Action Button</div>
  5. </div>
  6. </div>
  7. <!-- Toolbar FAB Morph Target -->
  8. <div class="toolbar toolbar-bottom fab-morph-target">
  9. <div class="toolbar-inner">
  10. <!-- Each Toolbar link will close FAB (morph back) on click -->
  11. <a class="link fab-close">Link 1</a>
  12. <a class="link fab-close">Link 2</a>
  13. <a class="link fab-close">Link 3</a>
  14. </div>
  15. </div>
  16. <!-- FAB Left Top (Yellow) -->
  17. <div class="fab fab-left-top color-yellow">
  18. <a href="#">
  19. <!-- Icons For iOS Theme -->
  20. <i class="icon f7-icons if-not-md">add</i>
  21. <i class="icon f7-icons if-not-md">close</i>
  22. <!-- Icons For MD Theme -->
  23. <i class="icon material-icons md-only">add</i>
  24. <i class="icon material-icons md-only">close</i>
  25. </a>
  26. <!-- FAB speed dial actions -->
  27. <div class="fab-buttons fab-buttons-bottom">
  28. <a href="">1</a>
  29. <a href="">2</a>
  30. <a href="">3</a>
  31. </div>
  32. </div>
  33. <!-- FAB Right Top (Pink) -->
  34. <div class="fab fab-right-top color-pink">
  35. ...
  36. <div class="fab-buttons fab-buttons-left">
  37. ...
  38. </div>
  39. </div>
  40. <!-- FAB Center (Green) -->
  41. <div class="fab fab-center-center color-green">
  42. ...
  43. <div class="fab-buttons fab-buttons-center">
  44. ...
  45. </div>
  46. </div>
  47. <!-- FAB Left Bottom (Blue) -->
  48. <!-- Will morph to Toolbar -->
  49. <div class="fab fab-left-bottom fab-morph color-blue" data-morph-to=".toolbar">
  50. ...
  51. </div>
  52. <!-- FAB Right Bottom (Orange) -->
  53. <div class="fab fab-right-bottom">
  54. ...
  55. <!-- FAB speed dial actions with labels -->
  56. <div class="fab-buttons fab-buttons-top">
  57. <a href="#" class="fab-label-button">
  58. <span>1</span>
  59. <span class="fab-label">Action 1</span>
  60. </a>
  61. <a href="#" class="fab-label-button">
  62. <span>2</span>
  63. <span class="fab-label">Action 2</span>
  64. </a>
  65. </div>
  66. </div>
  67. <!-- Extended FAB Center Bottom (Red) -->
  68. <div class="fab fab-extended fab-center-bottom color-red">
  69. <a href="#">
  70. <i class="icon f7-icons if-not-md">add</i>
  71. <i class="icon material-icons md-only">add</i>
  72. <div class="fab-text">Create</div>
  73. </a>
  74. </div>
  75. <div class="page-content">
  76. <div class="block">
  77. <p>Lorem ipsum dolor sit amet...</p>
  78. </div>
  79. </div>
  80. </div>