在CSS网格中创建单行和单列的粘性区域。

huangapple go评论85阅读模式
英文:

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(&#39;DOMContentLoaded&#39;, () =&gt; {
  const images = document.querySelector(&#39;.child1&#39;);
  const details = document.querySelector(&#39;.child2&#39;);

  const handleScroll = () =&gt; {
    const imagesRect = images.getBoundingClientRect();
    const detailsHeight = details.offsetHeight;

    if (imagesRect.bottom - detailsHeight &lt; 0) {
      details.style.position = &#39;absolute&#39;;
      details.style.top = `${images.offsetHeight - detailsHeight}px`;
    } else {
      details.style.position = &#39;sticky&#39;;
      details.style.top = &#39;0&#39;;
    }
  };

  window.addEventListener(&#39;scroll&#39;, 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:
    &#39;Images Details&#39;
    &#39;Tabs Tabs&#39;;
}

.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 -->

huangapple
  • 本文由 发表于 2023年6月27日 18:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定