使用CSS

时间:2017-12-13 12:10:50

标签: javascript jquery html css

我使用CSS resize:both;属性让用户调整我的内容大小。请参阅下面的代码。



.foo {
    resize: both;
    min-width: 250px;
    width: auto;
    text-align: center;
    min-height: 30px;
    border: 1px solid #515151;
    border-radius: 6px;
    position: absolute;
    padding: 0;
    overflow: hidden;
    text-shadow: 1px 1px 1px rgba(0,0,0,0.33);
    box-shadow: 5px 5px rgba(0,0,0, 0.1);
}
.thClass {
    margin-left: -3px;
    text-align: center;
    box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    -webkit-box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    -moz-box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    margin-top: -2px;
    min-height: 30px;
    line-height: 30px;
    font-size: 19px;
    cursor: move;
}
.header{
    margin-left: 17px;
}
.tableBody {
    display: block;
    min-height: 25px;
    text-align: center;
    width: 102%;
    margin-left: -2px;
    cursor: default;
}
.foo tbody tr td {
    display: block;
    line-height: 25px;
    text-align: center;
    border-bottom: 1px solid rgba(0,0,0,0.2);
}
#displaySizes {
    list-style: none;
    padding: 0;
    margin-top: 0;
}
.disp tbody tr th {
    border: 1px solid black;
}
.disp tbody tr td {
    display: table-cell;
}

<table class="foo disp elementTable">
                <tr class="tableHeader">
                    <th class="thClass" colspan="5">
                        <span class="header">Device</span>
                    </th>
                </tr>
                <tr>
                    <td rowspan="4" id="sizeContainer">
                        <ul id="displaySizes">
                            <li>4:3</li>
                            <li>16:9</li>
                            <li>Clock</li>
                        </ul>
                    </td>
                    <td>$100</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
            </table>
&#13;
&#13;
&#13;

有没有办法使用CSS逐步调整此表的大小,例如[10,10]像素? JavaScript也行。我在网上搜索过,但找不到任何东西。以下是您可以使用代码进行工作的fiddle

2 个答案:

答案 0 :(得分:2)

这是一个好点,所以可以使用css-element-queries库, 您所拥有的只是为您的表创建ResizeSensor(),然后进行计算以设置高度和宽度范围更改:

见下面的摘录:

var width = $('.elementTable').width();
var height = $('.elementTable').height();
var changePX = 50;


new ResizeSensor(jQuery('.elementTable'), function(){ 

    //width calcuation 
    if(Math.abs($('.elementTable').width()-width) > changePX) {
      $('.elementTable').width() > width ? width +=changePX : width -=changePX;
    }
    $('.elementTable').width(width);
    
    //height calcuation 
    if(Math.abs($('.elementTable').height()-height) > changePX) {
      $('.elementTable').height() > height ? height +=changePX : height -=changePX;
    }
    $('.elementTable').height(height);
    
})
.foo {
    resize: both;
    min-width: 250px;
    width: auto;
    text-align: center;
    min-height: 30px;
    border: 1px solid #515151;
    border-radius: 6px;
    position: absolute;
    padding: 0;
    overflow: hidden;
    text-shadow: 1px 1px 1px rgba(0,0,0,0.33);
    box-shadow: 5px 5px rgba(0,0,0, 0.1);
}
.thClass {
    margin-left: -3px;
    text-align: center;
    box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    -webkit-box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    -moz-box-shadow: 0 2px 2px rgba(0,0,0,0.26);
    margin-top: -2px;
    min-height: 30px;
    line-height: 30px;
    font-size: 19px;
    cursor: move;
}
.header{
    margin-left: 17px;
}
.tableBody {
    display: block;
    min-height: 25px;
    text-align: center;
    width: 102%;
    margin-left: -2px;
    cursor: default;
}
.foo tbody tr td {
    display: block;
    line-height: 25px;
    text-align: center;
    border-bottom: 1px solid rgba(0,0,0,0.2);
}
#displaySizes {
    list-style: none;
    padding: 0;
    margin-top: 0;
}
.disp tbody tr th {
    border: 1px solid black;
}
.disp tbody tr td {
    display: table-cell;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script>
<table class="foo disp elementTable">
                <tr class="tableHeader">
                    <th class="thClass" colspan="5">
                        <span class="header">Device</span>
                    </th>
                </tr>
                <tr>
                    <td rowspan="4" id="sizeContainer">
                        <ul id="displaySizes">
                            <li>4:3</li>
                            <li>16:9</li>
                            <li>Clock</li>
                        </ul>
                    </td>
                    <td>$100</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
                <tr>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                    <td>February</td>
                </tr>
            </table>

答案 1 :(得分:2)

这是我放在一起的解决方案,它将在调整大小时捕捉到给定网格(在此示例中为20px。)

它使用&#34;跨浏览器调整大小事件监听器&#34;,另一个开发人员放在一起(source):

基本上它只是侦听resize事件,然后使用javascript设置宽度和高度的样式。

小提琴:

https://jsfiddle.net/treetop1500/co8zbatz/

// utility function for rounding (20px in this case)
function round20(x)
{
    return Math.ceil(x/20)*20;
}

// Attach listener
var myElement = document.getElementById('resize'),
    myResizeFn = function(){
        h = round20(parseInt(this.style.height,10));
        w = round20(parseInt(this.style.width,10));
        this.style.height = h + "px";
        this.style.width = w + "px";
    };

addResizeListener(myElement, myResizeFn);