最近我曾经提出这个问题: 想要了解Animate函数(计算和步进) 并得到了答复。
我试图删除jQuery的不必要代码,只保留jQuery动画函数。
如果有人能提供仅包含其技术的jQuery动画函数,我将非常感激。
最近我曾经提出这个问题: 想要了解Animate函数(计算和步进) 并得到了答复。
我试图删除jQuery的不必要代码,只保留jQuery动画函数。
如果有人能提供仅包含其技术的jQuery动画函数,我将非常感激。
创建动画其实很简单。设置一个时间间隔来更改元素的CSS属性,让函数递归运行:
var distance = 300,
frames = 30,
current_frame = 0,
time = 1000,
delta = Math.floor(distance / frames),
$element = $('#my-element'),
my_timer;
my_timer = setInterval(function () {
//make sure the end of the animation has not been reached
if (current_frame < frames) {
//get the current property you want to animate and add to it the `delta` variable which is how much the property should change in each iteration
var left = parseInt($element.css('left').replace('px', ''), 10);
$element.css('left', (left + delta));
} else {
//the end of the animation has been reached so stop the interval
clearInterval(my_timer);
}
current_frame++;
}, Math.floor(time / frames));
.css()
函数,但你可以删除那一行,并放入任何你想要的JavaScript来操作元素。同时相关的是https://dev59.com/4XA75IYBdhLWcg3wuLjD#3143256,它讨论了创建自定义构建的问题。
.animate
方法依赖于 jQuery 的其他部分;你为什么想要它“独立”? - Shad.animate
函数的实现,你是否已经查看了jQuery源代码? - David