u-column-notice.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view class="u-notice" @tap="clickHandler">
  3. <slot name="icon">
  4. <view class="u-notice__left-icon" v-if="icon">
  5. <u-icon :name="icon" :color="color" size="19"></u-icon>
  6. </view>
  7. </slot>
  8. <swiper :disable-touch="disableTouch" :vertical="step ? false : true" circular :interval="duration" :autoplay="true" class="u-notice__swiper" @change="noticeChange">
  9. <swiper-item v-for="(item, index) in text" :key="index" class="u-notice__swiper__item">
  10. <text class="u-notice__swiper__item__text u-line-1" :style="[textStyle]">{{ item }}</text>
  11. </swiper-item>
  12. </swiper>
  13. <view class="u-notice__right-icon" v-if="['link', 'closable'].includes(mode)">
  14. <u-icon v-if="mode === 'link'" name="arrow-right" :size="17" :color="color"></u-icon>
  15. <u-icon v-if="mode === 'closable'" name="close" :size="16" :color="color" @click="close"></u-icon>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import props from './props.js';
  21. /**
  22. * ColumnNotice 滚动通知中的垂直滚动 内部组件
  23. * @description 该组件用于滚动通告场景,是其中的垂直滚动方式
  24. * @tutorial https://www.uviewui.com/components/noticeBar.html
  25. * @property {Array} text 显示的内容,字符串
  26. * @property {String} icon 是否显示左侧的音量图标 ( 默认 'volume' )
  27. * @property {String} mode 通告模式,link-显示右箭头,closable-显示右侧关闭图标
  28. * @property {String} color 文字颜色,各图标也会使用文字颜色 ( 默认 '#f9ae3d' )
  29. * @property {String} bgColor 背景颜色 ( 默认 '#fdf6ec' )
  30. * @property {String | Number} fontSize 字体大小,单位px ( 默认 14 )
  31. * @property {String | Number} speed 水平滚动时的滚动速度,即每秒滚动多少px(rpx),这有利于控制文字无论多少时,都能有一个恒定的速度 ( 默认 80 )
  32. * @property {Boolean} step direction = row时,是否使用步进形式滚动 ( 默认 false )
  33. * @property {String | Number} duration 滚动一个周期的时间长,单位ms ( 默认 1500 )
  34. * @property {Boolean} disableTouch 是否禁止用手滑动切换 目前HX2.6.11,只支持App 2.5.5+、H5 2.5.5+、支付宝小程序、字节跳动小程序 ( 默认 true )
  35. * @example
  36. */
  37. export default {
  38. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  39. watch: {
  40. text: {
  41. immediate: true,
  42. handler (newValue, oldValue) {
  43. if (!uni.$u.test.array(newValue)) {
  44. uni.$u.error('noticebar组件direction为column时,要求text参数为数组形式')
  45. }
  46. }
  47. }
  48. },
  49. computed: {
  50. // 文字内容的样式
  51. textStyle () {
  52. let style = {}
  53. style.color = this.color
  54. style.fontSize = uni.$u.addUnit(this.fontSize)
  55. return style
  56. },
  57. // 垂直或者水平滚动
  58. vertical () {
  59. if (this.mode == 'horizontal') return false
  60. else return true
  61. },
  62. },
  63. data () {
  64. return {
  65. index: 0
  66. }
  67. },
  68. methods: {
  69. noticeChange (e) {
  70. this.index = e.detail.current
  71. },
  72. // 点击通告栏
  73. clickHandler () {
  74. this.$emit('click', this.index)
  75. },
  76. // 点击关闭按钮
  77. close () {
  78. this.$emit('close')
  79. }
  80. }
  81. };
  82. </script>
  83. <style lang="scss" scoped>
  84. @import "../../libs/css/components.scss";
  85. .u-notice {
  86. @include flex;
  87. align-items: center;
  88. justify-content: space-between;
  89. &__left-icon {
  90. align-items: center;
  91. margin-right: 5px;
  92. }
  93. &__right-icon {
  94. margin-left: 5px;
  95. align-items: center;
  96. }
  97. &__swiper {
  98. height: 16px;
  99. @include flex;
  100. align-items: center;
  101. flex: 1;
  102. &__item {
  103. @include flex;
  104. align-items: center;
  105. overflow: hidden;
  106. &__text {
  107. font-size: 14px;
  108. color: $u-warning;
  109. }
  110. }
  111. }
  112. }
  113. </style>