限制可以增加和减少字体大小的次数

Limit the amount of times font size can be increased and decreased

在我的 ruby on rails 应用程序中,我在 application.js 中使用以下 java 脚本来增大和减小文本大小:

function resizeText(multiplier) {
var elem = document.getElementById("sizeable");
var currentSize = elem.style.fontSize || 1;
elem.style.fontSize = ( parseFloat(currentSize) + (multiplier * 0.2)) + "em";
}

视图中的这些代码调用了它:

<img id="plustext" alt="INCREASE TEXT SIZE" src="images/makeTextBigger.jpg" onclick="resizeText(1)" /> | 
<img id="minustext" alt="DECREASE TEXT SIZE" src="images/makeTextSmaller.jpg" onclick="resizeText(-1)" />

代码然后根据点击的内容使带有 id 的文本变大或变小:

<tr id='sizeable'>
    <td><%= certificate.age_rating %></td>
    <td>
        <%= link_to 'Edit', edit_certificate_path(certificate) %>
        <%= link_to 'Show', certificate_path(certificate) %>
        <%= link_to 'Destroy', certificate_path(certificate), method: :delete, data: { confirm: 'Are you sure?' } %>
    </td>
</tr>

但我想做的是将字体大小可以增加和减少的次数限制为 2 次点击。

我试过了,并替换了我以前的 java 脚本,所以我的 application.js 现在看起来像这样:

$(function(){
 $('#film_release_date').datepicker({
    dateFormat: 'dd-mm-yy', altFormat: 'yy-mm-dd', 
    changeYear: true, maxDate: "5Y", minDate: "0D", changeMonth: true
  });
 });

var minSize = 1;
var maxSize = 5;

function resizeText(multiplier) {
    var elem = document.getElementById("sizeable");
    var newSize = parseFloat(elem.style.fontSize) + multiplier * 0.2;
    if (newSize < minSize) newSize = minSize;
    if (newSize > maxSize) newSize = maxSize;
    elem.style.fontSize = newSize + "em";
}

但这似乎不起作用。

如何调整我的代码来执行此操作?我对 java 脚本了解不多。

var minSize = 1;
var maxSize = 5;

function resizeText(multiplier) {
    var elem = document.getElementById("sizeable");
    var newSize = parseFloat(elem.style.fontSize) + multiplier * 0.2;
    if (newSize < minSize) newSize = minSize;
    if (newSize > maxSize) newSize = maxSize;
    elem.style.fontSize = newSize + "em";
}
<p id="plustext"   onclick="resizeText(this,1)" >Hello</p> 
<p id="minustext"  onclick="resizeText(this,-1)">World</p>

javascript:

function resizeText(elem,multiplier) {
elem.count=elem.count || 0;
if(elem.count<2){
var currentSize = elem.style.fontSize || 1;
elem.style.fontSize = ( parseFloat(currentSize) + (multiplier * 0.2)) + "em";
elem.count++;
}

}

edit1 : 如果想一键增减。

function resizeText(elem,multiplier) {
elem.count=elem.count || 0;
var currentSize = elem.style.fontSize || 1;
if(elem.count<1){
elem.style.fontSize = ( parseFloat(currentSize) + (multiplier * 0.2)) + "em";
elem.count++;
} else {
elem.style.fontSize = ( parseFloat(currentSize) - (multiplier * 0.2)) + "em";
elem.count--;
}

}

假设您实际上想控制字体的大小而不是数量clicks.I希望这可以帮助您使它比您预期的更好。

 var initialFontSize = getFontSize();
 var maxFontSize;
 var minFontSize;

 function getFontSize() {
   var temp = document.getElementById("sizeable").style.fontSize;
   if (temp) {
     return temp.substr(0, temp.indexOf("em"))
   } else {
     return 1;
   }
 }


 function resizeText(multiplier) {
   maxFontSize = parseFloat(initialFontSize) + multiplier * 0.4;
   minFontSize = parseFloat(initialFontSize) - multiplier * 0.4;

   var elem = document.getElementById("sizeable");
   var currentSize = getFontSize();
   if ((event.srcElement.id == "plustext" && currentSize < maxFontSize) || (event.srcElement.id == "minustext" && currentSize > maxFontSize)) {

     elem.style.fontSize = (parseFloat(currentSize) + (multiplier * 0.2)) + "em";

   }
 }
<div id="sizeable">sdfsfsd</div>
<img id="plustext" alt="INCREASE TEXT SIZE" src="images/makeTextBigger.jpg " onclick="resizeText(1)" />|
<img id="minustext" alt="DECREASE TEXT SIZE" src="images/makeTextSmaller.jpg" onclick="resizeText(-1)" />

在采用@Andrei Nikolaenko 提供的答案后,将其添加到我的 application.js 中:

var minSize = 1;
var maxSize = 1.5;

function resizeText(multiplier) {
  var elem = document.getElementById("sizeable");
  var currentSize = elem.style.fontSize || 1;
  var newSize = parseFloat(currentSize) + multiplier * 0.2;
  if (newSize < minSize) newSize = minSize;
  if (newSize > maxSize) newSize = maxSize;
  elem.style.fontSize = newSize + "em";
}

主要区别在于我在行 var newSize = parseFloat(currentSize) + multiplier * 0.2;

中使用变量 currentSize

感谢所有帮助过的人。