1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 按钮宽度和高度固定 字体大小根据字数自适应用的javascript实现

按钮宽度和高度固定 字体大小根据字数自适应用的javascript实现

时间:2020-08-14 21:39:25

相关推荐

按钮宽度和高度固定 字体大小根据字数自适应用的javascript实现

演示效果如下:

其中按钮的高度固定为40px,按钮的宽度固定为60px,字体大小根据宽度自适应。

具体代码如下:

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

</head>

<body>

<div>

<br><br>

&nbsp;&nbsp;&nbsp;<input type="button" value="A"/>

<input type="button" value="AB"/>

<input type="button" value="ABC"/>

<br><br>

&nbsp;&nbsp;&nbsp;<input type="button" value="ABCD"/>

<input type="button" value="ABCDE"/>

<input type="button" value="ABCDEF"/>

</div>

</body>

<script type="text/javascript">

var maxHeight=40; //固定高度

var maxWidth=60; //固定宽度

var height=[];

var width=[];

var button=document.getElementsByTagName("input");

for(var i=0;i<button.length;i++){

button[i].style.fontSize="8px";

height[i]=button[i].offsetHeight;

width[i]=button[i].offsetWidth;

while(parseInt(height[i])<maxHeight&&parseInt(width[i])<maxWidth){ //逐次递增字体大小,使字体达到最大的字号

button[i].style.fontSize=parseInt(button[i].style.fontSize)+1+"px";

height[i]=button[i].offsetHeight;

width[i]=button[i].offsetWidth;

}

button[i].style.width="60px";

button[i].style.height="40px";

}

</script>

</html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。