上图 gif有问题丢帧,不是组件有这样的问题

组件代码
<template>
<div class="textScroll" @mouseenter="mouseHover" @mouseleave="mouseLevel">
<div class="wrapper" ref="wrapper">
<div ref="marquee" style="text-align: left;">
<span class="text-content" ref="marqueeContent">
<slot></slot>
</span>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
startFromTheEnd: {
type: Boolean,
default: true
},
scrollSpeed: {
type: Number,
default: 25
},
isHoverStop: {
type: Boolean,
default: true
},
delay: {
type: Number,
default: 0
}
},
components: {},
data() {
return {
timer: null,
isPlay: null,
nowPosition: 0
}
},
created() {
},
mounted() {
this.init()
},
computed: {},
watch: {},
methods: {
move(startPosition) {
let wrapper = this.$refs.wrapper
let wrapperWidth = wrapper.getBoundingClientRect().width
let marquee = this.$refs.marquee
let marqueeContent = this.$refs.marqueeContent
let contentWidth = marqueeContent.getBoundingClientRect().width
let distance = startPosition || (this.startFromTheEnd ? 0 : wrapperWidth)
this.timer = setInterval(() => {
if (marquee.getBoundingClientRect().x + contentWidth < wrapper.getBoundingClientRect().x) {
distance = wrapperWidth
}
distance--
this.nowPosition = distance
marquee.style.transform = 'translateX(' + distance + 'px)'
}, this.scrollSpeed)
},
init() {
let wrapper = this.$refs.wrapper
let content = this.$refs.marqueeContent
let wrapperWidth = wrapper.getBoundingClientRect().width
let contentWidth = content.getBoundingClientRect().width
this.isPlay = contentWidth > wrapperWidth
setTimeout(() => {
this.isPlay && this.move()
}, this.delay * 1000)
},
mouseHover() {
if (this.isHoverStop && this.isPlay) {
clearInterval(this.timer)
}
},
mouseLevel() {
if (this.isHoverStop && this.isPlay) {
this.move(this.nowPosition)
}
}
},
beforeDestroy() {
this.isPlay = false
this.timer && clearInterval(this.timer)
this.timer = null
}
}
</script>
<style lang="scss" scoped>
.textScroll {
overflow: hidden;
width: 100%;
.text-content {
white-space: nowrap;
}
}
</style>
使用方式
<div style="width: 200px;border:1px solid red;color: red">
<textScrolling >
哈哈哈哈嘻嘻嘻啊啊啊啊啊啊啊啊
</textScrolling>
<textScrolling>
没超过父元素是不会滚的
</textScrolling>
</div>