zoukankan      html  css  js  c++  java
  • JavaScript 控制字体大小设置

    在做公司的官网的时候,新闻内页会有一个让浏览者自己调整文字大小的功能,因此在这个空闲时间,把这个功能整理下来:

     1     function setFontSize (id,content,params){
     2             var oTarget = document.getElementById(id),
     3                 content = document.getElementById(content),
     4                 size = params.size || 14,
     5                 maxSize = params.maxSize || 20,
     6                 step = params.step || 2;
     7                 oBtn = '<input type="button" value="+"/><input type="button" value="-" />';
     8                 oBtn1 = null,
     9                 oBtn2 = null;
    10 
    11                 oTarget.innerHTML = oBtn;
    12                 oBtn1 = oTarget.childNodes[0];
    13                 oBtn2 = oTarget.childNodes[1];
    14 
    15                 oBtn1.onclick=function(){
    16                     if(size+step <= maxSize){
    17                         size+=step;
    18                     }else{
    19                         size = maxSize;
    20                         this.className+=' disabled';
    21                         this.disabled = true;
    22                     }
    23                     oBtn2.className.replace('disabled','');
    24                     oBtn2.disabled = false;
    25                     content.style.fontSize = size +'px';
    26                 }
    27                 oBtn2.onclick=function(){
    28                     if(size-step >= 12){ 
    29                         size-=step;
    30                     }else{
    31                         size = 12;
    32                         this.className+=' disabled'
    33                         this.disabled = true;
    34                     }
    35                     oBtn1.className.replace('disabled','');
    36                     oBtn1.disabled = false;
    37                     content.style.fontSize = size +'px';
    38             }
    39     }

    调用方式:

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

    提示:可以通过font-size:0的方式来隐藏Input元素的value值,然后自定义按钮的样式。

  • 相关阅读:
    linux搭建maven私服
    sgu438-The_Glorious_Karlutka_River
    [模板] 长链剖分
    bzoj3277-串
    [模板] 矩阵树定理
    [模板] 最短路/差分约束
    luogu2597-[ZJOI2012]灾难 && DAG支配树
    bzoj1150-[CTSC2007]数据备份Backup
    bzoj2152-[国家集训队]聪聪可可
    [模板] 树的重心/点分治/动态点分治
  • 原文地址:https://www.cnblogs.com/HCJJ/p/5573917.html
Copyright © 2011-2022 走看看