css 如何使固定元素的内容仅在超出视区高度时才可滚动?

nhjlsmyf  于 2023-03-20  发布在  其他
关注(0)|答案(9)|浏览(355)

我有一个div,它位于网页的左边,包含菜单和导航链接,它没有css中的高度设置,内容决定高度,宽度是固定的,问题是如果内容太多,div会大于窗口的高度,部分内容会看不见。(滚动窗口没有帮助,因为位置是fixed,而div不会滚动。)
我试着设置overflow-y:auto;,但也无济于事,div似乎没有注意到它的一部分在窗口之外。
如果div挂在窗口外,如何使其内容仅在需要时滚动?

gijlo24d

gijlo24d1#

你可能做不到。这里有一个很接近的东西。如果下面有空间,你就不会让内容围绕它流动。

.stuck {
  position: fixed;
  top: 10px;
  left: 10px;
  bottom: 10px;
  width: 180px;
  background-color: #eee;
  overflow-y: scroll;
}

.stuck p {
  margin: 10px;
}

.not-stuck {
  margin-left: 200px;
}
<div class="stuck">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex
    ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
    augue duis dolore te feugait nulla facilisi.</p>
</div>

<div class="not-stuck">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex
  ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue
  duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem.
  Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit
  litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
  ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie
  consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue
  nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam
  processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc
  nobis videntur parum clari, fiant sollemnes in futurum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci
  tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
  dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem
  insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam
  littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum. Lorem ipsum dolor sit amet, consectetuer
  adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
  iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
  Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores
  legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula
  quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</div>

也可以设置高度百分比:
一个二个一个一个

vq8itlhq

vq8itlhq2#

下面是我如何做到这一点。你只需要使用一些聪明的前端开发!!

//start MOBILE NAV
$('#mobnav-btn').click(

  function() {
    $("html").toggleClass("mobile-menu-open");
    $(".xnav-wrapper").delay(500).queue(function(reset_scroll) {
      $(this).scrollTop(0);
      reset_scroll();
    });
  });
//end MOBILE NAV
html {
  left: 0%;
  position: relative;
  overflow-x: hidden;
  transition: all .2s ease;
}

body {
  font-family: arial;
  font-size: 14px;
  line-height: 20px;
  padding: 30px;
}

/* start MOBILE NAV */

#mobnav-btn {
  position: fixed;
  top: 50px;
  right: 50px;
  opacity: 1;
  color: #000;
  display: block;
  cursor: pointer;
  padding: 10px 9px;
  padding-bottom: 6px;
  font-size: 16px;
  border: 2px solid #DEDEDE;
  z-index: 999999999999;
  transition: all .2s ease;
  background-color: white;
  border-radius: 3px;
}

.mobile-menu #mobnav-btn .mobnav-btn-label {
  display: block;
}

.mobile-menu-open #mobnav-btn {
  background-color: #232128;
  border-color: #232128;
}

.mobile-menu-open {
  left: 50%;
  overflow: hidden;
}

.mobile-menu-open #mobnav-btn .mobnav-icon {
  color: white;
}

.xnav {
  position: fixed;
  background: #232128;
  width: 50%;
  left: -50%;
  bottom: 0%;
  top: 0%;
  z-index: 99999999999999999;
  zoom: 1;
  transition: all .2s ease;
}

.mobile-menu-open .xnav {
  z-index: 9999999999999;
  zoom: 1;
  left: 0%;
}

.xnav-wrapper {
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  height: 100%;
}

.xnav ul {
  float: none;
  list-style: none;
  margin: 0px;
  padding: 0px;
}

.xnav ul ul {
  padding-left: 30px;
  font-size: 98%;
}

.sf-menu li {
  float: none;
  margin: 0px!important;
  background: none!important;
}

.sf-menu a,
.sf-menu .first a {
  background-image: none!important;
  color: white!important;
  padding: 15px;
  margin-left: 0px;
  margin-right: 0px;
  display: block;
  border-bottom: 1px solid #3f3b49;
  transition: all .2s linear;
  text-decoration: none;
}

.sf-menu a:hover {
  background-color: #3f3b49!important;
  padding-left: 20px;
}

::-webkit-scrollbar {
  width: 10px;
}

::-webkit-scrollbar-track {
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.6);
}

.xnav-wrapper::-webkit-scrollbar-thumb {
  border-radius: 4px;
  background: rgba(128, 128, 128, 0.7);
}

