【问题标题】:Is is possible to make element with % set dimensions to show scrolls, instead of increasing own size?是否可以使用 % set 尺寸制作元素来显示滚动,而不是增加自己的尺寸?
【发布时间】:2015-01-24 18:43:06
【问题描述】:

例如,我有一个尺寸为 width: 100%;height: 100%;overflow: auto; 的元素容器

当我在其中添加更多元素时,这些元素对于容器来说变得太大了,它会增加自己的区域以适应这些元素,而不是显示滚动。

有什么解决办法吗?

例子:

<html style = 'height: 100%;'>
    <head>
        <title>test scrolls</title>
    </head>
    <body style = 'height: 100%;'>
        <table border = '1' style = 'width: 100%; height: 100%;'>
            <tr><td id = "test_td" height = '50%' style = 'overflow: scroll;'></td></tr>
            <tr><td height = '50%'><input type = "button" value = "add a lto of junk in first td" onclick = "document.getElementById('test_td').innerHTML = 'asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />'" /></td></tr>
        </table>
    </body>
</html>

好的,另一个 div 示例:

<html style = 'height: 100%;'>
    <head>
        <title>test scrolls</title>
    </head>
    <body style = 'height: 100%;'>
        <table border = '1' style = 'width: 100%; height: 100%;'>
            <tr><td height = '50%'><div id = "test_div" style = 'width: 100%; height: 100%; overflow: scroll;'></div></td></tr>
            <tr><td height = '50%'><input type = "button" value = "add a lot of junk in div in first td" onclick = "document.getElementById('test_div').innerHTML = 'asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />'" /></td></tr>
        </table>
    </body>
</html>

【问题讨论】:

  • 请提供您拥有的代码或创建一个小提琴。
  • 请给我们看一些代码,并提供一个带有工作演示的 JSFiddle 链接
  • 你可以用javascript来做...我认为这是唯一的方法...
  • @Gezzasa - 我添加了示例。
  • @pomeh - 我添加了示例。

标签: html css scroll containers


【解决方案1】:

这是一个例子http://jsfiddle.net/83akmom1/

你需要将你要设置的div的父级的高度设置为height: 100%;它会起作用的

*{
    margin: 0px;
}
body{
    height: 100%;
}
.wrap{
    width: 100%; 
    height: 100%; 
    overflow: auto;
}

【讨论】:

  • 你能说我的例子有什么问题吗?它到处都设置了高度,但没有出现任何卷轴
  • 您希望滚动只显示第一个 tr 元素?
  • 我想让第一个 TD 在内容太大而无法容纳时显示滚动。这只是一个例子。我需要通用解决方案。
  • @Kosmos 看我上面的评论
【解决方案2】:

这就是你要找的东西

TD 不能滚动,但你可以在里面有一个可以滚动的 div。

http://jsfiddle.net/b9kzbf8c/1/

div.scrollable {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: auto;
}

<table border = '1' style = 'width: 100%; height: 100%;'>
    <tr style="height: 40px; overflow-y: scroll;">
        <td>
            <div class=scrollable>Some content with a scrollbar if it's too big for the cell
                Some content with a scrollbar if it's too big for the cell
                Some content with a scrollbar if it's too big for the cell
            </div>
        </td>
    <table>

编辑

我也用 jQuery 做到了

http://jsfiddle.net/pwcd9cxe/2/

<script type="text/javascript">
    var equalHeights;

    resize = function(me){
        equalHeights = me;
        $('.td').height(($(equalHeights).height())/2);
    };

    $(window).resize(function(){
        $('.td').height(($(equalHeights).height())/2);
    });

    resize($(window));
</script>

<style>
    .td {overflow-y: scroll;}
</style>

<div class="td">
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
</div>
</div>
<div class="tr">
<div class="td">
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
    Some content here that is copy pasted to fill up space. <br />
</div>

编辑

我已经设法在没有任何 JS 的情况下做到了。

http://jsfiddle.net/pwcd9cxe/3/

【讨论】:

  • 老实说,我不明白你的例子显示了什么。在ie 10 和opera 12.17 中没有卷轴。而且,我不知道&lt;tr style="height: 40px; 是干什么用的。
  • 去看看小提琴链接。表格行上的样式是设置一个高度,以便div在放更多内容时可以滚动。
  • 抱歉,一旦我移除固定高度,一切都会停止工作。我需要所有尺寸都在 % 中,但是一些容器在溢出时显示滚动,而不是增加自己的尺寸。
  • 需要使用表格吗?
  • 是的。他们无处不在。有了这些就更容易创建我需要的布局;我虽然关于可能的解决方案。将绝对定位的 div 放入 TD。那个 div 应该有overflow: scroll; 和 % 的尺寸。问题是 % size 不能以这种方式工作,并且 div 保持苗条。所以,我目前正在编写每 0.5 秒运行一次的递归函数,并根据其父级大小修复 div 的大小。
