Toggle - 图1

Toggle

Toggle Layout

Layout is pretty simple:

  1. <!-- Toggle element -->
  2. <label class="toggle">
  3. <!-- Toggle input -->
  4. <input type="checkbox">
  5. <!-- Toggle icon -->
  6. <span class="toggle-icon"></span>
  7. </label>

Inside of Simple List:

  1. <div class="list simple-list">
  2. <ul>
  3. ...
  4. <li>
  5. <span>Text label</span>
  6. <label class="toggle">
  7. <input type="checkbox">
  8. <span class="toggle-icon"></span>
  9. </label>
  10. </li>
  11. </div>

Inside of usual List (preferable in item-after):

  1. <div class="list">
  2. <ul>
  3. ...
  4. <li class="item-content">
  5. <div class="item-inner">
  6. <div class="item-title">Text label</div>
  7. <div class="item-after">
  8. <label class="toggle">
  9. <input type="checkbox">
  10. <span class="toggle-icon"></span>
  11. </label>
  12. </div>
  13. </div>
  14. </li>
  15. </ul>
  16. </div>

Toggle Colors

Toggle supports all default colors. So to change its color just add color-[color] class to toggle element.

  1. <!-- red toggle -->
  2. <label class="toggle color-red">...</label>
  3. <!-- orange toggle -->
  4. <label class="toggle color-orange">...</label>

Toggle App Methods

Let’s look at related App methods to work with Toggle:

app.toggle.create(parameters)- create Toggle instance

  • parameters - object. Object with toggle parameters

Method returns created Toggle’s instance

app.toggle.destroy(el)- destroy Toggle instance

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

app.toggle.get(el)- get Toggle instance by HTML element

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

Method returns Toggle’s instance

Toggle Parameters

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

ParameterTypeDefaultDescription
elHTMLElement
string
Toggle element. HTMLElement or string with CSS selector of toggle element
onobjectObject with events handlers. For example:
  1. var toggle = app.toggle.create({
  2. el: ‘.toggle’,
  3. on: {
  4. change: function () {
  5. console.log(‘Toggle changed’)
  6. }
  7. }
  8. })

Toggle Methods & Properties

So to create Toggle we have to call:

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

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

Properties
toggle.appLink to global app instance
toggle.elToggle HTML element
toggle.$elDom7 instance with toggle HTML element
toggle.inputElToggle input HTML element
toggle.$inputElDom7 instance with toggle input HTML element
toggle.checkedBoolean property indicating whether it is input is checked or not
toggle.paramsToggle parameters
Methods
toggle.toggle()Toggle input state
toggle.on(event, handler)Add event handler
toggle.once(event, handler)Add event handler that will be removed after it was fired
toggle.off(event, handler)Remove event handler
toggle.off(event)Remove all handlers for specified event
toggle.emit(event, …args)Fire event on instance

Toggle Events

Toggle will fire the following DOM events on toggle element and events on app and toggle instance:

DOM Events

EventTargetDescription
toggle:changeToggle Element<div class=”toggle”>Event will be triggered when Toggle state has been changed
toggle:beforedestroyToggle Element<div class=”toggle”>Event will be triggered right before Toggle instance will be destroyed

App and Toggle Instance Events

Toggle instance emits events on both self instance and app instance. App instance events has same names prefixed with toggle.

EventArgumentsTargetDescription
changetoggletoggleEvent will be triggered when toggle state has been changed. As an argument event handler receives toggle instance
toggleChangetoggleapp
beforeDestroytoggletoggleEvent will be triggered right before Toggle instance will be destroyed. As an argument event handler receives toggle instance
toggleBeforeDestroytoggleapp

Toggle Auto Initialization

If you don’t need to use toggle API and your toggle is inside of page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional toggle-init class:

  1. <!-- Add toggle-init class -->
  2. <label class="toggle toggle-init">
  3. <input type="checkbox">
  4. <span class="toggle-icon"></span>
  5. </label>

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

  1. var toggle = app.toggle.get('.toggle');
  2. if (toggle.checked) {
  3. // do something
  4. }

CSS Variables

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

  1. :root {
  2. --f7-toggle-handle-color: #fff;
  3. }
  4. .ios {
  5. --f7-toggle-width: 52px;
  6. --f7-toggle-height: 32px;
  7. --f7-toggle-border-color: #e5e5e5;
  8. --f7-toggle-inactive-color: #fff;
  9. }
  10. .ios .theme-dark,
  11. .ios.theme-dark {
  12. --f7-toggle-border-color: #555;
  13. --f7-toggle-inactive-color: #222;
  14. }
  15. .md {
  16. --f7-toggle-width: 36px;
  17. --f7-toggle-height: 14px;
  18. --f7-toggle-inactive-color: #b0afaf;
  19. }
  20. .md .theme-dark,
  21. .md.theme-dark {
  22. --f7-toggle-inactive-color: #555;
  23. }
  24. .aurora {
  25. --f7-toggle-width: 40px;
  26. --f7-toggle-height: 20px;
  27. --f7-toggle-inactive-color: #aaa;
  28. }
  29. .aurora .theme-dark,
  30. .aurora.theme-dark {
  31. --f7-toggle-inactive-color: #555;
  32. }

Examples

  1. <div class="block-title">Super Heroes</div>
  2. <div class="list simple-list">
  3. <ul>
  4. <li>
  5. <span>Batman</span>
  6. <label class="toggle toggle-init color-black">
  7. <input type="checkbox" checked>
  8. <span class="toggle-icon"></span>
  9. </label>
  10. </li>
  11. <li>
  12. <span>Aquaman</span>
  13. <label class="toggle toggle-init color-blue">
  14. <input type="checkbox" checked>
  15. <span class="toggle-icon"></span>
  16. </label>
  17. </li>
  18. <li>
  19. <span>Superman</span>
  20. <label class="toggle toggle-init color-red">
  21. <input type="checkbox" checked>
  22. <span class="toggle-icon"></span>
  23. </label>
  24. </li>
  25. <li>
  26. <span>Hulk</span>
  27. <label class="toggle toggle-init color-green">
  28. <input type="checkbox">
  29. <span class="toggle-icon"></span>
  30. </label>
  31. </li>
  32. <li>
  33. <span>Spiderman (Disabled)</span>
  34. <label class="toggle toggle-init disabled">
  35. <input type="checkbox">
  36. <span class="toggle-icon"></span>
  37. </label>
  38. </li>
  39. <li>
  40. <span>Ironman (Disabled)</span>
  41. <label class="toggle toggle-init toggle-init">
  42. <input type="checkbox" checked disabled>
  43. <span class="toggle-icon"></span>
  44. </label>
  45. </li>
  46. <li>
  47. <span>Thor</span>
  48. <label class="toggle toggle-init color-orange">
  49. <input type="checkbox" checked>
  50. <span class="toggle-icon"></span>
  51. </label>
  52. </li>
  53. <li>
  54. <span>Wonder Woman</span>
  55. <label class="toggle toggle-init color-pink">
  56. <input type="checkbox">
  57. <span class="toggle-icon"></span>
  58. </label>
  59. </li>
  60. </ul>
  61. </div>