新偏移量更新字体大小后不计算高度

new offsetHeight does not calculated after updating font size

本文关键字:计算 高度 偏移量 更新 字体      更新时间:2023-09-26

我有一个关于'offsetHeight'属性在更新字体大小后没有更新的问题。波纹管是代码。

function shrink_toolbar_text()
{
  var toolbar = document.getElementById("toolbar");
  var icons = document.getElementsByClassName("icon-alone");
  var icon_size = 64;
  var icon_height = icons[0].offsetHeight ;
  while( icon_height > toolbar.offsetHeight)
  {

   // textSpan.style.fontSize = parseInt(textSpan.style.fontSize) - 1;
    icon_size = icon_size -1;
    for ( i=0; i< icons.length ;i++){
      icons[i].style.fontSize = icon_size ; 
    }  
     icon_height = icons[0].offsetHeight ;
    console.log( icon_height);
    console.log( icon_size);
    // console.log( parseInt(icon_size));
  }
  console.log("shrink_toolbar_text() ");
}

关于这种情况的任何解决方法?到目前为止,我已经尝试过以下事情。1.尝试使用显示和隐藏重新绘制每个图标。2. 尝试了"getComputedStyle()",但它根本没有返回一个名为"offsetHeight"的值。

知道为什么代码不起作用吗?

代码应更正为此。

var icon_size = 64px;

icons[i].style.fontSize = icon_size+"px" ;

在某些浏览器中,如果没有后缀"px",此代码将无法工作。它也不会在控制台上发出错误消息。所以仔细编码。