使用 JS 更改字体大小时忽略小数

发布于 2024-12-28 17:29:35 字数 1047 浏览 3 评论 0原文

我正在尝试使用此代码添加一些文本调整大小功能:

$('#text-resize a').click(function(){
    var currentValue = $('#page-body').css('fontSize');
    var currentSize = parseFloat(currentValue, 10);
    var fontUnit = currentValue.slice(-2);
    var newSize = currentSize;
    if ($(this).attr('rel') == 'decrease'){
        if (currentSize > 13){
            var newSize = currentSize / 1.2;
        }
    }
    else if ($(this).attr('rel') == 'increase'){
        if (currentSize < 19){
            var newSize = currentSize * 1.2;
        }
    }
    else {
        newSize = 1;
        fontUnit = 'em';
    }
    newSize += fontUnit;
    $('#page-body').css('fontSize', newSize);
    return false;
});

我知道这不是最干净的代码,但我的问题是在某些时候(获取或设置 #page-body 的字体大小) 例如,我从 16px 的值开始(来自我的 CSS 文件),当我增加它时,

它会计算为 19.2px 如果我再次增加,则会出现 currentValue 的警报(我忽略了警报)。 )说这是19px。

谁能告诉我为什么小数位被忽略以及如何防止这种情况?

如果有帮助,我的起始 CSS 包括:

body { font-size: 16px; }
#page-body { font-size: 1em; }

谢谢

I'm trying to add some text resize functionality using this code:

$('#text-resize a').click(function(){
    var currentValue = $('#page-body').css('fontSize');
    var currentSize = parseFloat(currentValue, 10);
    var fontUnit = currentValue.slice(-2);
    var newSize = currentSize;
    if ($(this).attr('rel') == 'decrease'){
        if (currentSize > 13){
            var newSize = currentSize / 1.2;
        }
    }
    else if ($(this).attr('rel') == 'increase'){
        if (currentSize < 19){
            var newSize = currentSize * 1.2;
        }
    }
    else {
        newSize = 1;
        fontUnit = 'em';
    }
    newSize += fontUnit;
    $('#page-body').css('fontSize', newSize);
    return false;
});

I know it's not the cleanest code, but my problem is that at some point (either getting or setting the font size for #page-body decimal points are being lost.

For example, I start with a value of 16px (from my CSS file) and when I increase it gets calculated to 19.2px. If I increase again, an alert of the currentValue (I have left out the alert) says it's 19px.

Can anyone tell me why decimal places are being ignored and how to prevent that?

If it helps, my starting CSS includes:

body { font-size: 16px; }
#page-body { font-size: 1em; }

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2025-01-04 17:29:35

字体大小只能是整数像素,因此当您设置字体大小时,浏览器会将无效的字体大小转换为它可以使用的字体大小。下次获取它时,它是一个整数。

由于您要在由 ID 标识的元素上设置字体大小,因此您应该将大小存储在变量中,并且仅设置(从不获取)元素 css 字体大小。

例如,请参阅 http://jsfiddle.net/Qfzpb/

A font size can only be a whole number of pixels, so when you set the font-size the browser casts the invalid font size to something it can use. The next time you get it, it's an integer number.

Since you're setting the font size on an element identified by ID, you should store the size in a variable and only set (never get) the element css font size.

For example, see http://jsfiddle.net/Qfzpb/

森末i 2025-01-04 17:29:35

浏览器之间的子像素渲染不一致。

字体大小可以是,并且可以是。是一个<数字> (带或不带小数点)紧跟单位标识符(例如,px、em 等)。
http://www.w3.org/TR/CSS21/fonts .html#font-size-props

CSS 3 表示“UA 应支持合理有用的范围和精度。”开放解释的措辞为“合理”和“有用”:
http://dev.w3.org/csswg/css3-values/#numeric -类型

Sub-pixel rendering is inconsistent between browsers.

Font size can be a <length>, and a <length> is a <number> (with or without a decimal point) immediately followed by a unit identifier (e.g., px, em, etc.).
http://www.w3.org/TR/CSS21/fonts.html#font-size-props

CSS 3 says " UAs should support reasonably useful ranges and precisions. " Open-to-interpretation wording as 'reasonable' and 'useful':
http://dev.w3.org/csswg/css3-values/#numeric-types

迷途知返 2025-01-04 17:29:35
$('#page-body').css('fontSize')

这可能会返回不同的值(取决于所使用的浏览器)。

通常(在现代浏览器中)jQuery 通过使用 getCompulatedStyle()
这里的返回值是浏览器使用的值,而不是style-property设置的值。
您可以使用它:

var currentValue = document.getElementById('page-body').style.fontSize
                      ||
                   $('#page-body').css('fontSize');

查看小提琴以确定差异:http://jsfiddle.net/doktormolle /DMWhh/

$('#page-body').css('fontSize')

This may return different values(depending on the used browser).

Usually (in modern browsers) jQuery gets the value by using getComputedStyle() .
The returned value here is the value used by the browser, not the value the style-property is set to.
You may use this instead:

var currentValue = document.getElementById('page-body').style.fontSize
                      ||
                   $('#page-body').css('fontSize');

See the fiddle to determine the differences: http://jsfiddle.net/doktormolle/DMWhh/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文