動作表單

動作表單是一個滑動式面板,用於向使用者提供一組如何執行某項任務的替代方案。

您也可以使用動作表單提示使用者確認潛在的危險動作。

動作表單包含一個選用標題和一個或多個按鈕,每個按鈕都對應一個要執行的動作。

請注意,不建議在大螢幕(iPad)上使用動作表單。在大螢幕上,您應該改用彈出視窗

動作表單應用程式方法

讓我們看看相關的應用程式方法來使用動作表單

app.actions.create(參數)- 建立動作表單實例

  • 參數 - 物件。包含動作表單參數的物件

方法傳回已建立的動作表單實例

app.actions.destroy(el)- 銷毀動作表單實例

  • el - HTMLElement字串(使用 CSS 選擇器)或 物件。要銷毀的動作元素實例。

app.actions.get(el)- 透過 HTML 元素取得動作表單實例

  • el - HTMLElement字串(使用 CSS 選擇器)。動作表單元素。

方法傳回動作表單實例

app.actions.open(el, 動畫)- 開啟動作表單

  • el - HTMLElement字串(使用 CSS 選擇器)。要開啟的動作表單元素。
  • 動畫 - 布林值。使用動畫開啟動作表單。

方法傳回動作表單實例

app.actions.close(el, 動畫)- 關閉動作表單

  • el - HTMLElement字串(使用 CSS 選擇器)。要關閉的動作表單元素。
  • 動畫 - 布林值。使用動畫關閉動作表單。

方法傳回動作表單實例

動作表單參數

現在讓我們看看建立動作表單所需的可用參數清單

參數類型預設值說明
elHTMLElement
字串
動作表單元素。如果你已經在 HTML 中有動作表單元素,而且想要使用此元素建立新實例,這會很有用
content字串完整的動作表單 HTML 內容字串。如果你想要使用自訂 HTML 建立動作表單元素,這會很有用
backdrop布林值true啟用動作表單背景(背後深色半透明圖層)
backdropElHTMLElement
字串
自訂背景元素的 HTML 元素或字串 CSS 選擇器
backdropUnique布林值false如果啟用,它會為此模式獨自建立唯一的背景元素
closeByBackdropClick布林值true啟用時,點擊背景會關閉動作表單
closeByOutsideClick布林值false啟用時,點擊動作表單外部會關閉動作表單
closeOnEscape布林值false啟用時,按下 ESC 鍵盤按鍵會關閉動作表單
animate布林值true動作表單是否應該使用動畫開啟/關閉。可以在 .open().close() 方法中覆寫
buttons陣列動作表單群組/按鈕。在此情況下,動作版面會根據傳遞的群組和按鈕動態產生。在 群組 的情況下,它應該是陣列,其中每個項目代表群組按鈕的陣列。
grid布林值false啟用按鈕網格版面
convertToPopover布林值true啟用時,動作表單會在大型螢幕上轉換為 彈出視窗
forceToPopover布林值false啟用時,動作表單會總是轉換為 彈出視窗
targetElHTMLElement
字串
目標元素的 HTML 元素或字串 CSS 選擇器。在轉換為彈出視窗時需要
targetX數字虛擬目標元素從螢幕左側的水平偏移量。在不使用真實目標元素 (targetEl) 的情況下轉換為彈出視窗時需要
targetY數字虛擬目標元素從螢幕頂端的垂直偏移量。在不使用真實目標元素 (targetEl) 的情況下轉換為彈出視窗時需要
targetWidth數字0虛擬目標元素寬度 (以像素為單位)。在不使用真實目標元素 (targetEl) 的情況下轉換為彈出視窗時需要
targetHeight數字0虛擬目標元素高度 (以像素為單位)。在不使用真實目標元素 (targetEl) 的情況下轉換為彈出視窗時需要
cssClass字串要設定在動作表單元素上的其他 CSS 類別名稱
onClickfunction(actions, e)在點擊動作表單按鈕後執行的回呼函式
renderfunction自訂函式,用於呈現動作表單。必須傳回動作表單 html
renderPopoverfunction在轉換為彈出視窗時,自訂函式用於呈現彈出視窗。必須傳回彈出視窗 html
containerElHTMLElement
字串
用於安裝模式的元素(預設為應用程式根元素)
開啟物件

