浏览器调整大小后重新计算宽度

L84*_*L84 0 javascript jquery

我正在运行第三方脚本,它会在加载时计算元素的宽度.它工作得很好,但我有一个响应式网站,当我重新调整浏览器大小时,元素不再正确显示.

以下是用于计算宽度的代码:

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);
Run Code Online (Sandbox Code Playgroud)

在调整浏览器大小时,如何修改脚本的这一部分以重新计算宽度?

Roh*_*wal 5

这是最好的工作

$(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
});
Run Code Online (Sandbox Code Playgroud)

超时的原因是该窗口需要一些时间才能获得settel的宽度,但经典window.resize事件获得触发器才能进行设置