Vue3 简单的实现文字无限滚动
vue
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
项目地址:https://gitcode.com/gh_mirrors/vu/vue
免费下载资源
·
需求:公告通知的需求-公告可以自己垂直无限滚动
效果图
<template>
<div class="announcement-notice" v-if="true">
<div class="main-title">
<img src="../../../../public/images/notice.png" class="title-logo" />
<span style="font-size: 16px">公告通知:</span>
</div>
<div class="loop-div">
<div
class="test-block"
id="test-block"
:style="{
transform: `translateY(${-state.scrollValue}px)`,
transition: state.scrollValue === 0 ? `none 0s` : `all 1.5s ease-in-out`
}"
v-for="(item, index) in state.testList"
@mouseover="mouseOver"
@mouseout="mouseOut"
:key="index"
@click="noticeCLick(item)"
>
{{ item }}
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
let state = reactive({
stop: false,
scrollValue: 0,
testList: [
'777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777',
'6666'
]
})
const noticeCLick = (item) => {
console.log(item)
router.push({
path: '/system/notice-manage',
query: {
id: '666'
}
})
}
function scrollText() {
if (!state.stop) {
if (state.scrollValue >= (state.testList.length - 1) * 30) {
state.scrollValue = 0
} else {
state.scrollValue += 30
}
if (state.scrollValue === 0) {
delayedScrolling(100)
} else {
delayedScrolling(3000)
}
} else {
delayedScrolling(3000)
}
}
const delayedScrolling = (timeout = 3000) => {
setTimeout(() => {
scrollText()
}, timeout)
}
const mouseOver = () => {
state.stop = true
}
const mouseOut = () => {
state.stop = false
}
onMounted(() => {
const params = state.testList[0]
if (state.testList.length > 1) {
state.testList.push(params)
}
delayedScrolling(3000)
})
</script>
<style lang="less" scoped>
.announcement-notice {
display: flex;
height: 30px;
line-height: 30px;
.main-title {
.title-logo {
height: 12px;
margin-bottom: 4px;
margin-right: 6px;
}
}
.loop-div {
flex: 1;
background: #f0f0f0;
height: 30px;
overflow: hidden;
.test-block {
height: 100%;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
</style>
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5
* chore: fix link broken
Signed-off-by: snoppy <michaleli@foxmail.com>
* Update packages/template-compiler/README.md [skip ci]
---------
Signed-off-by: snoppy <michaleli@foxmail.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891
Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)