在父母的一个子元素的水平滚动

时间:2018-01-29 12:30:31

标签: html css scroll overflow

我有一个包含多个部分的页面(请参阅代码段或JSFiddle example)。

用户应该只能在页面上垂直滚动。

现在,除此之外,我想水平滚动浏览一个:项目(不影响页面的其余部分)。这意味着,除了项目部分,其余部分必须保持不动。

我怎样才能实现这种局部水平滚动?

.container {
  width: 500px;
  height: 100%;
  overflow-x: scroll;
  background-color: lightgrey;
  padding: 20px;
}

.projects {
  width: 960px;
}

.project {
  display: inline-block;
  float: left;
  width: 300px;
  margin: 0 20px 20px 0;
  background-color: grey;
}
<div class="container">
    <h1>Page</h1>
    <div class="main">
        <h2>Main</h2>
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
    <div class="projects">
        <h2>Projects</h2>
        <div class="project">
            <p>Project</p>
        </div>
        <div class="project">
            <p>Project</p>
        </div>
        <div class="project">
            <p>Project</p>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

试试这个。

.projects .project-inwrap  {
  width: 100%;
  white-space: nowrap;
  overflow: auto;
}

&#13;
&#13;
.container {
  width: 500px;
  height: 100%;
  overflow-x: scroll;
  background-color: lightgrey;
  padding: 20px;
}

.projects .project-inwrap {
  width: 100%;
  white-space: nowrap;
  overflow: auto;
}

.project {
  display: inline-block;
  width: 300px;
  margin: 0 20px 20px 0;
  background-color: grey;
}
&#13;
<div class="container">
  <h1>Page</h1>
  <div class="main">
    <h2>Main</h2>
    <p>Lorem ipsum dolor sit amet.</p>
  </div>
  <div class="projects">
    <h2>Projects</h2>
    <div class="project-inwrap">
      <div class="project">
        <p>Project</p>
      </div>
      <div class="project">
        <p>Project</p>
      </div>
      <div class="project">
        <p>Project</p>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个

*{
  box-sizing: border-box;
}
body{
  overflow: hidden;
}

.container {
  width: 500px;
  height: 100vh;
  overflow-x: hidden;
  background-color: lightgrey;
  padding: 20px;
}

.projects {
  width: 100%;
  white-space: nowrap;
  overflow: auto;
}

.project {
  display: inline-block;
  width: 300px;
  margin: 0 20px 20px 0;
  background-color: grey;
}
<div class="container">
    <h1>Page</h1>
    <div class="main">
        <h2>Main</h2>
        <p>Lorem ipsum dolor sit amet.</p>
    </div>
    <div class="projects">
        <h2>Projects</h2>
        <div class="project">
            <p>Project</p>
        </div>
        <div class="project">
            <p>Project</p>
        </div>
        <div class="project">
            <p>Project</p>
        </div>
    </div>
</div>