最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery .height() not working until resize of window - Stack Overflow

programmeradmin2浏览0评论

I'm using this code to resize a div on my page:

function sizeContent() {
    var newHeight = ($("html").height() - $(".header").height() -3) + "px";
    $(".content").height(newHeight);
}

I am triggering this using:

$(window).resize(sizeContent());

But I also call it inside

$(document).ready(sizeContent());

to set all the heights properly.

If i resize the browser window by dragging at its borders it works great.

But the initial call does not seem to have any effect. Putting an alert inside the sizeContent function I can see that it is being called at page ready, and is setting the right height, but nothing is changed on the page.

It's as if the browser does not recognize that it needs to redraw anything unless the window itself is changed.

Solved: In the end it was not the javascript that was the problem, using div's with display:table and display:table-row was causing it. Removed these from the css and everything worked.

I'm using this code to resize a div on my page:

function sizeContent() {
    var newHeight = ($("html").height() - $(".header").height() -3) + "px";
    $(".content").height(newHeight);
}

I am triggering this using:

$(window).resize(sizeContent());

But I also call it inside

$(document).ready(sizeContent());

to set all the heights properly.

If i resize the browser window by dragging at its borders it works great.

But the initial call does not seem to have any effect. Putting an alert inside the sizeContent function I can see that it is being called at page ready, and is setting the right height, but nothing is changed on the page.

It's as if the browser does not recognize that it needs to redraw anything unless the window itself is changed.

Solved: In the end it was not the javascript that was the problem, using div's with display:table and display:table-row was causing it. Removed these from the css and everything worked.

Share Improve this question edited Aug 4, 2014 at 0:12 zx81 41.9k10 gold badges92 silver badges106 bronze badges asked May 2, 2014 at 10:35 JensBJensB 6,8803 gold badges62 silver badges107 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2
// If you put your code at the bottom of the page to avoid needing`$(document).ready`, it gets even simpler:

$(window).on('resize', function () {
    var newheight = ($(window).height() - $('.header').height() - 3);
    $(".content").css("height", newheight);
}).trigger('resize');


// Another way to do that same thing

$(document).ready(sizeContent);
$(window).on('resize', sizeContent);

function sizeContent() {
  var newheight = ($(window).height() - $('.header').height() - 3);
    $(".content").css("height", newheight);
}


// Another technique is to`.trigger()`one event inside the other:

$(window).on('resize', function () {
   var newheight = ($(window).height() - $('.header').height() - 3);
   $(".content").css("height", newheight);
});
$(document).ready(function () {
   $(window).trigger('resize');
});

Replace

$(".content").css({height:newHeight}) //replace this

with

(".content").height(newHeight);

Use below code:

function sizeContent() {
    var newHeight = ($(window).height() - $(".header").css('height')-3) ;
    $(".content").css('height',newHeight);
}
发布评论

评论列表(0)

  1. 暂无评论