重新调整网页内容的大小

时间:2012-07-27 08:30:15

标签: javascript jquery

在我的项目中,我有一个左右两个div区域的网页。左侧div占整个页面宽度的近60%,右侧占据页面大约36%。当我从右侧或左侧缩小浏览器时,我想以适当的比例调整两个div区域的大小。 UI是从Javascript生成的。这是代码。

boardHolderPadding = $board.outerHeight() - $board.height();
$board.height(viewportHeight - siblingsHeight - boardHolderPadding);

this.$('#board .droptarget').setWidthAsRatioOfHeight(this.options.ratio);

$('#sidebar').width($(window).width() - $board.find('#board').width() - 50);

我尝试使用JQuery resize插件,但无法获得我正在寻找的正确结果。有人有建议吗?

由于

3 个答案:

答案 0 :(得分:1)

请参阅jsfiddle example,但我认为您只需将宽度设置为百分比而不是尝试计算它们 - 请注意 display 设置为 inline-block

<div>
    <div class="small-left-column">this is the left column</div>
    <div class="large-right-column">this is the right column</div>
</div>

<style>
.small-left-column {
    width: 30%;
    background-color: #aeaeae;
    display: inline-block;
}
.large-right-column {
    width: 60%;
    background-color: #aeaeae;
    display: inline-block;
}
</style>

所以我认为你的例子就是这样的

$(document).ready(function () {
    $('#sidebar').addClass('small-left-column');
    $('#board').addClass('large-right-column');
});

答案 1 :(得分:0)

也许您正在寻找以上的纯Javascript版本:

$(document).ready(function () {
    $('#sidebar').css( {
        width: '30%'
        backgroundColor: '#aeaeae',
        display: 'inline-block'
    });
    $('#board').css({
        width: '60%',
        backgroundColor: '#aeaeae',
        display: 'inline-block'
    });
});

答案 2 :(得分:0)

我在Javascript中以不同的方式做到了这一点,我希望如果他们遇到类似的问题,这将有助于他人修复

relativeSize : function(width, height){
    var boardWidth = this.$("#board").width(),
            boardHeight = this.$("#board").height(), 
            newcard = this.$("#newCard").width(), 
            space = width - boardWidth - newcard;
            ratio = space / width * 3; //used to increment the ratio with 3 as the ratio is very tiny value, this will help to increase minimizing size
            bheight = boardHeight - 25; // used to reduce the height by 25px, u can use any amount to match ur ratio
        var relHeight = (space < ratio) ? bheight : height;
        return relHeight;
  }

由于