简体   繁体   English

增大/减小页面中所有元素的字体大小

[英]Increase/Decrease font size of all elements in page in angular

I have a piece of code in my angular component: 我的角度组件中有一段代码:

    IncreaseFontSize()
    {
        let FontSize:any = document.body.style.fontSize;
        if(!FontSize)
        {
            FontSize = 1.4;
        }
        else
        {
            FontSize = parseFloat(FontSize);
        }
        document.body.style.fontSize = (FontSize+0.1) + 'rem';
    }


    DecreaseFontSize()
    {
        let FontSize:any = document.body.style.fontSize;
        if(!FontSize)
        {
            FontSize = 1.4;
        }
        else
        {
            FontSize = parseFloat(FontSize);
        }
        document.body.style.fontSize = (FontSize-0.1) + 'rem';
    }

These functions are called upon clicking two buttons, but it is not changing the font size of elements which have a font size already defined for them. 单击两个按钮可调用这些功能,但不会更改已为其定义了字体大小的元素的字体大小。

I need to increase/decrease the font size of all elements , ie if a label has font size 12px, a paragraph with font size of 11px, a div with font size 1.2rem the should increase their font size by 0.1 rem on click of the increase button. 我需要增加/减少所有元素的字体大小 ,即,如果标签的字体大小为12px,段落的字体大小为11px,div的字体大小为1.2rem,则在单击时应将其字体大小增加0.1 rem。增加按钮。

How should I proceed with the approach? 我应该如何进行? Thanks in advance. 提前致谢。

Try something like this. 尝试这样的事情。 You can see the div with px does not grow, while other one does. 您可以看到pxdiv不会增长,而其他的会增长。

 var fontSize = 16; function IncreaseFontSize() { fontSize = (fontSize + 1); document.body.style.fontSize = fontSize + 'px'; } function DecreaseFontSize() { fontSize = (fontSize - 1); document.body.style.fontSize = fontSize + 'px'; } 
 body, .font-constant { font-size: 16px; } .font-varies { font-size: 1em; } 
 <button onclick="IncreaseFontSize()">+</button> <button onclick="DecreaseFontSize()">-</button> <div class="font-varies"> Hello </div> <div class="font-constant"> how are you? </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM