当前位置:网站首页>缩放文本以适合元素内部
缩放文本以适合元素内部
2022-07-26 20:31:00 【紫微前端】
假设我们要缩放标题内的文本:
<div id="headline">Hello World</div>首先,我们需要用元素当前的字体大小和文本内容来测量元素的宽度。
const measureWidth = function(text, font) {
// Measure the width of given text for given font
...
};
// Query the element
const ele = document.getElementById('headline');
// Get the styles
const styles = window.getComputedStyle(ele);
// Get the font size and font style
const font = styles.font;
const fontSize = parseInt(styles.fontSize);
const measured = measureWidth(ele.textContent, font);现在我们可以通过比较测量的宽度和全宽来计算元素的缩放比例:
const scale = ele.clientWidth / parseFloat(measured);最后,我们将字体大小设置为元素放大到全宽:
const scaleFontSize = Math.floor(scale * fontSize);
ele.style.fontSize = `${scaleFontSize}px`;边栏推荐
- What kind of security problems will the server encounter?
- 2022-7-26 第七组 抽象和接口
- CONDA reports an error: json.decoder.jsondecodeerror:
- How to implement Devops with automation tools | including low code and Devops application practice
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- About: get the domain controller of the current client login
- SPI configuration
- 微服务化解决文库下载业务问题实践
- 【HCIA安全】用户认证
- Redis interview questions
猜你喜欢

苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条

Redis面试题
![[question] browser get request with token](/img/95/f0b7186f930c014495414c3fcdaa6e.png)
[question] browser get request with token
HTTP cache browser cache that rabbits can understand

TCP的粘包拆包问题解决方案

Calculation formula of retained earnings rate

【HCIE安全】双机热备-主备备份

Why does it system need observability?

Go+mysql+redis+vue3 simple chat room, the sixth bullet: use vue3 and element plus to call the interface

PLSQL package
随机推荐
After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores
[hero planet July training leetcode problem solving daily] 26th and check the collection
ROS2获取当前系统时间的方法
微服务化解决文库下载业务问题实践
游览器——游览器游览器缓存
Mysql -count :count(1)、count(*)、count(列名)的区别
How to configure the legendary SF lander to automatically read the list without a network
Computer professional interview topic summary, general navigation
09_ UE4 advanced_ Enter the next level and reserve the blood volume
idea中设置核心配置文件的模板
Pointpillars: fast encoders for object detection from point clouds reading notes
Alkbh1
MySQL -count: the difference between count (1), count (*), and count (column name)
flask 源码梗概
Why does it system need observability?
Practice of microservice in solving Library Download business problems
[ffmpeg] add timestamp summary to video files
JDBC connection
Relevant contents about wireless communication
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复