字体大小更改按钮

huangapple go评论58阅读模式
英文:

Font size changing buttons

问题

I see that you're trying to implement font size adjustment buttons on a website using JavaScript. Here's the translated code you provided:

我尝试在网站上实现按钮,以允许用户增加/减小字体大小。
这是网站链接:https://font-size-d27117.webflow.io/
我在字体和图像上都使用REM。我想只更改字体大小,而不更改图像。

我找到了脚本。它似乎很简单,但不起作用。

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>

$('#font-size-plus').on('click', function() 
{
  var fontSize = $('body').css('font-size');
  console.log(fontSize);
  var newFontSize = parseInt(fontSize) + 1;

  $('body').css('font-size', newFontSize + 'px')
})

$('#font-size-minus').on('click', function() 
{
  var fontSize = $('body').css('font-size');
  var newFontSize = parseInt(fontSize) - 1;

  $('body').css('font-size', newFontSize + 'px')
})

$('#_reset').on('click', function() {
  $('body').css('font-size', '1')
})
</script>

在上面的脚本中,我看到了px,而不是rems。

我尝试将PX更改为REM,并从1px更改为0.1rem

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>

$('#font-size-plus').on('click', function() 
{
  var fontSize = $('body').css('font-size');
  console.log(fontSize);
  var newFontSize = parseFloat(fontSize) + 0.1;

  $('body').css('font-size', newFontSize + 'rem')
})

$('#font-size-minus').on('click', function() 
{
  var fontSize = $('body').css('font-size');
  var newFontSize = parseFloat(fontSize) - 0.1;

  $('body').css('font-size', newFontSize + 'rem')
})

$('#_reset').on('click', function() {
  $('body').css('font-size', '1rem')
})
</script>

这些脚本都不起作用。

有人可以帮忙吗?

Please note that I've translated the code for you. If you have any specific questions or need further assistance with this code, feel free to ask.

英文:

I'm trying to implement buttons in order to allow users to increase/decrease font size on a website.
This is the website: https://font-size-d27117.webflow.io/
I use REM everywhere, for font and for images. I'd like to change only font sizes, but not images.

