vue實現(xiàn)消息列表向上無縫滾動效果
一、背景
最近產品有個需求:后臺系統(tǒng)的未讀消息,在用戶登陸后,將未讀信息在右側浮窗無縫滾動;不關閉時,間隔10秒逐級消失逐級上浮,每次只展示一條消息;支持手動關閉浮窗;支持單擊浮窗打開相應消息。
二、需要實現(xiàn)的效果

三、實現(xiàn)思路
根據(jù)需求,準備采用CSS(transition)結合JS(setTimeout)的方案進行實現(xiàn),因為功能比較簡單,所以沒有使用第三方插件,也方便自定義樣式。
四、實現(xiàn)方法
- 首先我們先實現(xiàn)樣式,看下
html的實現(xiàn)代碼:

代碼如下圖:
<template>
<div :class="{anim:animate}" @mouseenter="stop()" @mouseleave="up()" class="unreadMsg">
<div class="news_name" v-for="(item, index) in newsList" :key="item.id" @click="handleDetail(item)">
<div class="content">
<div class="title">{{ item.title }}</div>
<div class="des">{{ item.description }}</div>
</div>
<span @click="handleDelete(item, index)" class="close">
<a-icon type="close" style="cursor: pointer" />
</span>
</div>
</div>
</template>
注:我的項目是
ant-vue框架,上面代碼中用到一個關閉icon(a-icon)標簽,可作替換。
- 接下來是
css的實現(xiàn)代碼:
<style lang="less" scoped>
.unreadMsg {
max-height: 132px;
overflow: hidden;
position: absolute;
right: 0;
top: 0;
bottom: auto;
margin-inline-end: 24px;
.news_name {
line-height: 30px;
transition: top 0.5s;
transition-delay: 10s;
position: relative;
.content {
position: relative;
width: 384px;
margin-bottom: 40px;
margin-inline-start: auto;
padding: 20px 30px;
overflow: hidden;
word-wrap: break-word;
background: #fff;
border-radius: 8px;
.title {
padding-right: 12px;
margin-bottom: 8px;
color: rgba(0, 0, 0, 0.88);
font-size: 16px;
line-height: 1.5;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.des {
font-size: 14px;
cursor: pointer;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
}
.close {
position: absolute;
top: 20px;
inset-inline-end: 24px;
color: rgba(0, 0, 0, 0.45);
outline: none;
width: 22px;
height: 22px;
border-radius: 4px;
transition: background-color 0.2s, color 0.2s;
display: flex;
align-items: center;
justify-content: center;
}
}
}
.anim {
transition: all 0.5s;
margin-top: -110px;
}
</style>
- 最后通過
js實現(xiàn)各項功能:
- 首先實現(xiàn) 每條消息間隔10秒逐級消失逐級上浮 功能:
// 滾動動畫
scrollUp() {
// 每個消息展示10s
this.timer = setInterval(() => {
this.animate = true // 向上滾動的時候需要添加動畫
setTimeout(() => {
this.newsList.push(this.newsList[0])// 將數(shù)組的第一個元素添加到數(shù)組最后一個
this.newsList.shift() // 刪除數(shù)組的第一個元素
this.animate = false
}, 500)
}, 10000)
},
需要注意到是,這個滾動動畫的方法,需要在mounted生命周期中執(zhí)行,在created生命周期中請求后端接口,獲取未讀消息的list數(shù)組。
另:此處的10s可根據(jù)自己項目需求進行調整。
- 實現(xiàn) 單擊浮窗打開相應消息 功能:
handleDetail(item) {
this.$router.push({ name: 'noticeDetailService', params: { id: item.id } })
},
這里只要點擊對應的消息,跳轉到這個消息具體的詳情頁即可。因為每個消息都會有自己對應的ID,不用多說,都懂。
- 實現(xiàn) 手動關閉當前的消息 功能:
handleDelete(item, index) {
this.newsList.splice(index, 1) // 刪除數(shù)組的當前元素
},
此處主要考慮的問題是,有的消息用戶不想點開看詳情,也不想看它在輪播,想直接關掉。只需要刪除數(shù)組中的當前元素即可。
最后記得在
beforeDestroy生命周期中清除計時器clearInterval。下面將
js的全部代碼附上:

<script>
import { noticeSearch } from '../api/index.js'
export default {
name: 'unreadMsg',
data() {
return {
timer: null,
animate: false,
newsList: []
}
},
created() {
this.noticeSearch()
},
mounted() {
this.scrollUp() // 開啟滾動效果
},
beforeDestroy() {
this.stop()
},
methods: {
async noticeSearch () {
const data = await noticeSearch({ size: -1 })
this.newsList = data.list
},
// 查看公告詳情
handleDetail(item) {
this.$router.push({ name: 'noticeDetailService', params: { id: item.id } })
},
handleDelete(item, index) {
this.newsList.splice(index, 1) // 刪除數(shù)組的當前元素
},
// 滾動動畫
scrollUp() {
// 每個消息展示10s
this.timer = setInterval(() => {
this.animate = true // 向上滾動的時候需要添加動畫
setTimeout(() => {
this.newsList.push(this.newsList[0])// 將數(shù)組的第一個元素添加到數(shù)組最后一個
this.newsList.shift() // 刪除數(shù)組的第一個元素
this.animate = false
}, 500)
}, 10000)
},
// 鼠標移上去停止
stop() {
clearInterval(this.timer)
},
// 鼠標離開繼續(xù)
up() {
this.scrollUp()
}
}
}
</script>
五、總結
功能雖簡單,需要注意的點也很多,要記得在對應的生命周期做對應的操作。用到定時器的地方也要記得進行清除。
以上就是vue實現(xiàn)消息列表向上無縫滾動的詳細內容,更多關于vue列表滾動的資料請關注腳本之家其它相關文章!
相關文章
Vue實現(xiàn)表格數(shù)據(jù)的增刪改查的示例代碼
Vue是一個用于構建用戶界面的JavaScript框架,在Vue中可以通過使用Vue組件來實現(xiàn)對表格的增刪改查操作,下面將介紹一些基礎的Vue組件和技術來實現(xiàn)對表格的增刪改查操作,需要的朋友可以參考下2024-08-08

