123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <template>
- <view
- class="u-notice"
- @tap="clickHandler"
- >
- <slot name="icon">
- <view
- class="u-notice__left-icon"
- v-if="icon"
- >
- <u-icon
- :name="icon"
- :color="color"
- size="19"
- ></u-icon>
- </view>
- </slot>
- <view
- class="u-notice__content"
- ref="u-notice__content"
- >
- <view
- ref="u-notice__content__text"
- class="u-notice__content__text"
- :style="[animationStyle]"
- >
- <text
- v-for="(item, index) in innerText"
- :key="index"
- :style="[textStyle]"
- >{{item}}</text>
- </view>
- </view>
- <view
- class="u-notice__right-icon"
- v-if="['link', 'closable'].includes(mode)"
- >
- <u-icon
- v-if="mode === 'link'"
- name="arrow-right"
- :size="17"
- :color="color"
- ></u-icon>
- <u-icon
- v-if="mode === 'closable'"
- @click="close"
- name="close"
- :size="16"
- :color="color"
- ></u-icon>
- </view>
- </view>
- </template>
- <script>
- import props from './props.js';
-
- const animation = uni.requireNativePlugin('animation')
- const dom = uni.requireNativePlugin('dom')
-
-
- export default {
- name: 'u-row-notice',
- mixins: [uni.$u.mpMixin, uni.$u.mixin,props],
- data() {
- return {
- animationDuration: '0',
- animationPlayState: 'paused',
-
-
- nvueInit: true,
- show: true
- };
- },
- watch: {
- text: {
- immediate: true,
- handler(newValue, oldValue) {
-
- this.nvueInit = true
-
-
- this.vue()
-
-
- if(!uni.$u.test.string(newValue)) {
- uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
- }
- }
- },
- fontSize() {
-
- this.nvueInit = true
-
-
- this.vue()
-
- },
- speed() {
-
- this.nvueInit = true
-
-
- this.vue()
-
- }
- },
- computed: {
-
- textStyle() {
- let style = {}
- style.color = this.color
- style.fontSize = uni.$u.addUnit(this.fontSize)
- return style
- },
- animationStyle() {
- let style = {}
- style.animationDuration = this.animationDuration
- style.animationPlayState = this.animationPlayState
- return style
- },
-
-
- innerText() {
- let result = [],
-
- len = 20
- const textArr = this.text.split('')
- for (let i = 0; i < textArr.length; i += len) {
-
- result.push(textArr.slice(i, i + len).join(''))
- }
- return result
- }
- },
- mounted() {
-
-
-
- var pages = getCurrentPages()
- var page = pages[pages.length - 1]
- var currentWebview = page.$getAppWebview()
- currentWebview.addEventListener('hide', () => {
- this.webviewHide = true
- })
- currentWebview.addEventListener('show', () => {
- this.webviewHide = false
- })
-
- this.init()
- },
- methods: {
- init() {
-
- this.nvue()
-
-
- this.vue()
-
-
- if(!uni.$u.test.string(this.text)) {
- uni.$u.error('noticebar组件direction为row时,要求text参数为字符串形式')
- }
- },
-
- async vue() {
-
- let boxWidth = 0,
- textWidth = 0
-
- await uni.$u.sleep()
-
- textWidth = (await this.$uGetRect('.u-notice__content__text')).width
- boxWidth = (await this.$uGetRect('.u-notice__content')).width
-
-
- this.animationDuration = `${textWidth / uni.$u.getPx(this.speed)}s`
-
- this.animationPlayState = 'paused'
- setTimeout(() => {
- this.animationPlayState = 'running'
- }, 10)
-
- },
-
- async nvue() {
-
- this.nvueInit = false
- let boxWidth = 0,
- textWidth = 0
-
- await uni.$u.sleep()
-
- textWidth = (await this.getNvueRect('u-notice__content__text')).width
- boxWidth = (await this.getNvueRect('u-notice__content')).width
-
- animation.transition(this.$refs['u-notice__content__text'], {
- styles: {
- transform: `translateX(${boxWidth}px)`
- },
- }, () => {
-
- !this.stopAnimation && this.loopAnimation(textWidth, boxWidth)
- });
-
- },
- loopAnimation(textWidth, boxWidth) {
-
- animation.transition(this.$refs['u-notice__content__text'], {
- styles: {
-
- transform: `translateX(-${textWidth}px)`
- },
-
- duration: (boxWidth + textWidth) / uni.$u.getPx(this.speed) * 1000,
- delay: 10
- }, () => {
- animation.transition(this.$refs['u-notice__content__text'], {
- styles: {
-
- transform: `translateX(${this.stopAnimation ? 0 : boxWidth}px)`
- },
- }, () => {
-
- if (!this.stopAnimation) {
-
- if (this.nvueInit) {
- this.nvue()
- } else {
- this.loopAnimation(textWidth, boxWidth)
- }
- }
- });
- })
-
- },
- getNvueRect(el) {
-
-
- return new Promise(resolve => {
- dom.getComponentRect(this.$refs[el], (res) => {
- resolve(res.size)
- })
- })
-
- },
-
- clickHandler(index) {
- this.$emit('click')
- },
-
- close() {
- this.$emit('close')
- }
- },
-
- beforeDestroy() {
- this.stopAnimation = true
- },
-
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/components.scss";
- .u-notice {
- @include flex;
- align-items: center;
- justify-content: space-between;
- &__left-icon {
- align-items: center;
- margin-right: 5px;
- }
- &__right-icon {
- margin-left: 5px;
- align-items: center;
- }
- &__content {
- text-align: right;
- flex: 1;
- @include flex;
- flex-wrap: nowrap;
- overflow: hidden;
- &__text {
- font-size: 14px;
- color: $u-warning;
-
-
- padding-left: 100%;
- word-break: keep-all;
- white-space: nowrap;
- animation: u-loop-animation 10s linear infinite both;
-
- @include flex(row);
- }
- }
- }
- @keyframes u-loop-animation {
- 0% {
- transform: translate3d(0, 0, 0);
- }
- 100% {
- transform: translate3d(-100%, 0, 0);
- }
- }
- </style>
|