Home  >  Article  >  Web Front-end  >  How to set font size in javascript

How to set font size in javascript

藏色散人
藏色散人Original
2021-10-26 15:37:367333browse

How to set the font size in JavaScript: 1. Create the "function setFontSize (id, content, params) {...}" method; 2. Pass "setFontSize (id, contentid, {...}) ” method can be debugged.

How to set font size in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to set font size in javascript?

JavaScript method of controlling font size setting

When making the company's official website, the news page will have a function that allows viewers to adjust the text size themselves, so during this free time Time, sort out this function:

function setFontSize (id,content,params){
      var oTarget = document.getElementById(id),
        content = document.getElementById(content),
        size = params.size || 14,
        maxSize = params.maxSize || 20,
        step = params.step || 2;
        oBtn = &#39;<input type="button" value="+"/><input type="button" value="-" />&#39;;
        oBtn1 = null,
        oBtn2 = null;

        oTarget.innerHTML = oBtn;
        oBtn1 = oTarget.childNodes[0];
        oBtn2 = oTarget.childNodes[1];

        oBtn1.onclick=function(){
          if(size+step <= maxSize){
            size+=step;
          }else{
            size = maxSize;
            this.className+=&#39; disabled&#39;;
            this.disabled = true;
          }
          oBtn2.className.replace(&#39;disabled&#39;,&#39;&#39;);
          oBtn2.disabled = false;
          content.style.fontSize = size +&#39;px&#39;;
        }
        oBtn2.onclick=function(){
          if(size-step >= 12){ 
            size-=step;
          }else{
            size = 12;
            this.className+=&#39; disabled&#39;
            this.disabled = true;
          }
          oBtn1.className.replace(&#39;disabled&#39;,&#39;&#39;);
          oBtn1.disabled = false;
          content.style.fontSize = size +&#39;px&#39;;
      }
  }

Calling method:

setFontSize(id,contentid,{size:,maxSize,step:});
/*
 * id :用于存放增加或减小两个按钮的父级盒子的id。
 * contentid: 存放内容的id。
 * {} 一个对象,用于提供设置的参数。
     |— szie : 字体起始(默认)大小。
     |— maxSize : 最大字体。
     |— step : 增长的步长值。
*/

Tip: You can hide the value of the Input element through font-size:0, and then customize it The style of the button.

[Recommended learning: javascript basic tutorial]

The above is the detailed content of How to set font size in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn