u-notice-bar.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="u-notice-bar-wrap" v-if="isShow" :style="{
  3. borderRadius: borderRadius + 'rpx',
  4. }">
  5. <block v-if="mode == 'horizontal' && isCircular">
  6. <u-row-notice
  7. :type="type"
  8. :color="color"
  9. :bgColor="bgColor"
  10. :list="list"
  11. :volumeIcon="volumeIcon"
  12. :moreIcon="moreIcon"
  13. :volumeSize="volumeSize"
  14. :closeIcon="closeIcon"
  15. :mode="mode"
  16. :fontSize="fontSize"
  17. :speed="speed"
  18. :playState="playState"
  19. :padding="padding"
  20. @change="change"
  21. @getMore="getMore"
  22. @close="close"
  23. @click="click"
  24. ></u-row-notice>
  25. </block>
  26. <block v-if="mode == 'vertical' || (mode == 'horizontal' && !isCircular)">
  27. <u-column-notice
  28. :type="type"
  29. :color="color"
  30. :bgColor="bgColor"
  31. :list="list"
  32. :volumeIcon="volumeIcon"
  33. :moreIcon="moreIcon"
  34. :closeIcon="closeIcon"
  35. :mode="mode"
  36. :volumeSize="volumeSize"
  37. :disable-touch="disableTouch"
  38. :fontSize="fontSize"
  39. :duration="duration"
  40. :playState="playState"
  41. :padding="padding"
  42. @change="change"
  43. @getMore="getMore"
  44. @close="close"
  45. @click="click"
  46. @end="end"
  47. ></u-column-notice>
  48. </block>
  49. </view>
  50. </template>
  51. <script>
  52. /**
  53. * noticeBar 滚动通知
  54. * @description 该组件用于滚动通告场景,有多种模式可供选择
  55. * @tutorial https://www.uviewui.com/components/noticeBar.html
  56. * @property {Array} list 滚动内容,数组形式,见上方说明
  57. * @property {String} type 显示的主题(默认warning)
  58. * @property {Boolean} volume-icon 是否显示小喇叭图标(默认true)
  59. * @property {Boolean} more-icon 是否显示右边的向右箭头(默认false)
  60. * @property {Boolean} close-icon 是否显示关闭图标(默认false)
  61. * @property {Boolean} autoplay 是否自动播放(默认true)
  62. * @property {String} color 文字颜色
  63. * @property {String Number} bg-color 背景颜色
  64. * @property {String} mode 滚动模式(默认horizontal)
  65. * @property {Boolean} show 是否显示(默认true)
  66. * @property {String Number} font-size 字体大小,单位rpx(默认28)
  67. * @property {String Number} volume-size 左边喇叭的大小(默认34)
  68. * @property {String Number} duration 滚动周期时长,只对步进模式有效,横向衔接模式无效,单位ms(默认2000)
  69. * @property {String Number} speed 水平滚动时的滚动速度,即每秒移动多少距离,只对水平衔接方式有效,单位rpx(默认160)
  70. * @property {String Number} font-size 字体大小,单位rpx(默认28)
  71. * @property {Boolean} is-circular mode为horizontal时,指明是否水平衔接滚动(默认true)
  72. * @property {String} play-state 播放状态,play - 播放,paused - 暂停(默认play)
  73. * @property {String Nubmer} border-radius 通知栏圆角(默认为0)
  74. * @property {String Nubmer} padding 内边距,字符串,与普通的内边距css写法一直(默认"18rpx 24rpx")
  75. * @property {Boolean} no-list-hidden 列表为空时,是否显示组件(默认false)
  76. * @property {Boolean} disable-touch 是否禁止通过手动滑动切换通知,只有mode = vertical,或者mode = horizontal且is-circular = false时有效(默认true)
  77. * @event {Function} click 点击通告文字触发,只有mode = vertical,或者mode = horizontal且is-circular = false时有效
  78. * @event {Function} close 点击右侧关闭图标触发
  79. * @event {Function} getMore 点击右侧向右图标触发
  80. * @event {Function} end 列表的消息每次被播放一个周期时触发,只有mode = vertical,或者mode = horizontal且is-circular = false时有效
  81. * @example <u-notice-bar :more-icon="true" :list="list"></u-notice-bar>
  82. */
  83. export default {
  84. name: "u-notice-bar",
  85. props: {
  86. // 显示的内容,数组
  87. list: {
  88. type: Array,
  89. default() {
  90. return [];
  91. }
  92. },
  93. // 显示的主题,success|error|primary|info|warning
  94. type: {
  95. type: String,
  96. default: 'warning'
  97. },
  98. // 是否显示左侧的音量图标
  99. volumeIcon: {
  100. type: Boolean,
  101. default: true
  102. },
  103. // 音量喇叭的大小
  104. volumeSize: {
  105. type: [Number, String],
  106. default: 34
  107. },
  108. // 是否显示右侧的右箭头图标
  109. moreIcon: {
  110. type: Boolean,
  111. default: false
  112. },
  113. // 是否显示右侧的关闭图标
  114. closeIcon: {
  115. type: Boolean,
  116. default: false
  117. },
  118. // 是否自动播放
  119. autoplay: {
  120. type: Boolean,
  121. default: true
  122. },
  123. // 文字颜色,各图标也会使用文字颜色
  124. color: {
  125. type: String,
  126. default: ''
  127. },
  128. // 背景颜色
  129. bgColor: {
  130. type: String,
  131. default: ''
  132. },
  133. // 滚动方向,horizontal-水平滚动,vertical-垂直滚动
  134. mode: {
  135. type: String,
  136. default: 'horizontal'
  137. },
  138. // 是否显示
  139. show: {
  140. type: Boolean,
  141. default: true
  142. },
  143. // 字体大小,单位rpx
  144. fontSize: {
  145. type: [Number, String],
  146. default: 28
  147. },
  148. // 滚动一个周期的时间长,单位ms
  149. duration: {
  150. type: [Number, String],
  151. default: 2000
  152. },
  153. // 水平滚动时的滚动速度,即每秒滚动多少rpx,这有利于控制文字无论多少时,都能有一个恒定的速度
  154. speed: {
  155. type: [Number, String],
  156. default: 160
  157. },
  158. // 水平滚动时,是否采用衔接形式滚动
  159. // 水平衔接模式,采用的是swiper组件,水平滚动
  160. isCircular: {
  161. type: Boolean,
  162. default: true
  163. },
  164. // 播放状态,play-播放,paused-暂停
  165. playState: {
  166. type: String,
  167. default: 'play'
  168. },
  169. // 是否禁止用手滑动切换
  170. // 目前HX2.6.11,只支持App 2.5.5+、H5 2.5.5+、支付宝小程序、字节跳动小程序
  171. disableTouch: {
  172. type: Boolean,
  173. default: true
  174. },
  175. // 滚动通知设置圆角
  176. borderRadius: {
  177. type: [Number, String],
  178. default: 0
  179. },
  180. // 通知的边距
  181. padding: {
  182. type: [Number, String],
  183. default: '18rpx 24rpx'
  184. },
  185. // list列表为空时,是否显示组件
  186. noListHidden: {
  187. type: Boolean,
  188. default: true
  189. }
  190. },
  191. computed: {
  192. // 如果设置show为false,或者设置了noListHidden为true,且list长度又为零的话,隐藏组件
  193. isShow() {
  194. if(this.show == false || (this.noListHidden == true && this.list.length == 0)) return false;
  195. else return true;
  196. }
  197. },
  198. methods: {
  199. // 点击通告栏
  200. click(index) {
  201. this.$emit('click', index);
  202. },
  203. // 点击关闭按钮
  204. close() {
  205. this.$emit('close');
  206. },
  207. // 点击更多箭头按钮
  208. getMore() {
  209. this.$emit('getMore');
  210. },
  211. // 滚动一个周期结束,只对垂直,或者水平步进形式有效
  212. end() {
  213. this.$emit('end');
  214. },
  215. change(e){
  216. this.$emit('change',e);
  217. }
  218. }
  219. };
  220. </script>
  221. <style lang="scss" scoped>
  222. @import "../../libs/css/style.components.scss";
  223. .u-notice-bar-wrap {
  224. overflow: hidden;
  225. }
  226. .u-notice-bar {
  227. padding: 18rpx 24rpx;
  228. overflow: hidden;
  229. }
  230. .u-direction-row {
  231. @include vue-flex;
  232. align-items: center;
  233. justify-content: space-between;
  234. }
  235. .u-left-icon {
  236. @include vue-flex;
  237. align-items: center;
  238. }
  239. .u-notice-box {
  240. flex: 1;
  241. @include vue-flex;
  242. overflow: hidden;
  243. margin-left: 12rpx;
  244. }
  245. .u-right-icon {
  246. margin-left: 12rpx;
  247. @include vue-flex;
  248. align-items: center;
  249. }
  250. .u-notice-content {
  251. line-height: 1;
  252. white-space: nowrap;
  253. font-size: 26rpx;
  254. animation: u-loop-animation 10s linear infinite both;
  255. text-align: right;
  256. // 这一句很重要,为了能让滚动左右连接起来
  257. padding-left: 100%;
  258. }
  259. @keyframes u-loop-animation {
  260. 0% {
  261. transform: translate3d(0, 0, 0);
  262. }
  263. 100% {
  264. transform: translate3d(-100%, 0, 0);
  265. }
  266. }
  267. </style>