具有事件處理常式的物件。例如

var actions = app.actions.create({
  buttons: [ /* ... */ ],
  on: {
    opened: function () {
      console.log('Action Sheet opened')
    }
  }
})

請注意,以下所有參數都可以在全域應用程式參數中使用,在 actions 屬性下設定所有動作表單的預設值。例如

var app = new Framework7({
  actions: {
    convertToPopover: false,
    grid: true,
  }
});

按鈕參數

buttons 陣列中的每個按鈕都必須表示為具有按鈕參數的物件

參數類型預設值說明
文字字串按鈕文字的字串(可以是 HTML 字串)
圖示字串圖示的 HTML 字串
粗體布林值false啟用粗體按鈕文字
顏色字串按鈕顏色,預設顏色之一
背景字串按鈕背景顏色,預設顏色之一
標籤布林值false如果啟用,則會將其呈現為標籤,而不是按鈕
停用布林值false定義按鈕是否已停用。
關閉布林值true如果啟用,則按鈕按一下會關閉動作表單
onClickfunction(actions, e)按一下此按鈕後執行的回呼函式

動作表單方法與屬性

因此,若要建立動作表單,我們必須呼叫

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

之後,我們將取得其初始化的執行個體(例如上方範例中的 actions 變數),其中包含有用的方法和屬性

屬性
actions.app連結至全域應用程式執行個體
actions.el動作表單 HTML 元素
actions.$el具有動作表單 HTML 元素的 Dom7 執行個體
actions.backdropEl背景 HTML 元素
actions.$backdropEl具有背景 HTML 元素的 Dom7 執行個體
actions.params動作表單執行個體參數
actions.opened布林值屬性,表示動作表單是否已開啟
方法
actions.open(animate)開啟動作表單。其中
  • animate - 布林值(預設為 true) - 定義是否應以動畫開啟
actions.close(animate)關閉動作表單。其中
  • animate - 布林值(預設為 true) - 定義是否應以動畫關閉
actions.destroy()銷毀動作表單
actions.on(event, handler)新增事件處理常式
actions.once(event, handler)新增事件處理常式,在觸發後將其移除
actions.off(event, handler)移除事件處理常式
actions.off(event)移除指定事件的所有處理常式
actions.emit(event, ...args)在執行個體上觸發事件

可以使用連結上的特殊類別和資料屬性,開啟和關閉必要的動作表單(如果它們在 DOM 中)

  • 若要開啟動作表單,我們需要將「actions-open」類別新增至任何 HTML 元素(建議使用連結)

  • 若要關閉動作表單,我們需要將「actions-close」類別新增至任何 HTML 元素(建議使用連結)

  • 如果 DOM 中有多個動作表單,則需要透過此 HTML 元素上的其他 data-actions=".my-actions" 屬性,指定適當的動作表單

根據上述註解

<!-- In data-actions attribute we specify CSS selector of action sheet we need to open -->
<p><a href="#" data-actions=".my-actions" class="actions-open">Open Actions</a></p>

<!-- And somewhere in DOM -->
<div class="actions-modal my-actions">
  ...
</div>

動作表單事件

動作表單會在動作表單元素上觸發下列 DOM 事件,以及在應用程式和動作表單執行個體上觸發事件

DOM 事件

事件目標說明
actions:open動作表單元素<div class="actions-modal">當動作表單開始開啟動畫時,將觸發事件
actions:opened動作表單元素<div class="actions-modal">當動作表單完成開啟動畫後,將觸發事件
actions:close動作表單元素<div class="actions-modal">當動作表單開始關閉動畫時,將觸發事件
actions:closed動作表單元素<div class="actions-modal">當動作表單完成關閉動畫後,將觸發事件

