Please help.
function fontDown() {
var curSize = parseInt($('.box').css('font-size'));
curSize--;
$('.box').css('font-size', curSize + "pt").css('line-height', curSize + "pt");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="fontDown()">button</button>
<div class="box" style="font-size: 14pt; line-height: 14pt;">sometext</div>
I expect the font-size to decrease each time I press the button. But, it only increases. Not only that but the font size increases by about 5 or 7 points each time the button is pressed.
3
Answers
use this:
it easy
operators
+=
,*=
,-=
,/=
should be used in jquery.css('font-size')
returns a string like18.6667px
which is apx
value notpt
.Changing your code to use
px
instead will reduce the size as desired.You need to do a px to pt unit conversion.
The px to pt ratio is 4/3, so you get a bigger value in px unit, even if you decrease it by one. Then you assign this largest value with a pt unit, which produces a largest font size each time.