1、 文字上下滚动(vue)
<template>
<div class="scroll-temp">
<img :src="noticePng" alt="" class="scroll-img">
<div ref="scroll" class="scroll-box">
<div v-for="item in scrollList" :key="item.id" class="scroll-content">
{{ item.title }}
</div>
</div>
</div>
</template>
<script>
import noticePng from '@/assets/images/notice.png'
export default {
props: {
scrollData: {
type: Array,
default: () => {
return []
}
}
},
data() {
return {
noticePng,
scrollArea: '',
speed: 10,
timer: null,
timeObj: null,
delay: 3000,
contentHeight: '',
scrollList: []
}
},
watch: {
scrollData: {
handler(val) {
this.scrollList = JSON.parse(JSON.stringify(val))
this.initScroll()
},
immediate: true,
deep: true
}
},
methods: {
initScroll() {
this.$nextTick(() => {
this.scrollArea = this.$refs.scroll
const content = this.scrollArea.getElementsByClassName('scroll-content')
this.contentHeight = content[0].offsetHeight
this.scrollArea.scrollTop = 0
this.scrollArea.innerHTML += this.scrollArea.innerHTML
if (this.scrollData.length) {
this.timeObj = setTimeout(this.scrollStart, this.delay)
}
})
},
scrollStart() {
this.timer = setInterval(this.scroll, this.speed)
this.scrollArea.scrollTop++
},
scroll() {
if (this.scrollArea.scrollTop % this.contentHeight === 0) {
clearInterval(this.timer)
clearTimeout(this.timeObj)
this.timeObj = setTimeout(this.scrollStart, this.delay)
} else {
this.scrollArea.scrollTop++
if (this.scrollArea.scrollTop >= this.scrollArea.scrollHeight / 2) {
this.scrollArea.scrollTop = 0
}
}
}
}
}
</script>
<style lang="stylus" scoped>
.scroll-temp {
overflow: hidden;
height: 44px;
border-radius: 4px;
margin: 0 20px;
padding: 12px 16px;
background: #fff;
font-size: 14px;
color: #1d76f1;
box-shadow: 0 0 6px rgba(29,118,241,0.5) inset;
white-space: nowrap;
display: flex;
.scroll-img {
display: inline-block;
width: 16px;
height: 16px;
margin: 2px 10px 0 0;
}
.scroll-box, .scroll-content{
height: 44px;
overflow: hidden;
}
}
</style>
2、 文字走马灯左右滚动(css)
html
:
<div class="td-style">
<span class="td-text">内容1内容1内容1内容1内容1内容1</span>
</div>
css
:
<style>
.td-style {
width: 160px;
height: 72px;
overflow: hidden;
white-space: nowrap;
font-size: 26px;
line-height: 72px;
}
.td-text {
display: inline-block;
white-space: nowrap;
animation: scrollLeft 3s linear infinite;
}
.td-text:hover {
animation-play-state: paused;
}
@keyframes scrollLeft {
0% {
transform: translateX(0);
}
100% {
transform: translateX(calc(-90% - 0px));
}
}
</style>