I've found the script. It seems to be simple but doesn't work.

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(&#39;#font-size-plus&#39;).on(&#39;click&#39;, function() 
{
var fontSize = $(&#39;body&#39;).css(&#39;font-size&#39;);
console.log(fontSize);
var newFontSize = parseInt(fontSize) + 1;
$(&#39;body&#39;).css(&#39;font-size&#39;, newFontSize + &#39;px&#39;)
})
$(&#39;#font-size-minus&#39;).on(&#39;click&#39;, function() 
{
var fontSize = $(&#39;body&#39;).css(&#39;font-size&#39;);
var newFontSize = parseInt(fontSize) - 1;
$(&#39;body&#39;).css(&#39;font-size&#39;, newFontSize + &#39;px&#39;)
})
$(&#39;#_reset&#39;).on(&#39;click&#39;, function() {
$(&#39;body&#39;).css(&#39;font-size&#39;, &#39;1&#39;)
})
&lt;/script&gt;

In the above script I see px instead of rems.

I tried to change PX to REM and step from 1px to 0.1rem

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(&#39;#font-size-plus&#39;).on(&#39;click&#39;, function() 
{
var fontSize = $(&#39;body&#39;).css(&#39;font-size&#39;);
console.log(fontSize);
var newFontSize = parseInt(fontSize) + 0.1;
$(&#39;body&#39;).css(&#39;font-size&#39;, newFontSize + &#39;rem&#39;)
})
$(&#39;#font-size-minus&#39;).on(&#39;click&#39;, function() 
{
var fontSize = $(&#39;body&#39;).css(&#39;font-size&#39;);
var newFontSize = parseInt(fontSize) - 0.1;
$(&#39;body&#39;).css(&#39;font-size&#39;, newFontSize + &#39;rem&#39;)
})
$(&#39;#_reset&#39;).on(&#39;click&#39;, function() {
$(&#39;body&#39;).css(&#39;font-size&#39;, &#39;1&#39;  + &#39;rem&#39;)
})
&lt;/script&gt;

None of these scripts work.

Could anyone help?

答案1

得分: 0

你有两个主要问题:

  1. 事件处理程序没有被添加,因为脚本立即运行,但当它运行时,+/-按钮元素还不存在。解决方法是将你的 jQuery 代码包装在 $(() => { ... }) 中,这会使它在DOM加载后才运行。
  2. 你设置了body的字体大小,而不是html的字体大小。rem单位基于根大小,对于HTML文档来说,根元素是html

你的代码中还有一些其他问题(无效的HTML等),但下面是一个简化的版本可以工作:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(() => {
  $('#font-size-plus').on('click', function() {
    var fontSize = $('html').css('font-size');
    var newFontSize = parseInt(fontSize) + 1;

    $('html').css('font-size', newFontSize + 'px');
  });

  $('#font-size-minus').on('click', function() {
    var fontSize = $('html').css('font-size');
    var newFontSize = parseInt(fontSize) - 1;

    $('html').css('font-size', newFontSize + 'px');
  });

  $('#_reset').on('click', function() {
    $('html').css('font-size', '16px');
  });
});

<!-- language: lang-css -->

.font-16px {
  font-size: 16px;
}

.font-1rem {
  font-size: 1rem;
}

.font-2rem {
  font-size: 2rem;
}

.font-3rem {
  font-size: 3rem;
}

<!-- language: lang-html -->

<html style="font-size: 16px">
  <head>
    <meta charset="utf-8">
    <title>font size</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  </head>
  <body>
    <div class="font-16px">
      <button id="font-size-plus">+</button>
      <button id="font-size-minus">-</button>
      <button id="_reset">Reset</button>
    </div>
    <div class="font-1rem">1 rem</div>
    <div class="font-2rem">2 rem</div>
    <div class="font-3rem">3 rem</div>
  </body>
</html>

<!-- end snippet -->

请注意,设置html的字体大小会影响所有使用rem值的测量,无论它们是字体大小、图像等等。你可以在控制按钮本身所在的div中看到这个区别,它将其字体大小设置为px,因此始终保持相同的字体大小。

英文:

You have 2 main problems:

  1. The event handlers aren't getting added, as the script runs immediately but the +/- button elements aren't present when it runs. The fix is to wrap your jQuery code in $(() =&gt; { ... }), which causes it to run only once the DOM is loaded.
  2. You're setting the font size of body, not html. rem units are based on the root size, which for an HTML document is the html element.

There are a few other issues in your code (invalid HTML etc.), but here's a simplified version that works:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(() =&gt; {
$(&#39;#font-size-plus&#39;).on(&#39;click&#39;, function() {
var fontSize = $(&#39;html&#39;).css(&#39;font-size&#39;);
var newFontSize = parseInt(fontSize) + 1;
$(&#39;html&#39;).css(&#39;font-size&#39;, newFontSize + &#39;px&#39;);
});
$(&#39;#font-size-minus&#39;).on(&#39;click&#39;, function() {
var fontSize = $(&#39;html&#39;).css(&#39;font-size&#39;);
var newFontSize = parseInt(fontSize) - 1;
$(&#39;html&#39;).css(&#39;font-size&#39;, newFontSize + &#39;px&#39;);
});
$(&#39;#_reset&#39;).on(&#39;click&#39;, function() {
$(&#39;html&#39;).css(&#39;font-size&#39;, &#39;16px&#39;);
});
});

<!-- language: lang-css -->

.font-16px {
font-size: 16px;
}
.font-1rem {
font-size: 1rem;
}
.font-2rem {
font-size: 2rem;
}
.font-3rem {
font-size: 3rem;
}

<!-- language: lang-html -->

&lt;html style=&quot;font-size: 16px&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;font size&lt;/title&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;font-16px&quot;&gt;
&lt;button id=&quot;font-size-plus&quot;&gt;+&lt;/button&gt;
&lt;button id=&quot;font-size-minus&quot;&gt;-&lt;/button&gt;
&lt;button id=&quot;_reset&quot;&gt;Reset&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;font-1rem&quot;&gt;1 rem&lt;/div&gt;
&lt;div class=&quot;font-2rem&quot;&gt;2 rem&lt;/div&gt;
&lt;div class=&quot;font-3rem&quot;&gt;3 rem&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Note that setting the font size on html affect all measurements that use rem values, regardless of whether they're font sizes, images, etc. You can see this difference in the div housing the control buttons themselves, which sets its font size in px instead so always remains at the same font size.

答案2

得分: 0

我已经实施了 @Lionel Rowe 的建议。
似乎有效。
最终效果:font-size-d27117.webflow.io

英文:

I've implemented @Lionel Rowe advice.
IT seems to work.
Final effect: font-size-d27117.webflow.io

huangapple
  • 本文由 发表于 2023年6月26日 15:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定