目前,我可以改变字体大小一次:减少(—A),默认(A)或增加(A+).如果我点击按钮不止一次,它什么也不做.

我想要的是能够增加/减少字体大小,但限制它点击两次,使字体大小不超过—/+150%.

示例:当我点击-A时,字体大小减小到0.75em.我希望这进一步减少到0.5em,如果有人再次点击它,但这应该是最小大小,它可以go .同样,当点击A+时,字体大小会增加到1.25em,但如果用户多次点击,字体大小不会超过1.5em.

Demo: 100

Html:

<button type="button" onclick="changeSizeByBtn(0.75)" name="btn1">-A</button>
<button type="button" onclick="changeSizeByBtn(1)" name="btn2">A</button>
<button type="button" onclick="changeSizeByBtn(1.25)" name="btn3">A+</button>
<hr />
<div id="container">
  <h1>This is the title</h1>
  <h2>This is a sub title</h2>
  <p>I need to increase the texts here by clicking on A+ button and decrease them by clicking -A button above. <br />
The problem here is that it increase/decrease only once. I want it to increase or decrease twice by say 25%. Not Thrice but just twice. How can I go about doing this?</p>
</div>

JS:

var cont = document.getElementById("container");
function changeSizeByBtn(size) {
  cont.style.fontSize = size + "em";
}

Css:以下内容:

div {
  padding: 20px;
  border: 5px solid red;  
}
h1 {
  font-size: 2em;
}
h2 {
  font-size: 1.5em;
}
p {
  font-size: 1em;
}

推荐答案

如果sizeChange1,则意味着按下了重置按钮以将文本恢复为正常大小.

fontSizeMultiplier是通过增加sizeChange来更新的,它可以是正数或负数,这取决于你是增加还是减少大小.

分别跟踪增加和减少计数器,增加计数器为numIncreases,减少计数器为numDecreases.

使用此代码,限制计数器

if (numIncreases >= 2 && sizeChange < 0) {
  numIncreases = 0;
} else if (numDecreases >= 2 && sizeChange > 0) {
  numDecreases = 0;
}

为了防止用户减少文本太多或相反,

最后像你已经做的那样应用字体大小的改变

var cont = document.getElementById("container");
var fontSizeMultiplier = 1;
var numIncreases = 0;
var numDecreases = 0;

function changeSizeByBtn(sizeChange) {
  if (sizeChange === 1) {
    fontSizeMultiplier = 1;
    numIncreases = 0;
    numDecreases = 0;
  } else {
    fontSizeMultiplier += sizeChange;

    if (fontSizeMultiplier < 0.5) {
      fontSizeMultiplier = 0.5;
    } else if (fontSizeMultiplier > 1.5) {
      fontSizeMultiplier = 1.5;
    }

    if (sizeChange > 0) {
      numIncreases++;
    } else if (sizeChange < 0) {
      numDecreases++;
    }

    if (numIncreases >= 2 && sizeChange < 0) {
      numIncreases = 0;
    } else if (numDecreases >= 2 && sizeChange > 0) {
      numDecreases = 0;
    }
  }

  cont.style.fontSize = fontSizeMultiplier + "em";
}
div {
  padding: 20px;
  border: 5px solid red;  
}
h1 {
  font-size: 2em;
}
h2 {
  font-size: 1.5em;
}
p {
  font-size: 1em;
}
<button type="button" onclick="changeSizeByBtn(-0.25)" name="btn1">-A</button>
<button type="button" onclick="changeSizeByBtn(1)" name="btn2">A</button>
<button type="button" onclick="changeSizeByBtn(0.25)" name="btn3">A+</button>
<hr />
<div id="container">
  <h1>This is the title</h1>
  <h2>This is a sub title</h2>
  <p>I need to increase the texts here by clicking on A+ button and decrease them by clicking -A button above. <br />
The problem here is that it increase/decrease only once. I want it to increase or decrease twice by say 25%. Not Thrice but just twice. How can I go about doing this?</p>
</div>

Javascript相关问答推荐

使用JavaScript单击上一个或下一个特定按钮创建卡滑动器以滑动单个卡

仅圆角的甜甜圈图表

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

Angular:动画不启动

如何粗体匹配的字母时输入搜索框使用javascript?

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

从Node JS将对象数组中的数据插入Postgres表

如何在 cypress 中使用静态嵌套循环

优化Google Sheet脚本以将下拉菜单和公式添加到多行

Html文件和客户端存储的相关问题,有没有可能?

以Angular 实现ng-Circle-Progress时出错:模块没有导出的成员

Google脚本数组映射函数横向输出

使用Reaction窗体挂钩注册日历组件

Next.js无法从外部本地主机获取图像

JWT Cookie安全性

为什么我不能使用其同级元素调用和更新子元素?

select 2-删除js插入的项目将其保留为选项

material UI自动完成全宽

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

不允许在对象文本中注释掉的属性