index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <uni-shadow-root class="weapp-lib-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" :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'] = 'weapp/lib/notice-bar/index'
  23. "use strict";
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var component_1 = require("../common/component");
  26. var utils_1 = require("../common/utils");
  27. (0, component_1.VantComponent)({
  28. props: {
  29. text: {
  30. type: String,
  31. value: '',
  32. observer: 'init',
  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: 60,
  53. observer: 'init',
  54. },
  55. scrollable: null,
  56. leftIcon: {
  57. type: String,
  58. value: '',
  59. },
  60. color: String,
  61. backgroundColor: String,
  62. background: String,
  63. wrapable: Boolean,
  64. },
  65. data: {
  66. show: true,
  67. },
  68. created: function () {
  69. this.resetAnimation = wx.createAnimation({
  70. duration: 0,
  71. timingFunction: 'linear',
  72. });
  73. },
  74. destroyed: function () {
  75. this.timer && clearTimeout(this.timer);
  76. },
  77. mounted: function () {
  78. this.init();
  79. },
  80. methods: {
  81. init: function () {
  82. var _this = this;
  83. (0, utils_1.requestAnimationFrame)(function () {
  84. Promise.all([
  85. (0, utils_1.getRect)(_this, '.van-notice-bar__content'),
  86. (0, utils_1.getRect)(_this, '.van-notice-bar__wrap'),
  87. ]).then(function (rects) {
  88. var contentRect = rects[0], wrapRect = rects[1];
  89. var _a = _this.data, speed = _a.speed, scrollable = _a.scrollable, delay = _a.delay;
  90. if (contentRect == null ||
  91. wrapRect == null ||
  92. !contentRect.width ||
  93. !wrapRect.width ||
  94. scrollable === false) {
  95. return;
  96. }
  97. if (scrollable || wrapRect.width < contentRect.width) {
  98. var duration = ((wrapRect.width + contentRect.width) / speed) * 1000;
  99. _this.wrapWidth = wrapRect.width;
  100. _this.contentWidth = contentRect.width;
  101. _this.duration = duration;
  102. _this.animation = wx.createAnimation({
  103. duration: duration,
  104. timingFunction: 'linear',
  105. delay: delay,
  106. });
  107. _this.scroll(true);
  108. }
  109. });
  110. });
  111. },
  112. scroll: function (isInit) {
  113. var _this = this;
  114. if (isInit === void 0) { isInit = false; }
  115. this.timer && clearTimeout(this.timer);
  116. this.timer = null;
  117. this.setData({
  118. animationData: this.resetAnimation
  119. .translateX(isInit ? 0 : this.wrapWidth)
  120. .step()
  121. .export(),
  122. });
  123. (0, utils_1.requestAnimationFrame)(function () {
  124. _this.setData({
  125. animationData: _this.animation
  126. .translateX(-_this.contentWidth)
  127. .step()
  128. .export(),
  129. });
  130. });
  131. this.timer = setTimeout(function () {
  132. _this.scroll();
  133. }, this.duration);
  134. },
  135. onClickIcon: function (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: function (event) {
  144. this.$emit('click', event);
  145. },
  146. },
  147. });
  148. export default global['__wxComponents']['weapp/lib/notice-bar/index']
  149. </script>
  150. <style platform="mp-weixin">
  151. @import '../common/index.css';.van-notice-bar{align-items:center;background-color:var(--notice-bar-background-color,#fffbe8);color:var(--notice-bar-text-color,#ed6a0c);display:flex;font-size:var(--notice-bar-font-size,14px);height:var(--notice-bar-height,40px);line-height:var(--notice-bar-line-height,24px);padding:var(--notice-bar-padding,0 16px)}.van-notice-bar--withicon{padding-right:40px;position:relative}.van-notice-bar--wrapable{height:auto;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{align-items:center;display:flex;margin-right:4px;vertical-align:middle}.van-notice-bar__left-icon,.van-notice-bar__right-icon{font-size:var(--notice-bar-icon-size,16px);min-width:var(--notice-bar-icon-min-width,22px)}.van-notice-bar__right-icon{position:absolute;right:15px;top:10px}.van-notice-bar__wrap{flex:1;height:var(--notice-bar-line-height,24px);overflow:hidden;position:relative}.van-notice-bar__content{position:absolute;white-space:nowrap}.van-notice-bar__content.van-ellipsis{max-width:100%}
  152. </style>