简体   繁体   English

无法在固定div中滚动div

[英]Unable to scroll div within fixed div

I'm experiencing problems with a menu containing a parent container which spreads over the entire site and a div with the actual content. 我遇到一个菜单问题,该菜单包含一个分布在整个网站上的父容器和一个包含实际内容的div。 Everything is okay as long as the device screen is big enough. 只要设备屏幕足够大,一切都可以。 But especially with mobile devices its not possible to show the entire content. 但是特别是对于移动设备,不可能显示全部内容。

I've created a jsfiddle at http://jsfiddle.net/89xyzsfz/ to show the problem with a working sample and the needed js and css. 我在http://jsfiddle.net/89xyzsfz/创建了一个jsfiddle,以使用工作示例以及所需的js和css来显示问题。 On mobile devices just some of the parts are visible but it is not possible to scroll the content. 在移动设备上,只有某些部分可见,但是无法滚动内容。

The relevant code itself explained: 相关代码本身说明:

            <div class="hiddenMenu jsMenu">
                <div class="menuContainer jsMenuContainer">
                    <h3>Menu content</h3>

                    <ul>
                        <li><a href="#item1">Item1</a></li>
                        <li><a href="#item2">Item2</a></li>
                        <li><a href="#item3">Item3</a></li>
                        <li><a href="#item4">Item4</a></li>
                        <li><a href="#item5">Item5</a></li>
                        <li><a href="#item6">Item6</a></li>
                        <li><a href="#item7">Item7</a></li>
                        <li><a href="#item8">Item8</a></li>
                        <li><a href="#item9">Item9</a></li>
                    </ul>
                </div>

                <div class="menuBackground jsMenuBackground"></div>
            </div>
  • hiddenMenu holds the entire menu. hiddenMenu保存整个菜单。 At load the entire menu is hidden and enabled via clicking on an element with css class jsMenuButton assigned. 在加载时,通过单击分配了css类jsMenuButton的元素来隐藏和启用整个菜单。
  • menuContainer is the container with the content which should be scrollable when parts of it are not visible at the device. menuContainer是一个容器,其中的内容在设备上不可见时应该可以滚动。
  • menuBackground is used for designing the background while showing the content with no specific function apart from that. menuBackground用于设计背景,同时显示内容,除此之外没有其他特定功能。

Change your css for .hiddenMenu .menuContainer to the following: .hiddenMenu .menuContainer的css更改为以下内容:

.hiddenMenu .menuContainer {
  position: relative;
  margin: 0 auto;
  text-align: center;
  z-index: 12;
  height: 100%;
  overflow: auto;
}

The .menuContainer needs to scroll the menu items and therefore requires the overflow:auto; .menuContainer需要滚动菜单项,因此需要overflow:auto; , height:100% and needs to be positioned relative instead of absolute. height:100%并且需要相对放置而不是绝对放置。

http://jsfiddle.net/89xyzsfz/5/ http://jsfiddle.net/89xyzsfz/5/

Hope that helps! 希望有帮助!

Simple Change Your Code to this CSS. 只需将您的代码更改为此CSS。 The overflow is clipped, but a scroll-bar is added to see the rest of the content 溢出被裁剪,但是添加了滚动条以查看其余内容

.hiddenMenu {
    display: none;
    height: 100%;
    left: 0;
    position: fixed;
    top : 0;
    width: 100%;
    z-index: 10;
    overflow:scroll;
}

Overflow Values 溢出值

visible: content is not clipped when it proceeds outside its box. 可见:内容超出其框的范围时不会被剪切。 This is the default value of the property 这是属性的默认值

hidden: overflowing content will be hidden. 隐藏:溢出的内容将被隐藏。

scroll: similar to hidden except users will be able to scroll through the hidden content 滚动:类似于隐藏内容,但用户可以滚动浏览隐藏内容

auto: if the content proceeds outside its box then that content will be hidden whilst a scroll bar should be visible for users to read the rest of the content. 自动:如果内容超出其框的范围,则该内容将被隐藏,而滚动条应可见,以便用户阅读其余内容。

initial: uses the default value which is visible 初始值:使用可见的默认值

inherit: sets the overflow to the value of its parent element. 继承:将溢出设置为其父元素的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM