xzw-notice.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <view class="xzw_notice" :style="{color:getColor(theme),backgroundColor:getBgColor(theme)}">
  3. <uni-icons v-if="showIcon === true || showIcon === 'true'" class="notice_left" type="sound" :color="getColor(theme)" size="22" />
  4. <!-- 垂直滚动用到轮播组件 -->
  5. <swiper class="notice_center" vertical v-if="direction=='column'" :autoplay="true" :interval="speed=='fast'?4000:speed=='normal'?6000:10000" :duration="500" :circular="true" disable-touch>
  6. <swiper-item v-for="(item, index) in list" :key="index" class="swiperIn" @click="goItem(item)">
  7. <view>{{item[theKey]}}</view>
  8. </swiper-item>
  9. </swiper>
  10. <!-- 水平滚动取第一条公告 -->
  11. <view class="notice_center2" v-else>
  12. <view :class="speed">{{list[rowIndex][theKey]}}</view>
  13. </view>
  14. <view class="notice_right" v-if="showMore">
  15. <view @click="goMore">更多</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: "xzw-notice",
  22. props: {
  23. //主题色,default|primary|error|warning|success|info
  24. theme: {
  25. type: String,
  26. default: 'default'
  27. },
  28. // 是否显示左侧icon
  29. showIcon: {
  30. type: [Boolean, String],
  31. default: true
  32. },
  33. // 是否显示更多
  34. showMore: {
  35. type: [Boolean, String],
  36. default: true
  37. },
  38. //公告数组,必须是二维数组
  39. list: {
  40. type: Array,
  41. default () {
  42. return [{ id: 1, title: '公告1' }, { id: 2, title: '公告2' }]
  43. }
  44. },
  45. //公告数组的键名
  46. theKey: {
  47. type: String,
  48. default: 'title'
  49. },
  50. //方向,column垂直,row水平时取第一条公告
  51. direction: {
  52. type: String,
  53. default: 'column'
  54. },
  55. //滚动速度,快fast,慢slow,默认normal
  56. speed: {
  57. type: String,
  58. default: 'normal'
  59. }
  60. },
  61. data() {
  62. return {
  63. rowIndex: 0
  64. }
  65. },
  66. methods: {
  67. getColor(theme) {
  68. if (theme == "primary") {
  69. return "#2979FF"
  70. } else if (theme == "error") {
  71. return "#FA3534"
  72. } else if (theme == "warning") {
  73. return "#FF9A43"
  74. } else if (theme == "success") {
  75. return "#1BBF6C"
  76. } else if (theme == "info") {
  77. return "#909399"
  78. }else if(theme.indexOf('#') != -1) {
  79. return theme
  80. } else {
  81. return "#303133"
  82. }
  83. },
  84. getBgColor(theme) {
  85. if (theme == "primary") {
  86. return "#ECF5FF"
  87. } else if (theme == "error") {
  88. return "#FEF0F0"
  89. } else if (theme == "warning") {
  90. return "#FDF6EC"
  91. } else if (theme == "success") {
  92. return "#DBF1E1"
  93. } else if (theme == "info") {
  94. return "#F4F4F5"
  95. } else {
  96. return "#FFFFFF"
  97. }
  98. },
  99. // 点击公告
  100. goItem(item) {
  101. this.$emit('goItem', item)
  102. },
  103. //点击更多
  104. goMore() {
  105. this.$emit('goMore')
  106. }
  107. },
  108. created() {
  109. if (this.direction == "row") {
  110. let time = this.speed == "fast" ? 4000 : this.speed == "normal" ? 6000 : 10000
  111. setInterval(() => {
  112. if (this.rowIndex == this.list.length - 1) {
  113. this.rowIndex = 0;
  114. } else {
  115. this.rowIndex++;
  116. }
  117. }, time);
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .xzw_notice {
  124. font-size: 26upx;
  125. height: 90upx;
  126. display: flex;
  127. align-items: center;
  128. justify-content: space-between;
  129. padding: 0 20upx;
  130. box-sizing: border-box;
  131. width: 100%;
  132. .notice_left {
  133. margin: 0 20upx 0 0;
  134. }
  135. .notice_center {
  136. flex: 1;
  137. height: 90upx;
  138. .swiperIn {
  139. height: 80upx;
  140. display: flex;
  141. align-items: center;
  142. view {
  143. overflow: hidden;
  144. display: -webkit-box;
  145. -webkit-line-clamp: 1;
  146. -webkit-box-orient: vertical;
  147. }
  148. }
  149. }
  150. .notice_center2 {
  151. flex: 1;
  152. position: relative;
  153. display: flex;
  154. align-items: center;
  155. height: 90upx;
  156. overflow: hidden;
  157. view {
  158. position: absolute;
  159. white-space: nowrap;
  160. padding-left: 100%;
  161. &.fast {
  162. animation: notice 4s linear infinite both;
  163. }
  164. &.normal {
  165. animation: notice 6s linear infinite both;
  166. }
  167. &.slow {
  168. animation: notice 10s linear infinite both;
  169. }
  170. }
  171. }
  172. .notice_right {
  173. margin: 0 0 0 20upx;
  174. }
  175. @keyframes notice {
  176. 100% {
  177. transform: translate3d(-100%, 0, 0);
  178. }
  179. }
  180. }
  181. </style>