gpt4 book ai didi

html - CSS Grid如何选择行添加悬停效果?

转载 作者:太空狗 更新时间:2023-10-29 14:56:56 25 4
gpt4 key购买 nike

我需要选择一行以在其上添加 hover 效果。我试图用 div 将所有单元格排成一行,但所有标记都被破坏了。有谁知道该怎么做?这可能吗?

在此处查看完整代码:https://codesandbox.io/s/goofy-easley-w5rrg

const TableWrapperUI = styled.div `
display: grid;
box-sizing: border-box;
width: 100%;
border: 1px solid #dbeaf4;
grid-template-columns: repeat(
${props => props.columns && props.columns},
fit-content(400px)
);
justify-items: center;
padding: 5px 0;
justify-content: space-between;
> span {
padding: 5px;
justify-self: left;
:hover {
background: #dbeaf4;
}
}`;

const LineUI = styled.div `
border-bottom: 1px solid #dbeaf4;
width: 100%;
grid-column: 1 / -1;
`;

最佳答案

display: contents;来救援!

有点。

取决于您的 browser support and/or accessibility requirements ,我们可以使用您拥有的一般结构,使用相对较新的 display: contents property 来达到您想要的效果.

描述display: contents有点困难,所以我会指出这个优秀的CSS Tricks article .

要使用它,我们将包装每组 <span>一行中的元素变成<div>display: contents .这使我们能够定位 div:hover > span元素并应用背景颜色。

您的样式还需要进行其他一些小改动,例如制作 <span>元素填充可用宽度。这是一个工作示例:

.parent {
display: grid;
box-sizing: border-box;
width: 100%;
border: 1px solid #dbeaf4;
grid-template-columns: repeat(4, minmax(15%, max-content));
padding: 5px 0;
}

.parent span {
padding: 5px;
border-bottom: 1px solid #dbeaf4;
}

.row {
display: contents;
}

.row:hover span {
background-color: #dbeaf4;
cursor: pointer;
}
<div class="parent">
<div class="row">
<span>1</span>
<span>2</span>
<span>3</span>
<span>Knowledge process outsourcing land the plane yet to be inspired is to become creative, innovative and energized we want this</span>
</div>
<div class="row">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
<div class="row">
<span>We need to socialize the comms with the wider stakeholder community</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
<div class="row">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
<div class="row">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
</div>
</div>

使用 React 和样式化组件

现在我们的 CSS 已经工作了,我们可以把它放回样式组件中。我对您的代码所做的主要更改是使用 <LineUI />组件来包装每一行,以及上面的新 CSS。

const titles = [
"Id",
"Type",
"Name",
"Category",
"Client",
"Date",
"Watched",
"Amount",
"State",
"Delete"
];

const data = [
{
id: 23,
type: "test",
name: "joaaaahnny cageasdasdasd cageasdasdasd cageasdasdasd cageasdasdasd",
category: "selasdasler",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 211,
type: "test",
name: "johnny cage",
category: "seller",
client: "custsdsom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 2222,
type: "test",
name: "johnny cage",
category: "seller",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 2222,
type: "test",
name: "johnny cage",
category: "seller",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 2222,
type: "test",
name: "johnny cage",
category: "seller",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 2222,
type: "test",
name: "johnny cage",
category: "seller",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
},
{
id: 2222,
type: "test",
name: "johnny cage",
category: "seller",
client: "custom",
date: "01-01-2019",
watched: "yes",
amount: 1231,
state: "pending",
delete: "button"
}
];

const TableWrapperUI = styled.div`
display: grid;
box-sizing: border-box;
width: 100%;
border: 1px solid #dbeaf4;
grid-template-columns: repeat(
${props => props.columns && props.columns},
minmax(auto, max-content)
);
padding: 5px;

> * {
padding: 5px;
}
`;

const LineUI = styled.div`
display: contents;

> * {
padding: 5px;
border-bottom: 1px solid #dbeaf4;
}

:hover > * {
background-color: #dbeaf4;
cursor: pointer;
overflow: visible;
}
`;

const Table = ({ children, titles, data }) => {
const [amountColumns, setAmountColumns] = React.useState(0);
React.useEffect(() => {
setAmountColumns(titles.length);
}, []);

const displayData = data => {
return data.map((x, idx) => {
return (
<React.Fragment key={idx}>
<LineUI>
{Object.keys(x).map((value, ids) => (
<span key={ids}>{x[value]}</span>
))}
</LineUI>
</React.Fragment>
);
});
};

const displayTitles = titles => {
return titles.map((title, idx) => {
return <span key={idx}>{title}</span>;
});
};

return (
amountColumns > 0 && (
<TableWrapperUI columns={amountColumns}>
{displayTitles(titles)}
{displayData(data)}
</TableWrapperUI>
)
);
};

function App() {
return (
<div className="App">
<Table columns={10} titles={titles} data={data} />
</div>
);
}

ReactDOM.render(<App />, document.getElementById("root"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/styled-components/4.3.2/styled-components.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>


<div id="root"></div>

关于html - CSS Grid如何选择行添加悬停效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56902735/

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