javascript - 最大化和最小化按钮不触发效果
问题描述
所以我有一个 javascript 文件,我用它来确定我的网页的布局,并让字体适合某些 div。这是我的javascript代码:
/*
use class fittextpara to fit text in a paragraph
use class fittexthead to fit text in a paragraph
*/
var stylesheet = document.getElementById("site_structure");
var ratio;
//when the page loads, set its layout by changing address of css file
layout();
document.body.onload = function(){
font("fittextpara" , 8);
};
//on resize, change font and layout
document.body.onresize = layout_and_font;
function layout_and_font(){
layout();
font("fittextpara" , 8);
}
// function to change layout
function layout(){
//handling the layout
ratio = window.innerWidth / window.innerHeight;
//normal
if(ratio <= 2 && ratio >= 1.3)
stylesheet.setAttribute("href" , "normal.css");
//mobile
else if (ratio < 1.3)
stylesheet.setAttribute("href" , "mobile.css");
//widescreen
else
stylesheet.setAttribute("href" , "widescreen.css");
}
// function to fit font: enter the class name and scale factor
function font(classname , scale_factor){
//handling the text
var x = document.getElementsByClassName(classname)[0];
if(x != null){
if (x.clientHeight <= x.clientWidth)
x.style.fontSize = (x.clientHeight / scale_factor) + "px";
else
x.style.fontSize = (x.clientWidth / scale_factor) + "px";
}
}
代码运行良好,除了在一种情况下:当我使用最大化/最小化按钮调整浏览器窗口的大小时,布局正在改变,但字体大小(取决于 div 的宽度和高度,如fonts
函数)不是。我尝试将时间间隔设置为 500 毫秒,以延迟字体功能的执行,但仍然没有变化。
作为参考,我使用 Mozilla Firefox 作为我的浏览器。在我看来,当我使用最大化/最小化按钮调整浏览器窗口的大小时,代码无法检测到 div 的新高度/宽度。
解决方案
推荐阅读
- android - 无论如何使用android应用程序登录twitchtv并获取令牌?
- java - 如何使用Java在表单中搜索多个字段?
- java - DynamicUpdate 在更新语句中添加了额外的未触及列
- python - 在 Google AI Platform 上使用 Scikit-learn 获取预测时出现问题:“numpy.ndarray”对象没有属性“lower””
- filter - 一张纸上不存在时过滤另一张纸上的值
- google-apps-script - 如果 Google 表格今天未打开或未修改(24 小时内),则退出脚本
- django - django中所有URL的编程错误,postgres在db中找不到表?
- python - 从多个数据文件中提取列并将它们保存到新数据框中的单独列中
- php - 如何在 PHP 中修复“CURL 返回无效标头”
- django - Django - 编辑默认石墨烯集查询