Skip to content

NoticeBar 通知栏

介绍

用于循环播放展示一组消息通知。

引入

通过以下方式来引入组件。

js
import { createApp } from 'vue';
import { NoticeBar } from '@szhn/dh-design-mobile';

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

代码演示

基础用法

通过 text 属性设置通知栏的内容,通过 left-icon 属性设置通知栏左侧的图标。

html
<van-notice-bar
  left-icon="volume-o"
  text="无论我们能活多久,我们能够享受的只有无法分割的此刻,此外别无其他。"
/>

滚动播放

通知栏的内容长度溢出时会自动开启滚动播放,通过 scrollable 属性可以控制该行为。

html
<!-- 文字较短时,通过设置 scrollable 属性开启滚动播放 -->
<van-notice-bar scrollable text="米袋虽空——樱花开哉!" />

<!-- 文字较长时,通过禁用 scrollable 属性关闭滚动播放 -->
<van-notice-bar
  :scrollable="false"
  text="不会回头的东西有四件:说出口的话、离弦的箭、逝去的生活和失去的机会。"
/>

多行展示

文字较长时,可以通过设置 wrapable 属性来开启多行展示。

html
<van-notice-bar
  wrapable
  :scrollable="false"
  text="不会回头的东西有四件:说出口的话、离弦的箭、逝去的生活和失去的机会。"
/>

通知栏模式

通知栏支持 closeablelink 两种模式。

html
<!-- closeable 模式,在右侧显示关闭按钮 -->
<van-notice-bar mode="closeable">米袋虽空——樱花开哉!</van-notice-bar>

<!-- link 模式,在右侧显示链接箭头 -->
<van-notice-bar mode="link">米袋虽空——樱花开哉!</van-notice-bar>

自定义样式

通过 color 属性设置文本颜色,通过 background 属性设置背景色。

html
<van-notice-bar color="#1989fa" background="#ecf9ff" left-icon="info-o">
  米袋虽空——樱花开哉!
</van-notice-bar>

垂直滚动

搭配 NoticeBar 和 Swipe 组件,可以实现垂直滚动的效果。

html
<van-notice-bar left-icon="volume-o" :scrollable="false">
  <van-swipe
    vertical
    class="notice-swipe"
    :autoplay="3000"
    :touchable="false"
    :show-indicators="false"
  >
    <van-swipe-item>明月直入,无心可猜。</van-swipe-item>
    <van-swipe-item>仙人抚我顶,结发受长生。</van-swipe-item>
    <van-swipe-item>今人不见古时月,今月曾经照古人。</van-swipe-item>
  </van-swipe>
</van-notice-bar>

<style>
  .notice-swipe {
    height: 40px;
    line-height: 40px;
  }
</style>

API

Props

参数说明类型默认值
mode通知栏模式,可选值为 closeable linkstring''
text通知文本内容string''
color通知文本颜色string#ed6a0c
background滚动条背景string#fffbe8
left-icon左侧图标名称或图片链接,等同于 Icon 组件的 name 属性string-
delay动画延迟时间 (s)number | string1
speed滚动速率 (px/s)number | string60
scrollable是否开启滚动播放,内容长度溢出时默认开启boolean-
wrapable是否开启文本换行,只在禁用滚动时生效booleanfalse

Events

事件名说明回调参数
click点击通知栏时触发event: MouseEvent
close关闭通知栏时触发event: MouseEvent
replay每当滚动栏重新开始滚动时触发-

方法

通过 ref 可以获取到 NoticeBar 实例并调用实例方法,详见组件实例方法

方法名说明参数返回值
reset重置通知栏到初始状态--

类型定义

组件导出以下类型定义:

ts
import type { NoticeBarMode, NoticeBarProps, NoticeBarInstance } from '@szhn/dh-design-mobile';

NoticeBarInstance 是组件实例的类型,用法如下:

ts
import { ref } from 'vue';
import type { NoticeBarInstance } from '@szhn/dh-design-mobile';

const noticeBarRef = ref<NoticeBarInstance>();

noticeBarRef.value?.reset();

Slots

名称内容
default通知文本内容
left-icon自定义左侧图标
right-icon自定义右侧图标

主题定制

样式变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 组件

名称默认值描述
--van-notice-bar-height40px通知栏的高度
--van-notice-bar-padding0 var(--van-padding-md)通知栏的内边距
--van-notice-bar-wrapable-paddingvar(--van-padding-xs) var(--van-padding-md)多行通知栏的内边距
--van-notice-bar-text-colorvar(--van-orange-dark)通知栏的文字颜色
--van-notice-bar-font-sizevar(--van-font-size-md)通知栏的字体大小
--van-notice-bar-line-height24px通知栏的行高
--van-notice-bar-backgroundvar(--van-orange-light)通知栏的背景色
--van-notice-bar-icon-size16px图标的大小
--van-notice-bar-icon-min-width24px图标的最小宽度