|
| 1 | +<template> |
| 2 | + <div v-if="show" class="i-noticebar" :style="{color: color, backgroundColor: backgroundColor}" :class="iClass"> |
| 3 | + <i-icon v-if="icon" :type="icon" size="24" i-class="i-noticebar-icon"></i-icon> |
| 4 | + <div class="i-noticebar-content-wrap" :class="wrapClassObj"> |
| 5 | + <div class="i-noticebar-content" :class="classObj" :animation="animationData"> |
| 6 | + <slot></slot> |
| 7 | + </div> |
| 8 | + </div> |
| 9 | + <i-icon v-if="closable" i-class="i-noticebar-operation" type="close" size="20" :color="color" @click="handleClose"></i-icon> |
| 10 | + </div> |
| 11 | +</template> |
| 12 | +<script> |
| 13 | +import icon from '../icon/icon' |
| 14 | +export default { |
| 15 | + components: { |
| 16 | + 'i-icon': icon |
| 17 | + }, |
| 18 | + props: { |
| 19 | + closable: { |
| 20 | + type: Boolean, |
| 21 | + default: false |
| 22 | + }, |
| 23 | + icon: { |
| 24 | + type: String, |
| 25 | + default: '' |
| 26 | + }, |
| 27 | + loop: { |
| 28 | + type: Boolean, |
| 29 | + default: false |
| 30 | + }, |
| 31 | + backgroundColor: { |
| 32 | + type: String, |
| 33 | + default: '#fefcec' |
| 34 | + }, |
| 35 | + color: { |
| 36 | + type: String, |
| 37 | + default: '#f76a24' |
| 38 | + }, |
| 39 | + speed: { |
| 40 | + type: Number, |
| 41 | + default: 1000 |
| 42 | + }, |
| 43 | + iClass: { |
| 44 | + type: String, |
| 45 | + default: '' |
| 46 | + } |
| 47 | + }, |
| 48 | + data() { |
| 49 | + const animElemId = "C_" + Math.ceil(Math.random() * 10e5).toString(36); |
| 50 | + const wrapElemId = "W_" + Math.ceil(Math.random() * 10e5).toString(36); |
| 51 | + return { |
| 52 | + show: true, |
| 53 | + animationData: null, |
| 54 | + animation: null, |
| 55 | + timer: null, |
| 56 | + wrapWidth: 0, |
| 57 | + width: 0, |
| 58 | + duration: 0, |
| 59 | + animElemId, |
| 60 | + wrapElemId |
| 61 | + } |
| 62 | + }, |
| 63 | + mounted() { |
| 64 | + if (this.loop) { |
| 65 | + setTimeout(() => { |
| 66 | + this.initAnimation() |
| 67 | + }, 100) |
| 68 | + } |
| 69 | + }, |
| 70 | + onShow() { |
| 71 | + if (this.timer) { |
| 72 | + this.destroyTimer() |
| 73 | + if (this.loop) { |
| 74 | + setTimeout(() => { |
| 75 | + this.initAnimation() |
| 76 | + }, 100) |
| 77 | + } |
| 78 | + } |
| 79 | + }, |
| 80 | + computed: { |
| 81 | + classObj() { |
| 82 | + return this.loop ? 'i-noticebar-content-loop ' + this.animElemId : this.animElemId |
| 83 | + }, |
| 84 | + wrapClassObj() { |
| 85 | + return this.wrapElemId |
| 86 | + } |
| 87 | + }, |
| 88 | + methods: { |
| 89 | + initAnimation() { |
| 90 | + const globalMpvue = wx || swan || tt || my |
| 91 | + if (!globalMpvue.createSelectorQuery) { |
| 92 | + return |
| 93 | + } |
| 94 | + globalMpvue.createSelectorQuery().in(this.$root.$mp.page).select(`.${this.wrapElemId}`).boundingClientRect() |
| 95 | + .select(`.${this.animElemId}`).boundingClientRect() |
| 96 | + .exec((retArray) => { |
| 97 | + this.createAnimation(globalMpvue, retArray[0], retArray[1]) |
| 98 | + }) |
| 99 | + }, |
| 100 | + startAnimation() { |
| 101 | + const resetAction = () => { |
| 102 | + const resetAnimation = this.animation.translateX(this.wrapWidth).step() |
| 103 | + this.animationData = resetAnimation.export() |
| 104 | + } |
| 105 | + // 微信小程序 |
| 106 | + if (this.animation.option && this.animation.option.transition) { |
| 107 | + if (this.animation.option.transition.duration !== 0) { |
| 108 | + this.animation.option.transition.duration = 0 |
| 109 | + resetAction() |
| 110 | + } |
| 111 | + this.animation.option.transition.duration = this.duration |
| 112 | + } |
| 113 | + // 头条小程序 |
| 114 | + if (this.animation.option && this.animation.option.hasOwnProperty('duration')) { |
| 115 | + if (this.animation.option.duration !== 0) { |
| 116 | + this.animation.option.duration = 0 |
| 117 | + resetAction() |
| 118 | + } |
| 119 | + this.animation.option.duration = this.duration |
| 120 | + } |
| 121 | + // 支付宝小程序 |
| 122 | + if (this.animation.config && this.animation.config.hasOwnProperty('duration')) { |
| 123 | + if (this.animation.config.duration !== 0) { |
| 124 | + this.animation.config.duration = 0 |
| 125 | + resetAction() |
| 126 | + } |
| 127 | + this.animation.config.duration = this.duration |
| 128 | + } |
| 129 | + // 百度小程序 |
| 130 | + if (this.animation.hasOwnProperty('duration')) { |
| 131 | + if (this.animation.duration !== 0) { |
| 132 | + this.animation.duration = 0 |
| 133 | + resetAction() |
| 134 | + } |
| 135 | + this.animation.duration = this.duration |
| 136 | + } |
| 137 | + const animationData = this.animation.translateX(-this.width).step() |
| 138 | + setTimeout(() => { |
| 139 | + this.animationData = animationData.export() |
| 140 | + }, 100) |
| 141 | + this.timer = setTimeout(() => { |
| 142 | + this.startAnimation() |
| 143 | + }, this.duration) |
| 144 | + }, |
| 145 | + createAnimation(globalMpvue, wrapRect, rect) { |
| 146 | + const duration = this.duration = rect.width / 40 * this.speed |
| 147 | + this.animation = globalMpvue.createAnimation({ |
| 148 | + duration: duration, |
| 149 | + timingFunction: "linear", |
| 150 | + }) |
| 151 | + this.wrapWidth = wrapRect.width |
| 152 | + this.width = rect.width |
| 153 | + this.startAnimation() |
| 154 | + }, |
| 155 | + destroyTimer() { |
| 156 | + if (this.timer) { |
| 157 | + clearTimeout(this.timer) |
| 158 | + const animationData = this.animation.translateX(this.wrapWidth).step() |
| 159 | + this.animationData = animationData.export() |
| 160 | + } |
| 161 | + }, |
| 162 | + handleClose() { |
| 163 | + console.log(123) |
| 164 | + this.destroyTimer() |
| 165 | + this.show = false |
| 166 | + this.timer = null |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +</script> |
| 171 | + |
0 commit comments