set div height using jquery (stretch div height)

前端 未结 6 1947
抹茶落季
抹茶落季 2020-12-04 17:37

I have 3 divs with ids header, content and footer. Header and footer has fixed height and they are styled to float on top and bottom.

相关标签:
6条回答
  • 2020-12-04 18:11

    The correct way to do this is with good-old CSS:

    #content{
        width:100%;
        position:absolute;
        top:35px;
        bottom:35px;
    }
    

    And the bonus is that you don't need to attach to the window.onresize event! Everything will adjust as the document reflows. All for the low-low price of four lines of CSS!

    0 讨论(0)
  • 2020-12-04 18:23

    Off the top of my head:

    $('#content').height(
        $(window).height() - $('#header').height() - $('#footer').height()
    );
    

    Is that what you mean?

    0 讨论(0)
  • 2020-12-04 18:24

    well you can do this:

    $(function(){
    
        var $header = $('#header');
        var $footer = $('#footer');
        var $content = $('#content');
        var $window = $(window).on('resize', function(){
           var height = $(this).height() - $header.height() + $footer.height();
           $content.height(height);
        }).trigger('resize'); //on page load
    
    });
    

    see fiddle here: http://jsfiddle.net/maniator/JVKbR/
    demo: http://jsfiddle.net/maniator/JVKbR/show/

    0 讨论(0)
  • 2020-12-04 18:24
    $(document).ready(function(){ contsize();});
    $(window).bind("resize",function(){contsize();});
    
    function contsize()
    {
    var h = window.innerHeight;
    var calculatecontsize = h - 70;/*if header and footer heights= 35 then total 70px*/ 
    $('#content').css({"height":calculatecontsize + "px"} );
    }
    
    0 讨论(0)
  • 2020-12-04 18:29

    I think will work.

    $('#DivID').height('675px');
    
    0 讨论(0)
  • 2020-12-04 18:30

    You can bind function as follows, instead of init on load

    $("div").css("height", $(window).height());
    $(​window​).bind("resize",function() {
        $("div").css("height", $(window).height());
    });​​​​
    
    0 讨论(0)
提交回复
热议问题