semp-notice-bar.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view class="semp-notice-bar" style="box-shadow: 0rpx 5rpx 12rpx 0rpx #c7c7c7;" :class="round?'round':''">
  3. <view class="show-box flex-row" :class="round?'round':''" v-show="showNotice" :style="{'background':bgColor}">
  4. <view class="icon" v-if="icon != 'none'">
  5. <uni-icon :type="icon" :size="iconSize" :color="setIconColor"></uni-icon>
  6. </view>
  7. <view class="show-info flex-row" :class="icon=='none'?'no-icon':''" v-if="showType=='slider'">
  8. <rich-text class="text-box" :class="rows && !scrollable?'text-rows':''" :style="{'margin-left':sw+'px','color':color,'font-size':textFontSize}" @click="onItemClick" :nodes="strText"></rich-text>
  9. </view>
  10. <view class="scroll-box" :style="{'color':color}" v-if="showType=='scrollTop'">
  11. <swiper vertical="true" autoplay="true" circular="true" interval="3000">
  12. <swiper-item v-for="(item, index) in arrayText" :key="index">
  13. <navigator @click="onItemClick">{{item}}</navigator>
  14. </swiper-item>
  15. </swiper>
  16. </view>
  17. <view class="scroll-box" :style="{'color':color}" v-if="showType=='scrollLeft'">
  18. <swiper autoplay="true" circular="true" interval="3000">
  19. <swiper-item v-for="(item, index) in arrayText" :key="index">
  20. <navigator @click="onItemClick">{{item}}</navigator>
  21. </swiper-item>
  22. </swiper>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import uniIcon from '../uni-icon/uni-icon.vue'
  29. export default {
  30. name: 'semp-notice-bar',
  31. components: {
  32. uniIcon
  33. },
  34. data() {
  35. return {
  36. boxWidth: '398',
  37. textwidth: '',
  38. sw: '0',
  39. timer: null,
  40. showNotice: true
  41. }
  42. },
  43. props: {
  44. strText: {
  45. type: String,
  46. default: '新版震撼发部了!',
  47. },
  48. //多条信息滚动模式
  49. arrayText: {
  50. type: Array,
  51. default () {
  52. return [
  53. '新版震撼发部了!',
  54. '人气爆红,发布日流量超过十万',
  55. '36氪热文榜推荐、CSDN公号推荐分享文章'
  56. ]
  57. }
  58. },
  59. //slider 滑动 scrollTop 上下滚动 scrollLeft横向滚动
  60. showType: {
  61. type: String,
  62. default: 'slider',
  63. },
  64. showTime: {
  65. type: [Number, String],
  66. default: ''
  67. },
  68. icon: {
  69. type: String,
  70. default: 'sound',
  71. },
  72. setIconColor: {
  73. type: String,
  74. default: '#3e6ffd',
  75. },
  76. iconSize:{
  77. type: Number,
  78. default: 30,
  79. },
  80. bgColor: {
  81. type: String,
  82. default: 'rgba(89, 158, 248, .08)',
  83. },
  84. color: {
  85. type: String,
  86. default: '#3e6ffd',
  87. },
  88. textFontSize: {
  89. type: String,
  90. default: '28rpx',
  91. },
  92. scrollable: {
  93. type: [Boolean, String],
  94. default: false,
  95. },
  96. //圆角
  97. round: {
  98. type: [Boolean, String],
  99. default: false,
  100. },
  101. //是否多行
  102. rows: {
  103. type: [Boolean, String],
  104. default: false,
  105. },
  106. },
  107. mounted() {
  108. let that = this;
  109. if (this.scrollable && !this.rows && this.showType == "slider") {
  110. that.sinit();
  111. setTimeout(function(){
  112. that.move();
  113. }, 2000)
  114. // this.move();
  115. }
  116. this.close()
  117. },
  118. methods: {
  119. sinit() {
  120. let query = uni.createSelectorQuery().in(this);
  121. let view = query.select(".text-box");
  122. view.fields({
  123. size: true,
  124. scrollOffset: true
  125. }, data => {
  126. // console.log("得到textwidth信息" + JSON.stringify(data));
  127. this.textwidth = data.width;
  128. }).exec();
  129. let box = query.select(".show-box");
  130. box.fields({
  131. size: true,
  132. scrollOffset: true
  133. }, data => {
  134. // console.log("得到boxWidth信息" + JSON.stringify(data));
  135. this.boxWidth = data.width;
  136. }).exec();
  137. },
  138. move() {
  139. let textLength = this.strText.length;
  140. this.sw = 0;
  141. this.timer = setInterval(() => {
  142. this.sw = this.sw - 1;
  143. if (-this.sw - textLength * 10 > this.textwidth) {
  144. this.sw = this.boxWidth
  145. }
  146. }, 25)
  147. // this.sw = this.boxWidth;
  148. // //console.error('运行长度:'+this.textwidth)
  149. // this.timer = setInterval(() => {
  150. // this.sw = this.sw - 1;
  151. // if (-this.sw - this.textwidth > 0) {
  152. // //clearInterval(this.timer);
  153. // this.sw = this.boxWidth
  154. // }
  155. // }, 25)
  156. },
  157. show() {
  158. this.showNotice = true;
  159. },
  160. close() {
  161. if ((this.timer || this.showType != 'slider') && this.showTime != '') {
  162. setInterval(() => {
  163. if (this.timer) {
  164. clearInterval(this.timer);
  165. }
  166. this.showNotice = false;
  167. }, this.showTime)
  168. }
  169. this.$emit("close");
  170. },
  171. onItemClick() {
  172. this.$emit('click')
  173. }
  174. },
  175. }
  176. </script>
  177. <style>
  178. .show-box {
  179. width: 96%;
  180. overflow: hidden;
  181. padding: 0 2%;
  182. }
  183. .text-box {
  184. display: block;
  185. white-space: nowrap;
  186. line-height: 36upx;
  187. padding: 20upx 0;
  188. }
  189. .icon {
  190. width: 60rpx;
  191. height: 76rpx;
  192. text-align: center;
  193. line-height: 76rpx;
  194. display: inline-block;
  195. vertical-align: middle;
  196. }
  197. .no-icon {
  198. flex: 1;
  199. width: calc(100%) !important;
  200. }
  201. .show-info {
  202. overflow: hidden;
  203. width: calc(100% - 60rpx);
  204. display: inline-block;
  205. vertical-align: middle;
  206. }
  207. .scroll-box {
  208. width: 94%;
  209. overflow: hidden;
  210. height: 76upx;
  211. line-height: 76upx;
  212. }
  213. .round {
  214. border-radius: 10rpx;
  215. }
  216. .text-box.text-rows {
  217. white-space: normal;
  218. }
  219. </style>