99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Vant4 Popover 氣泡彈出框

2023-02-16 17:56 更新

介紹

彈出式的氣泡菜單。

引入

通過(guò)以下方式來(lái)全局注冊(cè)組件,更多注冊(cè)方式請(qǐng)參考組件注冊(cè)。

import { createApp } from 'vue';
import { Popover } from 'vant';

const app = createApp();
app.use(Popover);

代碼演示

基礎(chǔ)用法

當(dāng) Popover 彈出時(shí),會(huì)基于 ?reference? 插槽的內(nèi)容進(jìn)行定位。

<van-popover v-model:show="showPopover" :actions="actions" @select="onSelect">
  <template #reference>
    <van-button type="primary">淺色風(fēng)格</van-button>
  </template>
</van-popover>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const showPopover = ref(false);

    // 通過(guò) actions 屬性來(lái)定義菜單選項(xiàng)
    const actions = [
      { text: '選項(xiàng)一' },
      { text: '選項(xiàng)二' },
      { text: '選項(xiàng)三' },
    ];
    const onSelect = (action) => showToast(action.text);

    return {
      actions,
      onSelect,
      showPopover,
    };
  },
};

深色風(fēng)格

Popover 支持淺色和深色兩種風(fēng)格,默認(rèn)為淺色風(fēng)格,將 ?theme? 屬性設(shè)置為 ?dark? 可切換為深色風(fēng)格。

<van-popover v-model:show="showPopover" theme="dark" :actions="actions">
  <template #reference>
    <van-button type="primary">深色風(fēng)格</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項(xiàng)一' },
      { text: '選項(xiàng)二' },
      { text: '選項(xiàng)三' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

彈出位置

通過(guò) ?placement? 屬性來(lái)控制氣泡的彈出位置。

<van-popover placement="top" />

?placement? 支持以下值:

top           # 頂部中間位置
top-start     # 頂部左側(cè)位置
top-end       # 頂部右側(cè)位置
left          # 左側(cè)中間位置
left-start    # 左側(cè)上方位置
left-end      # 左側(cè)下方位置
right         # 右側(cè)中間位置
right-start   # 右側(cè)上方位置
right-end     # 右側(cè)下方位置
bottom        # 底部中間位置
bottom-start  # 底部左側(cè)位置
bottom-end    # 底部右側(cè)位置

展示圖標(biāo)

在 ?actions? 數(shù)組中,可以通過(guò) ?icon? 字段來(lái)定義選項(xiàng)的圖標(biāo),支持傳入圖標(biāo)名稱(chēng)或圖片鏈接,等同于 Icon 組件的 name 屬性。

<van-popover v-model:show="showPopover" :actions="actions">
  <template #reference>
    <van-button type="primary">展示圖標(biāo)</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項(xiàng)一', icon: 'add-o' },
      { text: '選項(xiàng)二', icon: 'music-o' },
      { text: '選項(xiàng)三', icon: 'more-o' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

禁用選項(xiàng)

在 ?actions? 數(shù)組中,可以通過(guò) ?disabled? 字段來(lái)禁用某個(gè)選項(xiàng)。

<van-popover v-model:show="showPopover" :actions="actions">
  <template #reference>
    <van-button type="primary">禁用選項(xiàng)</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    const actions = [
      { text: '選項(xiàng)一', disabled: true },
      { text: '選項(xiàng)二', disabled: true },
      { text: '選項(xiàng)三' },
    ];

    return {
      actions,
      showPopover,
    };
  },
};

自定義內(nèi)容

通過(guò)默認(rèn)插槽,可以在 Popover 內(nèi)部放置任意內(nèi)容。

<van-popover v-model:show="showPopover">
  <van-grid
    square
    clickable
    :border="false"
    column-num="3"
    style="width: 240px;"
  >
    <van-grid-item
      v-for="i in 6"
      :key="i"
      text="選項(xiàng)"
      icon="photo-o"
      @click="showPopover = false"
    />
  </van-grid>
  <template #reference>
    <van-button type="primary">自定義內(nèi)容</van-button>
  </template>
</van-popover>
import { ref } from 'vue';

export default {
  setup() {
    const showPopover = ref(false);
    return { showPopover };
  },
};

非受控模式

你可以把 Popover 當(dāng)做受控組件或非受控組件使用:

  • 當(dāng)綁定 ?v-model:show? 時(shí),Popover 為受控組件,此時(shí)組件的顯示完全由 ?v-model:show? 的值決定。
  • 當(dāng)未綁定 ?v-model:show? 時(shí),Popover 為非受控組件,此時(shí)你可以通過(guò) ?show? 屬性傳入一個(gè)默認(rèn)值,組件值的顯示由組件自身控制。
<van-popover :actions="actions" position="top-start" @select="onSelect">
  <template #reference>
    <van-button type="primary">非受控模式</van-button>
  </template>
