简体   繁体   English

可滚动的div在窗口之外显示内容?

[英]Scrollable div show content outside of window?

I have this jsfiddle http://jsfiddle.net/r3pek/wxffL/ where i'm trying to understand why does the scroll goes beyond de window size :/ 我有这个jsfiddle http://jsfiddle.net/r3pek/wxffL/在这里我试图理解为什么滚动超出了de window size:/

If I remove the "height: 100%" from the rightpane class, I don't have a scroll; 如果我从右窗格类中删除“高度:100%”,则没有滚动。 if I add it, I have a scroll but that goes beyond the window. 如果添加它,则有滚动条,但这超出了窗口。 Any way I can limit the scroll to the window?! 有什么办法可以限制滚动到窗口吗?

Thanks in advance! 提前致谢!

EDIT: Just a quick update... I updated the fiddle to reflect the actual problem. 编辑:只是快速更新...我更新了小提琴以反映实际的问题。 I have an image that takes space as a header and it looks like that image size isn't accounted for. 我有一个以空格为标题的图像,看起来该图像的大小未考虑在内。 (I really suck at CSS :P ) (我真的很讨厌 CSS:P)

You have to define a height for an element to scroll. 您必须定义元素的高度才能滚动。 That's why the scrollbar disappears when you remove the height. 这就是为什么当您删除高度时滚动条会消失的原因。 You're also adding padding to the div along with the 100% height. 您还将在div中添加填充以及100%的高度。 That adds to the element's height so it ends up being taller than the window. 这增加了元素的高度,因此最终比窗口高。 Reduce the height to something less than 100%, maybe 90% and play with it. 将高度降低到小于100%,也许是90%的水平,然后使用它。 That will allow you to keep the scrollbar and keep it inside the window. 这样您就可以保留滚动条并将其保留在窗口内。 I have a fiddle set up for you here . 在这里为你准备了一个小提琴。

The total height (or "outer height") of an element equals inner height (which you can specify in css) + padding + border. 元素的总高度(或“外部高度”)等于内部高度(可以在css中指定)+填充+边框。

If you use height: 100% but then also add padding and/or borders then the total height will be bigger than 100%. 如果您使用height: 100%但同时还要添加填充和/或边框,则总高度将大于100%。 There's a css property called box-sizing that can help you but it's not cross-broswer (you guessed it, IE<9). 有一个叫做box-sizing的css属性可以为您提供帮助,但它不是交叉浏览器(您猜对了,IE <9)。

If you drop the borders and paddings, it'll be fixed. 如果放下边框和填充,它将得到修复。 But then to have borders and padding on outer elements... you'll need to get creative (or come back here with a specific question) 但是,要在外部元素上加上边框和填充...您将需要发挥创造力(或回到此处提出一个具体问题)

OK, I solved the problem, just not sure if it was the "right way". 好的,我解决了这个问题,只是不确定这是否是“正确的方法”。 Anyway, here's how I did it: 无论如何,这是我的做法:

added this right before the tag: 在标记之前添加了此代码:

<script>
    window.onload=setRightPaneHeight; 
</script>

Then, I created the function that will calculate the right size for the "rightpane": 然后,我创建了一个函数,该函数将为“右窗格”计算正确的大小:

function setRightPaneHeight(){
    var pic = document.getElementById("headerPic");
    var pic_h = pic.offsetHeight;
    var total_h = window.innerHeight;
    var right_pane =  document.getElementById("rightpane")

    $(".rightpane").height(total_h - pic_h - 30);
}

That being done, now after the page loads, the right height is calculated for the rightpane DIV. 这样做之后,现在在页面加载之后,为右窗格DIV计算了正确的高度。 And it works :) 它有效:)

Thanks for all the answers as they made me understand what the problem was! 感谢您提供所有答案,因为它们使我了解问题所在!

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

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