vuetify 数据表中滚动的粘性标题

cha*_*ans 5 css vue.js vuetify.js

当 vuetify 数据表高度大于窗口高度时,当我们滚动页面时,我希望标题行粘住直到数据表滚动结束

行为应该类似于此 https://output.jsbin.com/zuzuqe/1/

也喜欢他们使用的数据表https://www.worldometers.info/coronavirus/

我也尝试过

th {
    position:sticky;  
    top: 0;
    background-color: white;
}
Run Code Online (Sandbox Code Playgroud)

粘性位置是相对于数据表而不是窗口

任何人都可以建议我一些关于如何使用 Vuetify 数据表实现相同功能的想法或 codepen

Adr*_*oir 1

您可以使用 Vue 组件Float Thead vue 组件来做到这一点

编辑:这是可与 vuetify 一起使用的 vue 指令v-simple-table

使用 :

<v-simple-table v-simple-table-sticky></v-simple-table>
Run Code Online (Sandbox Code Playgroud)

指令:

function stickyScrollHandler(el) {
    return () => {
        const getOffsetTop = function(element) {
            let offsetTop = 0;
            while (element) {
                offsetTop += element.offsetTop;
                element = element.offsetParent;
            }
            return offsetTop;
        }

        const table = el.querySelector("table");
        const tableHeader = el.querySelector(".adx-table_sticky_header");
        let tableHeaderFloat = el.querySelector(".adx-table_sticky--float");

        const pos = getOffsetTop(table) - window.scrollY;

        if (pos < 0) {
            if (!tableHeaderFloat) {
                const clone = tableHeader.cloneNode(true);
                clone.classList.remove('.table_sticky_header');

                tableHeaderFloat = document.createElement('table');
                tableHeaderFloat.appendChild(clone);
                tableHeaderFloat.classList.add("adx-table_sticky--float");
                table.parentNode.appendChild(tableHeaderFloat);

                tableHeader.style.opacity = 0;
            }

            if (Math.abs(pos) < table.offsetHeight - tableHeaderFloat.offsetHeight) {
                tableHeaderFloat.style.position = "absolute";
                tableHeaderFloat.style.top = Math.abs(pos) + "px";
            }
        } else {
            if (tableHeaderFloat) {
                tableHeaderFloat.remove();
            }

            tableHeader.style.opacity = 1;
        }
    }
}

Vue.directive("simple-table-sticky", {
    bind: function(el, binding, vnode) {
        el.querySelector("table thead").classList.add("adx-table_sticky_header");
        el.style.position = "relative"

        window.addEventListener('scroll', stickyScrollHandler(el));
    },
    unbind: function(el) {
        window.removeEventListener('scroll', stickyScrollHandler(el));
    }
});
Run Code Online (Sandbox Code Playgroud)