英文:
Sticky area in single row and column in css grid
问题
我遇到了一个问题,可能只有CSS网格中的一个区域可以粘性定位。
在代码中的外观如下:
.wrapper {
display: grid;
gap: 32;
grid-template-columns: 1fr 478px;
grid-template-areas:
'Images Details'
'Tabs Tabs';
}
.child1 {
grid-area: Images;
}
.child2 {
align-self: flex-start;
grid-area: Details;
position: sticky;
top: 0;
}
.child3 {
grid-area: Tabs;
}
因此,Images
部分比 Details
部分要高得多。我想要实现的是,当向下滚动时,使 Details
粘性定位,但一旦到达 Images
底部,它应该停止向下移动。目前,区域 Images
会重叠到 Tabs
,因为它对包装器进行了粘性定位。
有什么方法可以解决这个问题吗?
演示
https://stackblitz.com/edit/web-platform-a8znlr?file=styles.css
英文:
I am facing the problem with possibility to make only one area sticky in the CSS grid.
How it looks in the code:
.wrapper {
display: grid;
gap: 32;
grid-template-columns: 1fr 478px;
grid-template-areas:
'Images Details'
'Tabs Tabs';
}
.child1 {
grid-area: Images;
}
.child2 {
align-self: flex-start;
grid-area: Details;
position: sticky;
top: 0;
}
.child3 {
grid-area: Tabs;
}
So the section Images
is much bigger (bigger height) than Details
and what I want to achieve is to make Details
sticky when scrolling down, but once I reach the bottom of Images
it should stop moving down. For now area Images
overlaps Tabs
because it is sticky for the wrapper.
Any idea how to fix it?
Demo
https://stackblitz.com/edit/web-platform-a8znlr?file=styles.css
答案1
得分: 1
以下是翻译好的内容:
所请求的解决方案无法通过纯CSS和HTML实现,但可以使用一些JavaScript来完成:
window.addEventListener('DOMContentLoaded', () => {
const images = document.querySelector('.child1');
const details = document.querySelector('.child2');
const handleScroll = () => {
const imagesRect = images.getBoundingClientRect();
const detailsHeight = details.offsetHeight;
if (imagesRect.bottom - detailsHeight < 0) {
details.style.position = 'absolute';
details.style.top = `${images.offsetHeight - detailsHeight}px`;
} else {
details.style.position = 'sticky';
details.style.top = '0';
}
};
window.addEventListener('scroll', handleScroll);
});
英文:
The solution you request is not achievable with pure css and html, however you can do this with some javascript:
window.addEventListener('DOMContentLoaded', () => {
const images = document.querySelector('.child1');
const details = document.querySelector('.child2');
const handleScroll = () => {
const imagesRect = images.getBoundingClientRect();
const detailsHeight = details.offsetHeight;
if (imagesRect.bottom - detailsHeight < 0) {
details.style.position = 'absolute';
details.style.top = `${images.offsetHeight - detailsHeight}px`;
} else {
details.style.position = 'sticky';
details.style.top = '0';
}
};
window.addEventListener('scroll', handleScroll);
});
答案2
得分: 0
我认为这会解决你的问题。
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-css -->
.wrapper {
display: grid;
gap: 32px;
grid-template-columns: 1fr 478px;
grid-template-areas:
'Images Details'
'Tabs Tabs';
}
.child1 {
grid-area: Images;
display: flex;
flex-wrap: wrap;
}
.child1 img {
width: 350px;
height: 200px;
margin-bottom: 16px;
}
.child2 {
grid-area: Details;
position: sticky;
top: 0;
}
.child3 {
grid-area: Tabs;
}
.child2 img {
width: 350px;
height: 200px;
margin-bottom: 16px;
}
<!-- 结束代码片段 -->
英文:
I think this will fix your problem.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.wrapper {
display: grid;
gap: 32px;
grid-template-columns: 1fr 478px;
grid-template-areas:
'Images Details'
'Tabs Tabs';
}
.child1 {
grid-area: Images;
display: flex;
flex-wrap: wrap;
}
.child1 img {
width: 350px;
height: 200px;
margin-bottom: 16px;
}
.child2 {
grid-area: Details;
position: sticky;
top: 0;
}
.child3 {
grid-area: Tabs;
}
.child2 img {
width: 350px;
height: 200px;
margin-bottom: 16px;
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论