【问题标题】:Make a div scollable on overflow-y but at the same time overflow-x should be completely visible使 div 在 overflow-y 上可滚动,但同时 overflow-x 应该完全可见
【发布时间】:2021-11-14 19:16:57
【问题描述】:

让我首先为我的问题提供stackblitz url,以防我所描述的内容令人困惑。
我正在开发一个反应打字稿项目并根据提供的线框构建 UI。我有一个固定宽度的垂直容器,其中包含一张显示在另一张下方的卡片。当我们将鼠标悬停在其中一张卡片上时,卡片的宽度应该像这样扩展:

这部分的代码是:
index.ts

class App extends Component<AppProps, AppState> {
  constructor(props) {
    super(props);
    this.state = {
      payload: [
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'AB' },
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'CD' },
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'EF' },
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'GH' },
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'IJ' },
        { text: 'sdsdgfdghgf', heading: 'Heading', intitials: 'KL' },
      ],
    };
  }

  render() {
    return (
      <div className="container">
        <div className="container-light">
          {this.state.payload.map((item: any) => (
            <div className="card"></div>
          ))}
        </div>
      </div>
    );
  }
}

styles.css

.container {
  display: flex;
  position: absolute;
  z-index: 1;
}

.container-light {
  background-color: #eaeaea;
  width: 100px;
}

.card {
  height: 60px;
  border-radius: 4px;
  flex-shrink: 0;
  justify-content: center;
  z-index: 2;
  border: 1px solid #5a5a5a;
  margin: 0px 10px 5px;
  background-color: #eaeaea;
}

.card:hover {
  width: 320px;
  border: 1px solid #1473e6;
}

然后,如果我们降低浏览器窗口的高度,我需要我的外部容器是可伸缩的,同时保持悬停行为。但是一旦我将overflow-y: auto 添加到可滚动容器中,它也会隐藏我的水平溢出数据。看起来像这样:

为了实现滚动,我将overflow-y: auto;height: 300px; 添加到.container-light,即我的可滚动div。

我有一种半成形的 CSS 解决方案,我在悬停时制作 div position: fixed; 并修改其兄弟的边距,就像这样:

.card:hover {
  position: fixed;
  top: 100px; /* have to calculate this dynamically somehow */
}
.card:hover + .card {
  margin-top: 66px; 
}

我被困在这里了。如果我弄清楚我的技巧,那么我想我可以暂时保留它。但是,如果我遗漏了一些非常明显的东西,或者如果有人有不同的解决方案,我将不胜感激。

【问题讨论】:

    标签: css reactjs typescript overflow react-typescript


    【解决方案1】:

    试试这个:

    .container {
      min-width:320px
    }
    

    【讨论】:

    • 我需要固定这些容器的宽度。只有卡片宽度必须扩大
    • 这里的问题是滚动条。您究竟需要滚动条出现在哪里?如果您按照我的建议进行操作,则滚动条将距左边框 320 像素。如果您希望它出现在距左边框 100px 处,使悬停的卡片出现在其上方,您可以使用:.container-light, container{ overflow-x : visible }
    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    • 滚动条应该在 100px 宽的 .container-light div 上。 .container div 只是一个包装器。
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2019-01-02
    • 1970-01-01
    • 2015-03-25
    • 2014-10-07
    • 2020-07-11
    相关资源
    最近更新 更多