强制jQuery动画使用整数值 [英] Force jQuery animations to use integer values

查看:107
本文介绍了强制jQuery动画使用整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方式(保持向前兼容性)强制jQuery动画在动画中只使用整数值?

Is there a way (keeping forward-compatibility in mind) to force jQuery animations to use only integer values amidst an animation?

我动画元素的宽度。

<div style="width: 25px">...</div>

我在检查器中看到的元素处于动画的中间,

I look at the element in the inspector while it's in the middle of animating, and often the value has massive granularity decimalwise.

<div style="width: 34.24002943013px">...</div>

只要达到目标,它就会冻结为整数。

It only 'calms down' to integers once it's reached its goal.

<div style="width: 50px">...</div>

通常我不会打扰自己,因为它显然是功能性的。

Usually I wouldn't bother myself about this as it's obviously functional.

但是,在这个项目中,我必须始终保持像素完美,我担心使用分数像素宽度(或任何其他属性以像素衡量)会在浏览器之间呈现不一致。我知道 letter-spacing 对于一个

However, in this project it's imperative that I stay pixel-perfect at all times, and I'm afraid using fractional pixel widths (or any other attribute measured in pixels) would render inconsistently between browsers. I know letter-spacing for one does.

有一种方法可以确保即使在动画过程中jQuery也只使用不含十进制的值?

Is there a way to make sure jQuery only uses decimal-free values even in the middle of animation?

编辑:我觉得我应该澄清;我不是只使用 .animate({width:50})。我的大部分动画都是jQuery UI驱动的 .hide() s和 .show()我正在寻找应该显然也与那些工作。

I feel like I should clarify; I'm not just using .animate({width:50}). Most of my animations are jQuery UI -powered .hide()s and .show()s, and the solution I'm looking for should obviously also work with those.

推荐答案

对我来说这是正确的方式:

For me this worked in the right way:

$("div").animate({ width: 50}, {
    duration: 1000,
    step: function(now, fx){
        fx.now = parseInt(now);
    }
});​

这篇关于强制jQuery动画使用整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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