index.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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)+'; background: '+(background)" @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. VantComponent({
  24. props: {
  25. text: {
  26. type: String,
  27. value: '',
  28. observer() {
  29. wx.nextTick(() => {
  30. this.init();
  31. });
  32. },
  33. },
  34. mode: {
  35. type: String,
  36. value: '',
  37. },
  38. url: {
  39. type: String,
  40. value: '',
  41. },
  42. openType: {
  43. type: String,
  44. value: 'navigate',
  45. },
  46. delay: {
  47. type: Number,
  48. value: 1,
  49. },
  50. speed: {
  51. type: Number,
  52. value: 50,
  53. observer() {
  54. wx.nextTick(() => {
  55. this.init();
  56. });
  57. },
  58. },
  59. scrollable: {
  60. type: Boolean,
  61. value: true,
  62. },
  63. leftIcon: {
  64. type: String,
  65. value: '',
  66. },
  67. color: String,
  68. backgroundColor: String,
  69. background: String,
  70. wrapable: Boolean,
  71. },
  72. data: {
  73. show: true,
  74. },
  75. created() {
  76. this.resetAnimation = wx.createAnimation({
  77. duration: 0,
  78. timingFunction: 'linear',
  79. });
  80. },
  81. destroyed() {
  82. this.timer && clearTimeout(this.timer);
  83. },
  84. methods: {
  85. init() {
  86. Promise.all([
  87. this.getRect('.van-notice-bar__content'),
  88. this.getRect('.van-notice-bar__wrap'),
  89. ]).then((rects) => {
  90. const [contentRect, wrapRect] = rects;
  91. if (
  92. contentRect == null ||
  93. wrapRect == null ||
  94. !contentRect.width ||
  95. !wrapRect.width
  96. ) {
  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(event) {
  136. if (this.data.mode === 'closeable') {
  137. this.timer && clearTimeout(this.timer);
  138. this.timer = null;
  139. this.setData({ show: false });
  140. this.$emit('close', event.detail);
  141. }
  142. },
  143. onClick(event) {
  144. this.$emit('click', event);
  145. },
  146. },
  147. });
  148. export default global['__wxComponents']['vant/notice-bar/index']
  149. </script>
  150. <style platform="mp-weixin">
  151. @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%}
  152. </style>