index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <uni-shadow-root class="vant-notice-bar-index"><view v-if="show" :class="'custom-class '+(utils.bem('notice-bar', { withicon: mode, wrapable }))" :style="'color: '+(color)+'; background-color: '+(backgroundColor)+';'" @click="onClick">
  3. <van-icon v-if="leftIcon" size="16px" :name="leftIcon" class="van-notice-bar__left-icon"></van-icon>
  4. <slot v-else name="left-icon"></slot>
  5. <view class="van-notice-bar__wrap">
  6. <view :class="'van-notice-bar__content '+(!scrollable && !wrapable ? 'van-ellipsis' : '')" :animation="animationData">
  7. {{ text }}
  8. </view>
  9. </view>
  10. <van-icon v-if="mode === 'closeable'" class="van-notice-bar__right-icon" name="cross" @click.native.stop.prevent="onClickIcon"></van-icon>
  11. <navigator v-else-if="mode === 'link'" :url="url" :open-type="openType">
  12. <van-icon class="van-notice-bar__right-icon" name="arrow"></van-icon>
  13. </navigator>
  14. <slot v-else name="right-icon"></slot>
  15. </view></uni-shadow-root>
  16. </template>
  17. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  18. <script>
  19. import VanIcon from '../icon/index.vue'
  20. global['__wxVueOptions'] = {components:{'van-icon': VanIcon}}
  21. global['__wxRoute'] = 'vant/notice-bar/index'
  22. import { VantComponent } from '../common/component';
  23. const FONT_COLOR = '#ed6a0c';
  24. const BG_COLOR = '#fffbe8';
  25. VantComponent({
  26. props: {
  27. text: {
  28. type: String,
  29. value: ''
  30. },
  31. mode: {
  32. type: String,
  33. value: ''
  34. },
  35. url: {
  36. type: String,
  37. value: ''
  38. },
  39. openType: {
  40. type: String,
  41. value: 'navigate'
  42. },
  43. delay: {
  44. type: Number,
  45. value: 1
  46. },
  47. speed: {
  48. type: Number,
  49. value: 50
  50. },
  51. scrollable: {
  52. type: Boolean,
  53. value: true
  54. },
  55. leftIcon: {
  56. type: String,
  57. value: ''
  58. },
  59. color: {
  60. type: String,
  61. value: FONT_COLOR
  62. },
  63. backgroundColor: {
  64. type: String,
  65. value: BG_COLOR
  66. },
  67. wrapable: Boolean
  68. },
  69. data: {
  70. show: true
  71. },
  72. watch: {
  73. text() {
  74. this.setData({}, this.init);
  75. }
  76. },
  77. created() {
  78. this.resetAnimation = wx.createAnimation({
  79. duration: 0,
  80. timingFunction: 'linear'
  81. });
  82. },
  83. destroyed() {
  84. this.timer && clearTimeout(this.timer);
  85. },
  86. methods: {
  87. init() {
  88. Promise.all([
  89. this.getRect('.van-notice-bar__content'),
  90. this.getRect('.van-notice-bar__wrap')
  91. ]).then((rects) => {
  92. const [contentRect, wrapRect] = rects;
  93. if (contentRect == null ||
  94. wrapRect == null ||
  95. !contentRect.width ||
  96. !wrapRect.width) {
  97. return;
  98. }
  99. const { speed, scrollable, delay } = this.data;
  100. if (scrollable && wrapRect.width < contentRect.width) {
  101. const duration = (contentRect.width / speed) * 1000;
  102. this.wrapWidth = wrapRect.width;
  103. this.contentWidth = contentRect.width;
  104. this.duration = duration;
  105. this.animation = wx.createAnimation({
  106. duration,
  107. timingFunction: 'linear',
  108. delay
  109. });
  110. this.scroll();
  111. }
  112. });
  113. },
  114. scroll() {
  115. this.timer && clearTimeout(this.timer);
  116. this.timer = null;
  117. this.setData({
  118. animationData: this.resetAnimation
  119. .translateX(this.wrapWidth)
  120. .step()
  121. .export()
  122. });
  123. setTimeout(() => {
  124. this.setData({
  125. animationData: this.animation
  126. .translateX(-this.contentWidth)
  127. .step()
  128. .export()
  129. });
  130. }, 20);
  131. this.timer = setTimeout(() => {
  132. this.scroll();
  133. }, this.duration);
  134. },
  135. onClickIcon() {
  136. this.timer && clearTimeout(this.timer);
  137. this.timer = null;
  138. this.setData({ show: false });
  139. },
  140. onClick(event) {
  141. this.$emit('click', event);
  142. }
  143. }
  144. });
  145. export default global['__wxComponents']['vant/notice-bar/index']
  146. </script>
  147. <style platform="mp-weixin">
  148. @import '../common/index.css';.van-notice-bar{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center;height:40px;height:var(--notice-bar-height,40px);padding:0 16px;padding:var(--notice-bar-padding,0 16px);font-size:14px;font-size:var(--notice-bar-font-size,14px);color:#ed6a0c;color:var(--notice-bar-text-color,#ed6a0c);line-height:24px;line-height:var(--notice-bar-line-height,24px);background-color:#fffbe8;background-color:var(--notice-bar-background-color,#fffbe8)}.van-notice-bar--withicon{position:relative;padding-right:40px}.van-notice-bar--wrapable{height:auto;padding:8px 16px;padding:var(--notice-bar-wrapable-padding,8px 16px)}.van-notice-bar--wrapable .van-notice-bar__wrap{height:auto}.van-notice-bar--wrapable .van-notice-bar__content{position:relative;white-space:normal}.van-notice-bar__left-icon{display:-webkit-flex;display:flex;-webkit-align-items:center;align-items:center;margin-right:4px;vertical-align:middle}.van-notice-bar__left-icon,.van-notice-bar__right-icon{font-size:16px;font-size:var(--notice-bar-icon-size,16px);min-width:22px;min-width:var(--notice-bar-icon-min-width,22px)}.van-notice-bar__right-icon{position:absolute;top:10px;right:15px}.van-notice-bar__wrap{position:relative;-webkit-flex:1;flex:1;overflow:hidden;height:24px;height:var(--notice-bar-line-height,24px)}.van-notice-bar__content{position:absolute;white-space:nowrap}.van-notice-bar__content.van-ellipsis{max-width:100%}
  149. </style>