應用程式和動作表單執行個體事件

動作表單執行個體會在自身執行個體和應用程式執行個體上發出事件。應用程式執行個體事件有相同的名稱,但前面加上 actions

事件引數目標說明
openactionsactions當動作表單開始開啟動畫時,將觸發事件。事件處理常式會收到動作表單執行個體作為引數
actionsOpenactionsapp
openedactionsactions當動作表單完成開啟動畫後,將觸發事件。事件處理常式會收到動作表單執行個體作為引數
actionsOpenedactionsapp
關閉actionsactions當動作表單開始關閉動畫時,將觸發事件。事件處理常式會收到動作表單執行個體作為引數
actionsCloseactionsapp
closedactionsactions當動作表單完成關閉動畫後,將觸發事件。事件處理常式會收到動作表單執行個體作為引數
actionsClosedactionsapp
beforeDestroyactionsactions在 Action Sheet 執行個體被銷毀之前,會觸發事件。作為引數,事件處理常式會收到動作表單執行個體
actionsBeforeDestroyactionsapp

CSS 變數

以下是相關 CSS 變數 (CSS 自訂屬性) 清單。

請注意,註解的變數預設未指定,而它們的值是它們在此情況下會回退到的值。

:root {
  --f7-actions-grid-button-font-size: 12px;
  --f7-actions-grid-button-icon-size: 48px;
}
.ios {
  --f7-actions-transition-timing-function: initial;
  --f7-actions-border-radius: 13px;
  /*
  --f7-actions-button-text-color: var(--f7-theme-color);
  */
  --f7-actions-grid-button-text-color: #757575;
  --f7-actions-button-padding: 0px;
  --f7-actions-button-text-align: center;
  --f7-actions-button-height: 57px;
  --f7-actions-button-height-landscape: 44px;
  --f7-actions-button-font-size: 20px;
  --f7-actions-button-icon-size: 28px;
  --f7-actions-button-justify-content: center;
  --f7-actions-label-padding: 8px 10px;
  --f7-actions-label-font-weight: 400;
  --f7-actions-label-font-size: 13px;
  --f7-actions-label-justify-content: center;
  --f7-actions-group-border-color: transparent;
  --f7-actions-group-margin: 8px;
  --f7-actions-bg-color: rgba(255, 255, 255, 0.95);
  --f7-actions-bg-color-rgb: 255, 255, 255;
  --f7-actions-button-border-color: rgba(0, 0, 0, 0.2);
  --f7-actions-button-pressed-bg-color: rgba(230, 230, 230, 0.9);
  --f7-actions-button-pressed-bg-color-rgb: 230, 230, 230;
  --f7-actions-label-text-color: #8a8a8a;
}
.ios .dark,
.ios.dark {
  --f7-actions-bg-color: rgba(45, 45, 45, 0.95);
  --f7-actions-bg-color-rgb: 45, 45, 45;
  --f7-actions-button-border-color: rgba(255, 255, 255, 0.15);
  --f7-actions-button-pressed-bg-color: rgba(50, 50, 50, 0.9);
  --f7-actions-button-pressed-bg-color-rgb: 50, 50, 50;
  --f7-actions-label-text-color: rgba(255, 255, 255, 0.55);
}
.md {
  --f7-actions-transition-timing-function: cubic-bezier(0, 0.8, 0.34, 1);
  --f7-actions-border-radius: 16px;
  --f7-actions-button-border-color: transparent;
  --f7-actions-button-padding: 0 16px;
  --f7-actions-button-text-align: left;
  --f7-actions-button-height: 48px;
  --f7-actions-button-height-landscape: 48px;
  --f7-actions-button-font-size: 16px;
  --f7-actions-button-icon-size: 24px;
  --f7-actions-button-justify-content: space-between;
  --f7-actions-label-padding: 12px 16px;
  --f7-actions-label-font-size: 14px;
  --f7-actions-label-font-weight: 500;
  --f7-actions-label-justify-content: flex-start;
  --f7-actions-group-margin: 0px;
  --f7-actions-button-pressed-bg-color: #e5e5e5;
}
.md .dark,
.md.dark {
  --f7-actions-button-pressed-bg-color: #2e2e2e;
}
.md,
.md .dark,
.md [class*='color-'] {
  --f7-actions-button-text-color: var(--f7-md-on-surface);
  --f7-actions-bg-color: var(--f7-md-surface-3);
  --f7-actions-group-border-color: var(--f7-md-outline);
  --f7-actions-label-text-color: var(--f7-theme-color);
  --f7-actions-grid-button-text-color: var(--f7-md-on-surface);
}

