Commit 219cea56 by 胡悦

修改跑马灯

1 parent d6643460
<template> <template>
<div id="app"> <div id="app">
<router-view /> <router-view />
<div id="ruler" ref="ruler" class="ruler" style=""></div>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'App' name: 'App',
mounted() {
var _this = this
String.prototype.visualLength = function (size, family) {
const ruler = _this.$refs.ruler
ruler.style.fontSize = size || 'inherit'
ruler.innerHTML = this
return ruler.offsetWidth
}
}
} }
</script> </script>
<style lang="scss"></style> <style lang="scss">
.ruler {
position: absolute;
bottom: 0;
visibility: hidden;
white-space: nowrap;
z-index: -100
}
</style>
<template>
<div class="marquee-box" ref="marqueeBox">
<span class="marquee" ref="marqueeText" :style="[{ transition: `all ${move ? speed : 0}s linear` }, { left: target }]">{{ text }}</span>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'Marquee',
props: {
text: String,
direction: {
type: Number,
default: 5000
},
delay: {
type: Number,
default: 1000
}
},
data () {
return {
move: false,
valueWidth: 0,
target: 0,
boxWidth: 0,
speed: 0
}
},
watch:{
'text'(n){
if(n)setTimeout(() => {
let { marqueeBox, marqueeText } = this.$refs
if (!marqueeBox) return
let boxWidth = marqueeBox.offsetWidth,
valueWidth = marqueeText.offsetWidth
this.valueWidth = valueWidth
this.boxWidth = boxWidth
if (boxWidth - 10 <= valueWidth) this.run(valueWidth)
}, this.delay)
}
},
mounted () {
},
methods: {
run () {
let { valueWidth, direction } = this
this.target = `-${valueWidth}px`
this.move = true
this.speed = direction / 1000
setTimeout(() => {
this.again()
}, direction)
},
again () {
let { boxWidth, valueWidth, direction } = this
this.move = false
this.target = `${boxWidth}px`
setTimeout(() => {
this.speed = direction * (boxWidth / valueWidth).toFixed(2) / 1000
this.move = true
this.reset()
}, 500)
},
reset () {
let awaitTime = (this.speed + 2) * this.delay
this.target = 0
setTimeout(() => {
this.run()
}, awaitTime)
}
}
}
</script>
<style scoped lang="scss">
.marquee-box {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
.marquee {
position: absolute;
white-space: nowrap;
display: flex;
align-items: center;
}
.move {
transform: translateX(-100%);
}
}
</style>
<template>
<marquee ref="marqueeBox" :key="key" :behavior="behavior" :direction="direction" :scrollamount="speed" :scrolldelay="scrolldelay">{{ text }}</marquee>
</template>
<script type="text/ecmascript-6">
export default {
name: 'Marquees',
props: {
text: String,
behavior: {
type: String,
default: 'scroll'
},
direction: {
type: String,
default: 'left'
},
scrollamount: {
type: Number,
default: 2
},
scrolldelay: {
type: Number,
default: 100
},
fontSize: {
type: String,
default: '0.694vw'
}
},
data() {
return {
boxWidth: 0,
valueWidth: 0,
speed: this.scrollamount,
key: new Date().getTime()
}
},
watch: {
text(val) {
this.$nextTick(() => {
this.computedValue()
})
}
},
mounted() {
this.$nextTick(() => {
this.computedValue()
})
// window.addEventListener('resize', () => {
// this.computedValue
// })
},
methods: {
getTextLength() {
return this.text.visualLength(this.fontSize)
},
computedValue() {
const { marqueeBox } = this.$refs
console.log(this.text)
this.boxWidth = marqueeBox.offsetWidth // 盒子宽度
this.valueWidth = this.text.visualLength(this.fontSize) // 内容文本宽度
const { boxWidth, valueWidth } = this
console.log(valueWidth, boxWidth)
if (valueWidth < boxWidth) {
this.$set(this, 'speed', 0)
} else {
this.$set(this, 'speed', this.scrollamount)
}
this.key = new Date().getTime()
console.log(valueWidth > boxWidth, this.speed, this.scrollamount)
}
}
}
</script>
<style scoped lang="scss">
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!