【解决方案3】:
<html style = 'height: 100%;'>
    <head>
        <title>test scrolls</title>

        <script type = "text/JavaScript">
            function endsWith(str, suffix)
            {
                if (!str)
                    return false;

                return str.indexOf(suffix, str.length - suffix.length) >= 0;
            }

            function fixDimensionsRecursiveIn(start_elem)
            {
                if (!document || !document.body)
                    return;

                var curr_elem = start_elem ? start_elem : document.body;

                var pos = curr_elem.style.position || curr_elem.style.getAttribute("position");

                if (pos == "absolute")
                {
                    var curr_w = curr_elem.width || curr_elem.style.width;
                    var curr_h = curr_elem.height || curr_elem.style.height;
                    var parent_w = curr_elem.parentNode.clientWidth || curr_elem.parentNode.offsetWidth;
                    var parent_h = curr_elem.parentNode.clientHeight || curr_elem.parentNode.offsetHeight;

                    var w_fixed = false;
                    var h_fixed = false;

                    if (endsWith(curr_w, "%") && parent_w > 0)
                    {
                        curr_elem.original_width = curr_w;
                        curr_w = curr_w.substr(0, curr_w.length - 1);
                        curr_elem.style.width = ((curr_w * parent_w) / 100) + "px";

                        w_fixed = true;
                    }

                    if (endsWith(curr_h, "%") && parent_h > 0)
                    {
                        curr_elem.original_height = curr_h;
                        curr_h = curr_h.substr(0, curr_h.length - 1);
                        curr_elem.style.height = ((curr_h * parent_h) / 100) + "px";

                        h_fixed = true;
                    }

                    var possible_original_width = curr_elem.original_width;
                    var possible_original_height = curr_elem.original_height;

                    if (!w_fixed && endsWith(possible_original_width, "%") && parent_w > 0)
                    {
                        possible_original_width = possible_original_width.substr(0, possible_original_width.length - 1);
                        curr_elem.style.width = ((possible_original_width * parent_w) / 100) + "px";
                    }

                    if (!h_fixed && endsWith(possible_original_height, "%") && parent_h > 0)
                    {
                        possible_original_height = possible_original_height.substr(0, possible_original_height.length - 1);
                        curr_elem.style.height = ((possible_original_height * parent_h) / 100) + "px";
                    }
                }

                for (var i = 0; i < curr_elem.children.length; i++)
                    fixDimensionsRecursiveIn(curr_elem.children[i]);
            }

            setInterval(fixDimensionsRecursiveIn, 500);
        </script>
    </head>
    <body style = 'height: 100%;'>
        <table border = '1' style = 'width: 100%; height: 100%;'>
            <tr><td height = '50%' style = 'position: relative;'><div id = "test_div" style = 'border: dashed; margin: 0px; position: absolute; left: 0px; top: 0px; width: 100%; height: 100%; overflow: scroll;'></div></td></tr>
            <tr><td height = '50%'><input type = "button" value = "add a lot of junk in div in first td" onclick = "document.getElementById('test_div').innerHTML = 'asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />'" /></td></tr>
        </table>
    </body>
</html>

这是我自己的解决方案,但我希望将来能得到更好的解决方案。

或者根据 Gezzasa 的解决方案使用双 div:

<html style = 'height: 100%;'>
    <head>
        <title>test scrolls</title>
    </head>
    <body style = 'height: 100%;'>
        <table border = '1' style = 'width: 100%; height: 100%;'>
            <tr>
                <td height = '50%'>
                <div  style = "border: solid; position: relative; width: 100%; height: 100%;">
                    <div id = "test_div" style = 'border: dashed; position: absolute; top: 0px; left: 0px; bottom: 0%; width: 100%; overflow: scroll;'></div>
                </div>
                </td>
            </tr>
            <tr>
                <td height = '50%'>
                    <input type = "button" value = "add a lot of junk in div in first td" onclick = "document.getElementById('test_div').innerHTML = 'asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />asd<br />'" />
                </td>
            </tr>
        </table>
    </body>
</html>

【讨论】:

  • jQuery 更简单一些。
猜你喜欢
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多