0

I am running a third party script and it calculates an element's width upon loading. It works great but I have a responsive website and when I re-size the browser the element is no longer displayed correctly.

Here is the code that is used to calculate the width:

var w = ticker.width();
if (label.length) w -= label.outerWidth() + parseFloat(label.css("margin-right"));
if (controls.length) w -= controls.outerWidth() + parseFloat(controls.css("margin-left"));
news.css("width", w);

How do I modify this part of the script to re-calculate the width when the browser is resized?

L84
  • 45,514
  • 58
  • 177
  • 257

2 Answers2

5

Here is what work best

$(window).resize(function() {
if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
      $(this).trigger('windowResize');
    }, 500); 
});
$(window).on('windowResize', function() {
   console.log($(window).width()); // calculating here
});

reason for having timeout is that window takes some time to get settel the width but classic window.resize event get triggers before it setteled down

Rohit Agrawal
  • 5,372
  • 5
  • 19
  • 30
0
$(window).on('resize', function(){
   //your code to calculate the width
});
claustrofob
  • 5,448
  • 2
  • 19
  • 22