u-column-notice.vue 4.0 KB

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