vue實現消息列表向上無縫滾動效果
一、背景
最近產品有個需求:后臺系統的未讀消息,在用戶登陸后,將未讀信息在右側浮窗無縫滾動;不關閉時,間隔10秒逐級消失逐級上浮,每次只展示一條消息;支持手動關閉浮窗;支持單擊浮窗打開相應消息。
二、需要實現的效果
三、實現思路
根據需求,準備采用CSS(transition)
結合JS(setTimeout)
的方案進行實現,因為功能比較簡單,所以沒有使用第三方插件,也方便自定義樣式。
四、實現方法
- 首先我們先實現樣式,看下
html
的實現代碼:
代碼如下圖:
<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
的實現代碼:
<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
實現各項功能:
- 首先實現 每條消息間隔10秒逐級消失逐級上浮 功能:
// 滾動動畫 scrollUp() { // 每個消息展示10s this.timer = setInterval(() => { this.animate = true // 向上滾動的時候需要添加動畫 setTimeout(() => { this.newsList.push(this.newsList[0])// 將數組的第一個元素添加到數組最后一個 this.newsList.shift() // 刪除數組的第一個元素 this.animate = false }, 500) }, 10000) },
需要注意到是,這個滾動動畫的方法,需要在mounted
生命周期中執(zhí)行,在created
生命周期中請求后端接口,獲取未讀消息的list
數組。
另:此處的10s
可根據自己項目需求進行調整。
- 實現 單擊浮窗打開相應消息 功能:
handleDetail(item) { this.$router.push({ name: 'noticeDetailService', params: { id: item.id } }) },
這里只要點擊對應的消息,跳轉到這個消息具體的詳情頁即可。因為每個消息都會有自己對應的ID,不用多說,都懂。
- 實現 手動關閉當前的消息 功能:
handleDelete(item, index) { this.newsList.splice(index, 1) // 刪除數組的當前元素 },
此處主要考慮的問題是,有的消息用戶不想點開看詳情,也不想看它在輪播,想直接關掉。只需要刪除數組中的當前元素即可。
最后記得在
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) // 刪除數組的當前元素 }, // 滾動動畫 scrollUp() { // 每個消息展示10s this.timer = setInterval(() => { this.animate = true // 向上滾動的時候需要添加動畫 setTimeout(() => { this.newsList.push(this.newsList[0])// 將數組的第一個元素添加到數組最后一個 this.newsList.shift() // 刪除數組的第一個元素 this.animate = false }, 500) }, 10000) }, // 鼠標移上去停止 stop() { clearInterval(this.timer) }, // 鼠標離開繼續(xù) up() { this.scrollUp() } } } </script>
五、總結
功能雖簡單,需要注意的點也很多,要記得在對應的生命周期做對應的操作。用到定時器的地方也要記得進行清除。
以上就是vue實現消息列表向上無縫滾動的詳細內容,更多關于vue列表滾動的資料請關注腳本之家其它相關文章!
相關文章
Vue?Mock.js介紹和使用與首頁導航欄左側菜單搭建過程
Mock.js是一個模擬數據的生成器,用來幫助前端調試開發(fā)、進行前后端的原型分離以及用來提高自動化測試效率,這篇文章主要介紹了Vue?Mock.js介紹和使用與首頁導航欄左側菜單搭建,需要的朋友可以參考下2023-09-09關于vue 的slot分發(fā)內容 (多個分發(fā))
這篇文章主要介紹了關于vue 的slot分發(fā)內容 (多個分發(fā)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03vue+echarts實現動態(tài)繪制圖表及異步加載數據的方法
vue寫的后臺管理,需要將表格數據繪制成圖表(折線圖,柱狀圖),圖表數據都是通過接口請求回來的。這篇文章主要介紹了vue+echarts 動態(tài)繪制圖表及異步加載數據的相關知識,需要的朋友可以參考下2018-10-10vue執(zhí)行配置選項npm?run?serve的本質圖文詳解
本地開發(fā)一般通過執(zhí)行npm run serve命令來啟動項目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關于vue執(zhí)行配置選項npm?run?serve的本質的相關資料,需要的朋友可以參考下2023-05-05