範例

action-sheet.html
<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-bg"></div>
      <div class="navbar-inner sliding">
        <div class="title">Action Sheet</div>
      </div>
    </div>
    <div class="page-content">
      <div class="block block-strong inset">
        <p class="grid grid-cols-2 grid-gap">
          <a class="button button-fill" @click=${openDemo1}>One group</a>
          <a class="button button-fill" @click=${openDemo2}>Two groups</a>
        </p>
        <p>
          <a class="button button-fill" @click=${openGrid}>Action Grid</a>
        </p>
      </div>
      <div class="block-title">Action Sheet To Popover</div>
      <div class="block block-strong inset">
        <p>Action Sheet can be automatically converted to Popover (for tablets). This button will open Popover on
          tablets and Action Sheet on phones: <a style="display:inline-block"
            class="button button-to-popover button-small" @click=${openActionsPopover}>Actions</a></p>
      </div>
    </div>
  </div>
</template>
<script>
  export default (props, { $on, $f7, $el }) => {
    let actions1;
    let actions2;
    let actionsGrid;
    let actionsToPopover;

    const openDemo1 = () => {
      actions1.open();
    }
    const openDemo2 = () => {
      actions2.open();
    }
    const openGrid = () => {
      actionsGrid.open();
    }
    const openActionsPopover = () => {
      actionsToPopover.open();
    }

    $on('pageInit', () => {
      // 1 Group
      const buttons1 = [
        {
          text: 'Do something',
          label: true
        },
        {
          text: 'Button 1',
          strong: true
        },
        {
          text: 'Button 2',
        },
        {
          text: 'Cancel',
          color: 'red'
        },
      ];

      // 2 Groups
      const buttons2 = [
        // First Group
        [
          {
            text: 'Do something',
            label: true
          },
          {
            text: 'Button 1',
            strong: true
          },
          {
            text: 'Button 2',
          }
        ],
        // Second Group
        [
          {
            text: 'Cancel',
            color: 'red'
          }
        ]
      ]

      // Grid buttons with icons/images
      const gridButtons = [
        [
          {
            text: 'Button 1',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-1.jpg" width="48" style="max-width: 100%; border-radius: 8px"/>'
          },
          {
            text: 'Button 2',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-2.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
          {
            text: 'Button 3',
            icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-3.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
        ],
        [
          {
            text: 'Button 1',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-4.jpg" width="48" style="max-width: 100%; border-radius: 8px"/>'
          },
          {
            text: 'Button 2',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-5.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
          {
            text: 'Button 3',
            icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-6.jpg" width="48" style="max-width: 100%; border-radius: 8px">'
          },
        ],
      ]

      actions1 = $f7.actions.create({ buttons: buttons1 });
      actions2 = $f7.actions.create({ buttons: buttons2 });
      actionsGrid = $f7.actions.create({ buttons: gridButtons, grid: true });

      // Actions To Popover
      actionsToPopover = $f7.actions.create({
        buttons: buttons1,
        // Need to specify popover target
        targetEl: $el.value.find('.button-to-popover')
      });
    })

    $on('pageBeforeRemove', () => {
      actions1.destroy();
      actions2.destroy();
      actionsGrid.destroy();
      actionsToPopover.destroy();
    })

    return $render;
  }

</script>