Element UI 表格某行突出悬浮效果

Element UI 表格某行突出悬浮效果

代码

html 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">

<title>Element UI 2 浮层高亮行</title>

<!-- Element UI 2 CDN -->
<link rel="stylesheet"
href="https://unpkg.com/element-ui@2.15.14/lib/theme-chalk/index.css">

<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui@2.15.14/lib/index.js"></script>

<style>

body {
    padding: 40px;
    background: #f5f7fa;
}

/* 容器 */
.wrapper {
    position: relative;
    width: 700px;
    margin: auto;
    padding-left: 13px;
}

/* 浮层 */
.floating-row {

    position: absolute;

    left: 0;
    right: 0;

    height: 48px;

    display: flex;

    align-items: center;

    background: linear-gradient(
        to right,
        #cfeeff,
        #e8f7ff
    );

    border: 2px solid #66b1ff;

    border-radius: 6px;

    box-shadow:
        0 4px 12px rgba(0,0,0,0.15);

    font-weight: bold;

    z-index: 999;

    pointer-events: none;

}

/* 星标 */
.star {

    position: absolute;

    left: 16px;

    font-size: 18px;

    color: #409EFF;

}

/* 模拟列对齐 */
.floating-cell {

    flex: 1;

    text-align: center;

}

</style>

</head>

<body>

<div id="app">

<div class="wrapper">

<el-table
    ref="table"
    :data="tableData"
    border
    height="400"
    style="width: 100%">

    <el-table-column
        prop="rank"
        label="年达成率排名">
    </el-table-column>

    <el-table-column
        prop="org"
        label="机构">
    </el-table-column>

    <el-table-column
        prop="rate"
        label="年达成率">
    </el-table-column>

</el-table>

<!-- 浮层 -->
<div
    v-show="floatingTop !== null"
    class="floating-row"
    :style="{ top: floatingTop + 'px' }">

    <span class="star">★</span>

    <div class="floating-cell">
        {{ currentRow.rank }}
    </div>

    <div class="floating-cell">
        {{ currentRow.org }}
    </div>

    <div class="floating-cell">
        {{ currentRow.rate }}
    </div>

</div>

</div>

</div>

<script>

new Vue({

    el: "#app",

    data() {

        return {

            targetIndex: 5,

            floatingTop: null,

            currentRow: {},

            tableData: []

        };

    },

    mounted() {

        // 生成数据
        for (let i = 1; i <= 20; i++) {

            this.tableData.push({

                rank: i,

                org: "北京",

                rate: "125.25%"

            });

        }

        this.$nextTick(() => {

            this.updateFloating();

            // 监听滚动
            const body =
                this.$refs.table.$el.querySelector(
                    ".el-table__body-wrapper"
                );

            body.addEventListener(
                "scroll",
                this.updateFloating
            );

            window.addEventListener(
                "resize",
                this.updateFloating
            );

        });

    },

    methods: {

        updateFloating() {

            const table =
                this.$refs.table.$el;

            const rows =
                table.querySelectorAll(
                    ".el-table__body tbody tr"
                );

            const target =
                rows[this.targetIndex];

            if (!target) return;

            const wrapperRect =
                table.getBoundingClientRect();

            const rowRect =
                target.getBoundingClientRect();

            this.floatingTop =
                rowRect.top -
                wrapperRect.top +
                table.scrollTop;

            this.currentRow =
                this.tableData[
                    this.targetIndex
                ];

        }

    }

});

</script>

</body>
</html>
相关推荐
zttbee10 分钟前
echarts图表tooltip色块失效
前端·javascript·echarts
excel2 小时前
IndexNow:让搜索引擎更快发现网站更新的主动通知协议
前端·后端·架构
子兮曰2 小时前
CSS 复现 23 种设计风格:从拟物到液态玻璃的成本阶梯与兜底写法【实战】
前端·css·视觉设计
虚无境2 小时前
如何用AI帮我写出实用的插件
前端·javascript·ai编程
柳杉3 小时前
从数字化到数智化:3D 可视化大屏的下一个五年
前端·ai编程·数据可视化
濮水大叔3 小时前
函数式与对象式:前端架构的两种成本曲线
vue.js·react.js·函数式编程
zimoyin3 小时前
在 C# 里嵌入 JavaScript:用 Jint 做可热更新的自动化脚本(从入门到踩坑)
javascript·c#·自动化
风骏时光牛马4 小时前
从零到实战:AI全栈学习进阶路线
前端
FRing034 小时前
JavaScript入门到精通二十六(变量提升、函数剩余参数、展开运算符、ES6箭头函数)
开发语言·前端·javascript·es6·箭头函数
用户360055579004 小时前
Kestrel_VLLM - 15 · decode 优化四连击:19ms → 8ms
前端