gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:12:37 24 4
gpt4 key购买 nike

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

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

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

我也试过

th {
position:sticky;
top: 0;
background-color: white;
}

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

任何人都可以建议我一些关于如何使用 Vuetify 数据表实现相同内容的想法或代码笔

最佳答案

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

编辑:这是可与 vuetify v-simple-table

一起使用的 vue 指令

使用:

<v-simple-table v-simple-table-sticky></v-simple-table>

指令:

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));
}
});

关于css - vuetify 数据表中滚动的粘性标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61474945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com