index.vue 6.2KB

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