u-column-notice.vue 4.0 KB

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