::-webkit-scrollbar-thumb:window-inactive {
  background: rgba(32, 32, 32, 0.05);
}

/* end MOBILE NAV */
<!-- start MOBILE BTN -->
<div id="mobnav-btn">
  <div class="mobnav-icon">
    Menu
  </div>
</div>
<!-- end MOBILE BTN -->

<!-- start REGULAR CMS NAV -->
<div class="xnav">
  <div class="xnav-wrapper">
    <ul class="sf-menu">
      <li><a href="#">Home</a></li>
      <li><a href="#">Cupcakes</a>
        <ul>
          <li><a href="#">1.1 Cupcakes</a></li>
          <li><a href="#">1.2 Cupcakes</a></li>
          <li><a href="#">1.3 Cupcakes</a>
            <ul>
              <li><a href="#">2.1 Cupcakes</a></li>
              <li><a href="#">2.2 Cupcakes</a></li>
              <li><a href="#">2.3 Cupcakes</a></li>
            </ul>

          </li>
        </ul>
      </li>
      <li><a href="#">Doughnuts</a></li>
      <li><a href="#">Icecream</a></li>
      <li><a href="#">Banana Split</a> </li>
      <li><a href="#">Brownies</a></li>
      <li><a href="#">Lollipop</a></li>
      <li><a href="#">Pixie Stix</a></li>
      <li><a href="#">Bubble Gum</a></li>
      <li><a href="#">Chocolate Chips</a></li>
      <li><a href="#">Cherries</a></li>
    </ul>
  </div>
</div>
<!-- end REGULAR CMS NAV -->

<h1>Mobile Nav </h1>
<b><a href="mailto:RyanBrackett@gmail.com">RyanBrackett@gmail.com</a></b>
<br/>
<br/>The concept is very simple: Off-Canvas navigation based on the active state ( html.mobile-menu-open ). Mobile nav items options scroll if out of viewport.
<br/>
<br/>The CSS portion will be a little different because I had to create some styles that are natively in the framework. Also, the CSS portion will drop into your phone.css and tablet.css files. Cupcake ipsum dolor sit amet. Wafer tootsie roll I love oat
cake. Unerdwear.com icing bear claw liquorice marshmallow gummies cupcake tiramisu. Applicake cupcake powder jelly beans chupa chups muffin. Lollipop bear claw sweet roll macaroon. Jelly-o gummi bears apple pie. I love cheesecake carrot cake pudding toffee
marzipan chupa chups jelly beans dragée. Pie chocolate cake bear claw toffee gingerbread. Chupa chups bonbon lollipop topping I love. Cake dessert cheesecake candy canes sugar plum. Marzipan cookie apple pie muffin lemon drops. Chocolate cake oat cake
brownie cookie apple pie. I love I love sugar plum I love I love chocolate marshmallow lollipop.
<br/>
<br/>Apple pie ice cream dragée. Fruitcake cotton candy jujubes tootsie roll biscuit croissant caramels. Marzipan jelly-o candy I love lollipop bonbon. Applicake chocolate bar tootsie roll bonbon tiramisu jelly jelly beans. Marzipan I love sweet caramels
I love cupcake croissant marshmallow. Gingerbread danish marzipan jelly. Chocolate oat cake danish sugar plum bonbon. Pie gummi bears chocolate bar I love. I love halvah cotton candy sweet roll lollipop apple pie bear claw croissant. Sweet roll bear claw
donut topping. Cupcake I love jelly-o bear claw tootsie roll danish powder liquorice. Tart pastry tootsie roll I love.
<br/>
<br/>Cheesecake bear claw unerdwear.com unerdwear.com. Lemon drops chocolate oat cake donut unerdwear.com cheesecake dragée. Chocolate cake candy canes marzipan biscuit I love. Pudding biscuit powder. Pie gingerbread oat cake apple pie jujubes bonbon
powder. Danish I love I love gingerbread toffee. Icing I love chocolate. Halvah I love I love soufflé gingerbread jelly-o. Lollipop biscuit wafer marzipan I love cheesecake sesame snaps. Oat cake candy canes marzipan croissant. I love caramels cheesecake
chocolate toffee. Fruitcake powder gummies applicake cake lollipop topping icing.
<br/>
<br/>Wafer icing I love sweet jelly. I love muffin I love. Cotton candy I love I love I love candy biscuit. I love marzipan I love pie marshmallow gingerbread sesame snaps. Croissant pudding halvah sugar plum applicake candy I love sugar plum. Gingerbread
apple pie apple pie candy chocolate cake carrot cake jelly-o unerdwear.com. Chocolate sweet roll icing chupa chups brownie oat cake bonbon I love pie. Gummi bears cupcake pudding chocolate bar. Jujubes jelly-o sesame snaps toffee chocolate toffee. I love
toffee gingerbread marshmallow pie chocolate cake jelly beans. Biscuit jujubes jelly pudding gummies apple pie. Chupa chups I love chocolate cake unerdwear.com carrot cake I love sweet roll marshmallow.
<br/>
<br/>Cotton candy macaroon pudding croissant ice cream sesame snaps tiramisu. Unerdwear.com sweet roll candy I love. Tootsie roll soufflé lollipop chocolate cake soufflé. Jelly tootsie roll I love I love I love donut oat cake. Gummies croissant pastry
candy brownie ice cream. Wafer muffin icing. I love icing apple pie bonbon jelly I love jelly beans chupa chups. Candy bonbon carrot cake. Croissant I love candy canes pudding donut toffee. Cotton candy ice cream chupa chups candy canes muffin croissant
sesame snaps marshmallow. I love soufflé donut donut. Jelly beans bear claw sugar plum biscuit toffee gummi bears. Tiramisu lollipop applicake jujubes chocolate bar donut pudding chocolate bar bear claw.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
b5lpy0ml