</van-popover>
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
  setup() {
    const actions = [
      { text: '選項(xiàng)一' },
      { text: '選項(xiàng)二' },
      { text: '選項(xiàng)三' },
    ];
    const onSelect = (action) => showToast(action.text);
    return {
      actions,
      onSelect,
    };
  },
};

API

Props

參數(shù) 說(shuō)明 類(lèi)型 默認(rèn)值
v-model:show 是否展示氣泡彈出層 boolean false
actions 選項(xiàng)列表 PopoverAction[] []
placement 彈出位置 PopoverPlacement bottom
theme 主題風(fēng)格,可選值為 dark PopoverTheme light
trigger 觸發(fā)方式,可選值為 manual PopoverTrigger click
duration 動(dòng)畫(huà)時(shí)長(zhǎng),單位秒,設(shè)置為 0 可以禁用動(dòng)畫(huà) number | string 0.3
offset 出現(xiàn)位置的偏移量 [number, number] [0, 8]
overlay 是否顯示遮罩層 boolean false
overlay-class v3.0.10 自定義遮罩層類(lèi)名 string | Array | object -
overlay-style v3.0.10 自定義遮罩層樣式 object -
show-arrow v3.2.2 是否展示小箭頭 boolean true
close-on-click-action 是否在點(diǎn)擊選項(xiàng)后關(guān)閉 boolean true
close-on-click-outside 是否在點(diǎn)擊外部元素后關(guān)閉菜單 boolean true
close-on-click-overlay v3.0.10 是否在點(diǎn)擊遮罩層后關(guān)閉菜單 boolean true
teleport 指定掛載的節(jié)點(diǎn),等同于 Teleport 組件的 to 屬性 string | Element body
icon-prefix v3.0.17 圖標(biāo)類(lèi)名前綴,等同于 Icon 組件的 class-prefix 屬性 string van-icon

PopoverAction 數(shù)據(jù)結(jié)構(gòu)

?actions? 屬性是一個(gè)由對(duì)象構(gòu)成的數(shù)組,數(shù)組中的每個(gè)對(duì)象配置一列,對(duì)象可以包含以下值:

鍵名 說(shuō)明 類(lèi)型
text 選項(xiàng)文字 string
icon 文字左側(cè)的圖標(biāo),支持傳入圖標(biāo)名稱(chēng)或圖片鏈接,等同于 Icon 組件的 name 屬性 string
color 選項(xiàng)文字顏色 string
disabled 是否為禁用狀態(tài) boolean
className 為對(duì)應(yīng)選項(xiàng)添加額外的類(lèi)名 string | Array | object

Events

事件名 說(shuō)明 回調(diào)參數(shù)
select 點(diǎn)擊選項(xiàng)時(shí)觸發(fā) action: PopoverAction, index: number
open 打開(kāi)菜單時(shí)觸發(fā) -
close 關(guān)閉菜單時(shí)觸發(fā) -
opened 打開(kāi)菜單且動(dòng)畫(huà)結(jié)束后觸發(fā) -
closed 關(guān)閉菜單且動(dòng)畫(huà)結(jié)束后觸發(fā) -
click-overlay 點(diǎn)擊遮罩層時(shí)觸發(fā) event: MouseEvent

Slots

名稱(chēng) 說(shuō)明 參數(shù)
default 自定義菜單內(nèi)容 -
reference 觸發(fā) Popover 顯示的元素內(nèi)容 -
action v3.4.0 自定義選項(xiàng)內(nèi)容 { action: PopoverAction, index: number }

類(lèi)型定義

組件導(dǎo)出以下類(lèi)型定義:

import type {
  PopoverProps,
  PopoverTheme,
  PopoverAction,
  PopoverTrigger,
  PopoverPlacement,
} from 'vant';

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請(qǐng)參考 ConfigProvider 組件。

名稱(chēng) 默認(rèn)值 描述
--van-popover-arrow-size 6px -
--van-popover-radius var(--van-radius-lg) -
--van-popover-action-width 128px -
--van-popover-action-height 44px -
--van-popover-action-font-size var(--van-font-size-md) -
--van-popover-action-line-height var(--van-line-height-md) -
--van-popover-action-icon-size 20px -
--van-popover-light-text-color var(--van-text-color) -
--van-popover-light-background var(--van-background-2) -
--van-popover-light-action-disabled-text-color var(--van-text-color-3) -
--van-popover-dark-text-color var(--van-white) -
--van-popover-dark-background #4a4a4a -
--van-popover-dark-action-disabled-text-color var(--van-text-color-2) -

常見(jiàn)問(wèn)題

Popover 的點(diǎn)擊事件無(wú)法正確觸發(fā)?

這種情況通常是由于項(xiàng)目中引入了 ?fastclick? 庫(kù)導(dǎo)致的。建議移除 ?fastclick?,或者配置 ?fastclick? 的 ignore 規(guī)則。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)