123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <template>
- <view
- v-if="show"
- :class="'custom-class ' + utils.bem('notice-bar', { withicon: mode, wrapable })"
- :style="'color: ' + color + '; background-color: ' + backgroundColor + ';'"
- @tap="onClick"
- >
- <van-icon v-if="leftIcon" size="16px" :name="leftIcon" class="van-notice-bar__left-icon" />
- <slot v-else name="left-icon" />
- <view class="van-notice-bar__wrap">
- <view :class="'van-notice-bar__content ' + (!scrollable && !wrapable ? 'van-ellipsis' : '')" :animation="animationData">
- {{ text }}
- </view>
- </view>
- <van-icon v-if="mode === 'closeable'" class="van-notice-bar__right-icon" name="cross" @tap.native.stop.prevent="onClickIcon" />
- <navigator v-else-if="mode === 'link'" :url="url" :open-type="openType">
- <van-icon class="van-notice-bar__right-icon" name="arrow" />
- </navigator>
- <slot v-else name="right-icon" />
- </view>
- </template>
- <script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
- <script>
- import { VantComponent } from '../common/component';
- const FONT_COLOR = '#ed6a0c';
- const BG_COLOR = '#fffbe8';
- export default {
- data() {
- return {
- show: true,
- animationData: ''
- };
- },
- props: {
- text: {
- type: String,
- default: ''
- },
- mode: {
- type: String,
- default: ''
- },
- url: {
- type: String,
- default: ''
- },
- openType: {
- type: String,
- default: 'navigate'
- },
- delay: {
- type: Number,
- default: 1
- },
- speed: {
- type: Number,
- default: 50
- },
- scrollable: {
- type: Boolean,
- default: true
- },
- leftIcon: {
- type: String,
- default: ''
- },
- color: {
- type: String,
- default: FONT_COLOR
- },
- backgroundColor: {
- type: String,
- default: BG_COLOR
- },
- wrapable: Boolean
- },
- created() {
- this.resetAnimation = uni.createAnimation({
- duration: 0,
- timingFunction: 'linear'
- });
- },
- destroyed() {
- if (this.timer) {
- clearTimeout(this.timer);
- }
- },
- methods: {
- init() {
- Promise.all([this.getRect('.van-notice-bar__content'), this.getRect('.van-notice-bar__wrap')]).then((rects) => {
- const [contentRect, wrapRect] = rects;
- if (contentRect == null || wrapRect == null || !contentRect.width || !wrapRect.width) {
- return;
- }
- const { speed, scrollable, delay } = this;
- if (scrollable && wrapRect.width < contentRect.width) {
- const duration = (contentRect.width / speed) * 1000;
- this.wrapWidth = wrapRect.width;
- this.contentWidth = contentRect.width;
- this.duration = duration;
- this.animation = uni.createAnimation({
- duration,
- timingFunction: 'linear',
- delay
- });
- this.scroll();
- }
- });
- },
- scroll() {
- if (this.timer) {
- clearTimeout(this.timer);
- }
- this.timer = null;
- this.setData({
- animationData: this.resetAnimation.translateX(this.wrapWidth).step().export()
- });
- setTimeout(() => {
- this.setData({
- animationData: this.animation.translateX(-this.contentWidth).step().export()
- });
- }, 20);
- this.timer = setTimeout(() => {
- this.scroll();
- }, this.duration);
- },
- onClickIcon() {
- if (this.timer) {
- clearTimeout(this.timer);
- }
- this.timer = null;
- this.setData({
- show: false
- });
- },
- onClick(event) {
- this.$emit('click', event);
- }
- },
- watch: {
- text: {
- handler: function () {
- this.$nextTick(() => {
- this.init();
- });
- },
- immediate: true
- },
- speed: {
- handler: function () {
- this.$nextTick(() => {
- this.init();
- });
- },
- immediate: true
- }
- }
- };
- </script>
- <style>
- @import './index.css';
- </style>
|