防止jQuery在$ .Animate()中使用科学计数法? [英] Preventing jQuery from using Scientific Notation in $.Animate()?

查看:328
本文介绍了防止jQuery在$ .Animate()中使用科学计数法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止jQuery使用科学记数法-jQuery.animate()函数中的 1.2e + 07 而不是 12,000,000 ?

How am I able to prevent jQuery from using scientific notation - 1.2e+07 instead of 12,000,000 within the jQuery.animate() function?

使用:$('body').css({ backgroundPositionX: 12000000 + 'px' });

转换为: background-position-x: 1.2e+07px;

所需结果: background-position-x: 12000000px;

一旦数字达到100万(1,000,000),这种情况就会开始发生:

This starts occuring as soon as the number hits 1 million (1,000,000):

这又导致我的应用程序表现异常.我想要纯净,简单的整数-这些科学废话都不是!我环顾四周,但找不到与jQuery库相关的任何内容.只有纯JS函数.

This, in turn, causes my application to behave strangely. I want pure, simple integer numbers -- none of this scientific nonsense! I've looked around but I cannot find anything related to the jQuery library. Only pure JS functions.

注1:不仅是动画,而且还有$ .css().我认为其他jQuery函数也类似.

Note 1: It's not just animate, but $.css() as well. I assume other jQuery functions are similar also.

注2:我不认为是浏览器来执行此操作,因为如果我手动输入该值,则在您达到通常的最大32位整数之前,它都可以正常工作

Note 2: I don't think it's the browser that does it because if I enter the value in manually it works just fine until you hit the usual max 32 bit integer

为什么会出现问题:

12000321 转换为: 1.20003e + 07

然后将 1.20003e + 07 转换回整数时,我得到: 12000300

When I then convert 1.20003e+07 back to an integer I get: 12000300

进入科学计数法"时,步长为100秒& amp;不是1s,不够具体.

When you get to Scientific Notation, the step size is in the 100s & not 1s and not specific enough.

感谢您考虑我的问题

推荐答案

哇...花了我很长时间,但我终于设法找到一种解决方法.

Wow... took me a long time but I finally managed to work out a way of doing this.

经过大量测试,我注意到将直接字符串传递给$ .attr()并没有将数字转换为科学计数法.

After lots of testing I noticed that passing a direct string to $.attr() did not convert the numbers to the scientific notation.

我创建了一个自定义动画,并将样式属性手动设置为字符串.

I created a custom animation and manually set the style attribute as a string.

似乎可以正常工作&会以个位数而不是100s增长!!

Seems to be working & going up in single digits not 100s!!

$({
    x: this.x,
    y: this.y
}).animate({
    x: x,
    y: y
}, {
    easing: this.motion_type,
    duration: this.duration,
    step: function(now, fx) {

        var current_styles = $('.area').attr('style'),
            current_styles = string = current_styles.split(" ");

        var x = parseFloat(current_styles[1].replace('px', '').replace(';', ''));
        var y = parseFloat(current_styles[2].replace('px', '').replace(';', ''));

        if ('x' === fx.prop) {
            x = now;
        } else if ('y' === fx.prop) {
            y = now;
        }

        $('.area').attr({ style: 'background-position: ' + x + 'px ' + y + 'px;' });

    },
    complete: function() {
        t.stopNow();
    }
});

这篇关于防止jQuery在$ .Animate()中使用科学计数法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