开发者

jQuery calculation doesn't add up as expected when toggling height

开发者 https://www.devze.com 2023-03-05 02:03 出处:网络
I have the following function for calculating the heigh开发者_JAVA百科t of .node. It then takes away the height of a possible image, .node-image, from the height of the .node, and sets a column, .node

I have the following function for calculating the heigh开发者_JAVA百科t of .node. It then takes away the height of a possible image, .node-image, from the height of the .node, and sets a column, .node-content-column to have a height that is the difference (i.e. 500 - 50 = 450; column becomes 450 in height).

function initColumnSizer() {
  imageHeight  = $('.node-image').outerHeight(true);
  resizeHeight = ($('.node').outerHeight() + 75) - imageHeight;
  $('.node-content-column').removeAttr('style');
  $('.node-content-column').css('min-height', resizeHeight);
  $('.node-content-column').css('height', 'auto !important');
  $('.node-content-column').css('height', resizeHeight);
}

This function gets called on page load, and resizes .node-content-column as expected.

It also gets called when a div within .node is toggled using jQuery.toggle(), but this calculation returns a larger number everytime, instead of reverting back to the original once this toggle is reverted.

Can anyone see where I am going wrong with this calculation? Or if I am going about it the wrong way?

Thanks in advance! Karl


1) Maybe the problem is in outerHeight() function (it takes into account padding and border). Try using just height or clientHeight:

var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

2) why do you need to cleanup the whole elements' style? and then you try to assign height = auto, and after that: height = resizeHeight - what's the purpose for that ? check the logic of your code.


outerHeight(true) will return height + padding + border + margin. Possibly, you might want to use height() ?


Most possible is that "larger number everytime" have always constant difference -- for example 75. May be you just have some dependecies between .node-content-column and .node?


If your nodes like .node-content-column, .node and .node-image are all singles, then it's better to use IDs for them -- not CSS classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消