vue自定義table表如何實現(xiàn)內(nèi)容上下循環(huán)滾動
需求
對一段table表數(shù)據(jù)進行異常與正常類型的分類處理;
異常數(shù)據(jù)固定于table表內(nèi)上方顯示;
正常數(shù)據(jù)則在異常數(shù)據(jù)下方實現(xiàn)循環(huán)滾動效果;
(若正常數(shù)據(jù)高度未溢出table表高度,正常顯示即可,無需滾動)
效果圖展示

代碼核心部分
<!-- html 部分-->
<!--
scrollWrap: 滾動區(qū)域;
scrollWrapHeight:table總高度 - header高度 - 異常數(shù)據(jù)高度
-->
<div
class="scrollWrap"
:style="{
height: scrollWrapHeight + 'px',
overflowY: 'hidden',
}"
>
<!--
scrollNum:滾動時,復(fù)制正常table數(shù)據(jù)一份,用于上下循環(huán)滾動無縫銜接
-->
<div
:class="scrollNum > 1 ? 'scroll' : ''"
:style="{
animationDuration: time + 's',
}"
v-for="(a, index) in scrollNum"
:key="index"
>
<div
class="table-bodySuccess"
v-for="(item, index) in successData"
:key="index"
:style="{
background: successBgColor(index),
}"
>
<div class="table-body-item-title">
{{ item.name }}
</div>
<div class="table-body-item-title">
<div class="circle"></div>
</div>
<div class="table-body-item-title">{{ item.tampNum }}℃</div>
<div class="table-body-item-title">{{ item.eleNum }}A</div>
</div>
</div>
</div>
<!-- css 部分-->
.scrollWrap::-webkit-scrollbar {
width: 0 !important;
}
.scroll {
animation: scrollData 10s infinite linear;
}
@keyframes scrollData {
from {
transform: translateY(0px);
}
to {
transform: translateY(-100%);
}
}完整代碼展示
<template>
<div class="left">
<div class="table">
<div class="table-header">
<div class="table-header-item-title">名稱</div>
<div class="table-header-item-title">狀態(tài)</div>
<div class="table-header-item-title">數(shù)據(jù)標(biāo)題1</div>
<div class="table-header-item-title">數(shù)據(jù)標(biāo)題2</div>
</div>
<div
class="table-bodyError"
v-for="(item, index) in errData"
:key="index"
:style="{ background: errBgColor(index) }"
>
<div class="table-body-item-title">
{{ item.name }}
</div>
<div class="table-body-item-title">
<div class="circle"></div>
</div>
<div class="table-body-item-title">
{{ item.tampNum }}
</div>
<div class="table-body-item-title">
{{ item.eleNum }}
</div>
</div>
<div
class="scrollWrap"
:style="{
height: scrollWrapHeight + 'px',
overflowY: 'hidden',
}"
>
<div
:class="scrollNum > 1 ? 'scroll' : ''"
:style="{
animationDuration: time + 's',
}"
v-for="(a, index) in scrollNum"
:key="index"
>
<div
class="table-bodySuccess"
v-for="(item, index) in successData"
:key="index"
:style="{
background: successBgColor(index),
}"
>
<div class="table-body-item-title">
{{ item.name }}
</div>
<div class="table-body-item-title">
<div class="circle"></div>
</div>
<div class="table-body-item-title">{{ item.tampNum }}℃</div>
<div class="table-body-item-title">{{ item.eleNum }}A</div>
</div>
</div>
</div>
</div>
</div>
</template><script>
export default {
name: 'test',
data() {
return {
// 調(diào)節(jié)滾動速率
time: 15,
errData: [
{ name: 11, status: 0, tampNum: 10, eleNum: 15 },
{ name: 22, status: 0, tampNum: 10, eleNum: 15 },
{ name: 33, status: 0, tampNum: 10, eleNum: 15 },
],
successData: [
{ name: 'aaa', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'bbb', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'ccc', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'ddd', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'eee', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'fff', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'ggg', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'hhh', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'iii', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'jjj', status: 1, tampNum: 10, eleNum: 15 },
{ name: 'kkk', status: 1, tampNum: 10, eleNum: 15 },
],
}
},
computed: {
errBgColor: function () {
return function (index) {
if (index % 2 === 0) {
return '#e8f7ff'
} else {
return '#ffffff'
}
}
},
successBgColor: function () {
return function (index) {
if (this.errData.length % 2 === 0) {
if (index % 2 === 0) {
return '#e8f7ff'
} else {
return '#ffffff'
}
} else {
if (index % 2 === 0) {
return '#ffffff'
} else {
return '#e8f7ff'
}
}
}
},
// 滾動層高度
scrollWrapHeight: function () {
// left高度 - table-header高度 - table-bodyError高度 * 個數(shù)
return 600 - 52 - this.errData.length * 52
},
// 滾動層份數(shù),當(dāng)內(nèi)容溢出scrollWrapHeight,復(fù)制兩份,添加滾動動畫
// 否則就一份,不填加滾動動畫
scrollNum: function () {
let successHeight = this.successData.length * 52
if (successHeight > this.scrollWrapHeight) {
return 2
} else {
return 1
}
},
},
}
</script><style lang="less" scoped>
.left {
width: 520px;
height: 600px;
background-color: #fab4b4;
border-bottom: 1px solid red;
position: relative;
.table-header {
width: 100%;
background-color: skyblue;
color: #e1f3ff;
font-size: 16px;
font-weight: 700;
display: flex;
.table-header-item-title {
height: 52px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
}
.table-bodyError,
.table-bodySuccess {
width: 100%;
color: red;
font-size: 16px;
display: flex;
.table-body-item-title {
width: 100%;
height: 52px;
display: flex;
justify-content: center;
align-items: center;
.circle {
width: 12px;
height: 12px;
background: #ea4141;
border-radius: 50%;
}
}
}
.table-bodySuccess {
color: #000;
.table-body-item-title {
.circle {
background: #29b153;
}
}
}
.scrollWrap::-webkit-scrollbar {
width: 0 !important;
}
.scroll {
animation: scrollData 10s infinite linear;
}
@keyframes scrollData {
from {
transform: translateY(0px);
}
to {
transform: translateY(-100%);
}
}
}
</style>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談vue中document.getElementById()拿到的是原值的問題
這篇文章主要介紹了淺談vue中document.getElementById()拿到的是原值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
UNIapp實現(xiàn)局域網(wǎng)內(nèi)在線升級的操作方法
這篇文章主要介紹了UNIapp實現(xiàn)局域網(wǎng)內(nèi)在線升級的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
使用VueRouter的addRoutes方法實現(xiàn)動態(tài)添加用戶的權(quán)限路由
這篇文章主要介紹了使用VueRouter的addRoutes方法實現(xiàn)動態(tài)添加用戶的權(quán)限路由,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
vue獲取v-for異步數(shù)據(jù)dom的解決問題
這篇文章主要介紹了vue獲取v-for異步數(shù)據(jù)dom的解決問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
完美解決vue 項目開發(fā)越久 node_modules包越大的問題
這篇文章主要介紹了vue 項目開發(fā)越久 node_modules包越大的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
基于Vue實現(xiàn)文件上傳的幾種實現(xiàn)方式
文件上傳是web開發(fā)中一個常見的需求,Vue.js作為一款流行的前端框架,也提供了方便的方法來實現(xiàn)文件上傳功能,下面這篇文章主要給大家介紹了關(guān)于基于Vue實現(xiàn)文件上傳的幾種實現(xiàn)方式,需要的朋友可以參考下2024-03-03
vue如何設(shè)置描點跳轉(zhuǎn)到對應(yīng)頁面
這篇文章主要介紹了vue如何設(shè)置描點跳轉(zhuǎn)到對應(yīng)頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

