0

Now I have working on window load code, but can't make it work with window resize (height values will increase if I repeat the same function for resize event). Array is to show initial values after onload completed.

var arr = [];

window.onload = function() {
    var newsBlocks = document.getElementsByClassName('news');
    for (var i = 0; i < newsBlocks.length; i++) {
      var newsBlock = newsBlocks[i];
      var left = newsBlock.getElementsByTagName('article')[0];
      var right = newsBlock.getElementsByTagName('article')[1];
      var height = Math.max(left.offsetHeight, right.offsetHeight);
      left.style.height = right.style.height = height.toString() + 'px';
      arr.push(left.style.height);
      console.log(arr);
     }
};

Here is html:

<div class="main">
    <section class="news">
        <article>
            <a href="#" class="news-image"><img src="images/t.png"></a>
            <h2><a href="#">Heading</a></h2>
            <a href="#" class="description">test text</a>
        </article>
        <article>
            <a href="#" class="news-image"><img src="images/t.png"></a>
            <h2><a href="#">Heading</a></h2>
            <a href="#" class="description">a lot of test text here</a>
        </article>
</div>

Pure JavaScript solution is appreciated. Thanks in advance for a help!

Glebcha
  • 135
  • 1
  • 13

3 Answers3

0

You should re-organize your code to be able to repaint the whole screen any time, and hook on the resize event, see: JavaScript window resize event

Resize event is called also on mobile devices, when they change orientation.

Community
  • 1
  • 1
ern0
  • 3,062
  • 23
  • 35
  • I used window.onresize of course, but the main problem is that div height will only increment and grow fast6 but I need it to be the max value of tow divs in container that are fluid with width set to 50%. – Glebcha Sep 25 '13 at 09:09
0
<div id=div1></div>


<script>
div1.style.height = (your preffered height)
</script>

same you can do with width.

it works for me ...

Mayur Gupta
  • 717
  • 2
  • 7
  • 23
0

try the following code. It is working for me.

var buffer = 20; //scroll bar buffer
var divId = document.getElementById('yourDivID');

function pageY(elem) {
    return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
}

function resizeDiv() {
    var height = document.documentElement.clientHeight;
    height -= pageY(divId) + buffer;
    height = (height < 0) ? 0 : height;
    divId.style.height = height + 'px';
}

window.onresize = resizeDiv;
Ahsan Shah
  • 3,575
  • 1
  • 27
  • 44