elementUI Table組件實(shí)現(xiàn)表頭吸頂效果(示例代碼)
需求描述
當(dāng) table 內(nèi)容過多的時(shí)候,頁面上滑滾動(dòng),表頭的信息也會(huì)隨著被遮擋,無法將表頭信息和表格內(nèi)容對(duì)應(yīng)起來,需要進(jìn)行表頭吸頂
開始編碼??
環(huán)境:vue2.6+、element UI
step1: 給el-table__header-wrapper
加上樣式
//style/sticky-table-header.scss .el-table[is-sticky] { overflow: initial; --sticky-top: 0px; --stick-zIndex: 5; .el-table__header-wrapper{ position: sticky; top: var(--sticky-top); z-index: var(--stick-zIndex); } .el-table__fixed, .el-table__fixed-right{ overflow: visible; z-index: var(--stick-zIndex); .el-table__fixed-header-wrapper { position: sticky; top: var(--sticky-top); width: 100%; overflow: hidden; z-index: var(--stick-zIndex); } .el-table__fixed-body-wrapper { width: 100%; overflow: hidden; } } .el-table__fixed-right { .el-table__fixed-header-wrapper { display: flex; justify-content: flex-end; } .el-table__fixed-body-wrapper { display: flex; justify-content: flex-end; } } &.el-table--border::after{ z-index: var(--stick-zIndex); } } .el-table__fixed { --box-shadow: 10px 0 10px -10px rgba(0, 0, 0, 0.12); } .el-table__fixed-right { --box-shadow: -10px 0 10px -10px rgba(0, 0, 0, 0.12); } .el-table__fixed, .el-table__fixed-right { box-shadow: var(--box-shadow); }
step2: 注冊(cè)指令 directives/sticky-header.js
import '@/styles/sticky-table-header.scss' export default { bind(el, binding) { el.setAttribute('is-sticky', true) updateStickyTop(el, binding) }, update(el, binding) { updateStickyTop(el, binding) } } const updateStickyTop = (el, binding) => { const { value, oldValue } = binding if (value === oldValue) return const top = Number(value) if (!isNaN(top)) { el.style.setProperty('--sticky-top', `${top}px`) } }
step3: main.js 引入
import StickyTableHeader from './directives/sticky-header' Vue.directive('sticky-table-header', StickyTableHeader)
step4: table.vue
<template> <div class="wrapper"> <h3>純CSS表格吸頂</h3> <el-radio-group v-model="mode" aria-hidden="true" class="options"> <el-radio label="normal">正常模式</el-radio> <el-radio label="fixedLeft">固定左邊列</el-radio> <el-radio label="fixedRight">固定右邊列</el-radio> <el-radio label="fixedLeftRight">固定左右列</el-radio> </el-radio-group> <el-table v-sticky-table-header="0" border :data="tableData" > <el-table-column label="日期" prop="date" min-width="150" :fixed="fixedLeft" /> <el-table-column label="姓名" prop="name" width="120" /> <el-table-column label="省份" prop="province" width="120" /> <el-table-column label="市區(qū)" prop="city" width="120" /> <el-table-column label="地址" prop="address" width="300" /> <el-table-column label="郵編" prop="zip" min-width="120" :fixed="fixedRight" /> </el-table> </div> </template> <script> export default { name: 'CSSFixedTopTable', components: {}, data() { const tableData = new Array(100).fill(0).map((_, index) => { return { date: '2016-05-03', name: '王小虎', province: '上海', city: '普陀區(qū)', address: `上海市普陀區(qū)金沙江路 ${1 + index} 弄`, zip: 2000001 + index } }) return { tableData, mode: 'normal' } }, computed: { fixedLeft() { return /left/i.test(this.mode) ? 'left' : null }, fixedRight() { return /right/i.test(this.mode) ? 'right' : null } }, methods: {} } </script> <style lang="scss" scoped> .wrapper { width: 800px; margin: 0 auto; .options { width: 100%; margin: 30px 0; text-align: left; } } </style>
??????父元素不要有 overflow: hidden;會(huì)失效
step5: 效果呈現(xiàn)??
到此這篇關(guān)于elementUI Table組件實(shí)現(xiàn)表頭吸頂效果的文章就介紹到這了,更多相關(guān)elementUI Table組件表頭吸頂內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中數(shù)據(jù)綁定值(字符串拼接)的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了vue中數(shù)據(jù)綁定值(字符串拼接)的幾種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析
這篇文章主要介紹了Vue組件通信$attrs、$listeners實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09vue實(shí)現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例
這篇文章主要介紹了vue實(shí)現(xiàn)簡易圖片左右旋轉(zhuǎn),上一張,下一張組件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue?設(shè)置圖片不轉(zhuǎn)為base64的方式
這篇文章主要介紹了Vue實(shí)現(xiàn)設(shè)置圖片不轉(zhuǎn)為base64的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02vue 如何實(shí)現(xiàn)配置@絕對(duì)路徑
這篇文章主要介紹了vue 如何實(shí)現(xiàn)配置@絕對(duì)路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue使用Echarts實(shí)現(xiàn)排行榜效果
這篇文章主要為大家詳細(xì)介紹了Vue使用Echarts實(shí)現(xiàn)排行榜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03