NoticeBar 通知栏

介绍

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

引入

  1. import { createApp } from 'vue';
  2. import { NoticeBar } from 'vant';
  3. const app = createApp();
  4. app.use(NoticeBar);

代码演示

基础用法

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

  1. <van-notice-bar
  2. left-icon="volume-o"
  3. text="在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。"
  4. />

滚动播放

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

  1. <!-- 文字较短时,通过设置 scrollable 属性开启滚动播放 -->
  2. <van-notice-bar scrollable text="技术是开发它的人的共同灵魂。" />
  3. <!-- 文字较长时,通过禁用 scrollable 属性关闭滚动播放 -->
  4. <van-notice-bar
  5. :scrollable="false"
  6. text="在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。"
  7. />

多行展示

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

  1. <van-notice-bar
  2. wrapable
  3. :scrollable="false"
  4. text="在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。"
  5. />

通知栏模式

通知栏支持 closeablelink 两种模式。

  1. <!-- closeable 模式,在右侧显示关闭按钮 -->
  2. <van-notice-bar mode="closeable"> 技术是开发它的人的共同灵魂。 </van-notice-bar>
  3. <!-- link 模式,在右侧显示链接箭头 -->
  4. <van-notice-bar mode="link"> 技术是开发它的人的共同灵魂。 </van-notice-bar>

自定义样式

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

  1. <van-notice-bar color="#1989fa" background="#ecf9ff" left-icon="info-o">
  2. 技术是开发它的人的共同灵魂。
  3. </van-notice-bar>

垂直滚动

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

  1. <van-notice-bar left-icon="volume-o" :scrollable="false">
  2. <van-swipe
  3. vertical
  4. class="notice-swipe"
  5. :autoplay="3000"
  6. :show-indicators="false"
  7. >
  8. <van-swipe-item>内容 1</van-swipe-item>
  9. <van-swipe-item>内容 2</van-swipe-item>
  10. <van-swipe-item>内容 3</van-swipe-item>
  11. </van-swipe>
  12. </van-notice-bar>
  13. <style>
  14. .notice-swipe {
  15. height: 40px;
  16. line-height: 40px;
  17. }
  18. </style>

API

Props

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

Events

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

Slots

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

样式变量

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

名称默认值描述
@notice-bar-height40px-
@notice-bar-padding0 @padding-md-
@notice-bar-wrapable-padding@padding-xs @padding-md-
@notice-bar-text-color@orange-dark-
@notice-bar-font-size@font-size-md-
@notice-bar-line-height24px-
@notice-bar-background-color@orange-light-
@notice-bar-icon-size16px-
@notice-bar-icon-min-width24px-

NoticeBar 通知栏 - 图1