b5lpy0ml3#

在你的位置试试这个:固定元素。

overflow-y: scroll;
max-height: 100%;
jrcvhitl

jrcvhitl4#

你可能需要一个内部的div。用css是:

.fixed {
   position: fixed;
   top: 0;
   left: 0;
   bottom: 0;
   overflow-y: auto;
   width: 200px; // your value
}
.inner {
   min-height: 100%;
}
soat7uwm

soat7uwm5#

这是绝对可行的一些flexbox魔术。看看这个pen
你需要这样的CSS:

aside {
  background-color: cyan;
  position: fixed;
  max-height: 100vh;
  width: 25%;
  display: flex;
  flex-direction: column;
}

ul {
  overflow-y: scroll;
}

section {
  width: 75%;
  float: right;
  background: orange;
}

这在IE10+中有效

9rnv2umw

9rnv2umw6#

下面是纯粹的HTML和CSS解决方案。
1.我们为导航栏创建一个容器框,其位置为:固定;高度:100%;
1.然后我们创建一个具有高度的内框:100%;溢出y:滚动;
1.接下来,我们将内容放入该框中。
下面是代码:

.nav-box{
        position: fixed;
        border: 1px solid #0a2b1d;
        height:100%;
   }
   .inner-box{
        width: 200px;
        height: 100%;
        overflow-y: scroll;
        border: 1px solid #0A246A;
    }
    .tabs{
        border: 3px solid chartreuse;
        color:darkred;
    }
    .content-box p{
      height:50px;
      text-align:center;
    }
<div class="nav-box">
  <div class="inner-box">
    <div class="tabs"><p>Navbar content Start</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content</p></div>
    <div class="tabs"><p>Navbar content End</p></div>
    </div>
</div>
<div class="content-box">
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
  <p>Lorem Ipsum</p>
</div>

Link to jsFiddle:

8iwquhpp

8iwquhpp7#

出于您的目的,您可以使用

position: absolute;
top: 0%;

并且它仍然是可调整大小、可滚动和响应的。

oaxa6hgo

oaxa6hgo8#

我把它作为一个变通方法而不是一个解决方案来介绍。这可能不是一直都有效。我是这样做的,因为我正在一个非常奇怪的环境中做一个非常基本的HTML页面,供内部使用。我知道有一些像MaterializeCSS这样的库可以做非常好的导航栏。(我打算使用它们,但它不适合我的环境。)

<div id="nav" style="position:fixed;float:left;overflow-y:hidden;width:10%;"></div>
<div style="margin-left:10%;float:left;overflow-y:auto;width:60%;word-break:break-all;word-wrap:break-word;" id="content"></div>
cwtwac6a

cwtwac6a9#

将此添加到固定高度的代码中,并添加一个滚动条。

.fixedbox {
    max-height: auto;
    overflow-y: scroll;
}

相关问题