xzw-notice.vue 4.1 KB

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