- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在我的网站中制作一个scrollTo
功能,并将其基于this answer .因为我不想只是复制和粘贴代码,所以我正在尝试理解它。除了 scrollToX 函数
中的数学部分,我能够理解所有内容(花了我 2 天时间!)。
element.scrollTop = xFrom - (xFrom - xTo) * motion(t01);
t01 += speed * step;
我理解实际的数学原理,但我不明白它为什么会起作用。为什么这个数学会产生滚动动画?
document.getElementsByTagName('button')[0].onclick = function() {
scrollTo(0, 1000);
}
// Element to move, time in ms to animate
function scrollTo(element, duration) {
var e = document.documentElement;
if (e.scrollTop === 0) {
var t = e.scrollTop;
++e.scrollTop;
e = t + 1 === e.scrollTop-- ? e : document.body;
}
scrollToC(e, e.scrollTop, element, duration);
}
// Element to move, element or px from, element or px to, time in ms to animate
function scrollToC(element, from, to, duration) {
if (duration <= 0) return;
if (typeof from === "object") from = from.offsetTop;
if (typeof to === "object") to = to.offsetTop;
scrollToX(element, from, to, 0, 1 / duration, 20, easeOutCuaic);
}
function scrollToX(element, xFrom, xTo, t01, speed, step, motion) {
if (t01 < 0 || t01 > 1 || speed <= 0) {
element.scrollTop = xTo;
return;
}
element.scrollTop = xFrom - (xFrom - xTo) * motion(t01);
t01 += speed * step;
setTimeout(function() {
scrollToX(element, xFrom, xTo, t01, speed, step, motion);
}, step);
}
function linearTween(t) {
return t;
}
function easeInQuad(t) {
return t * t;
}
function easeOutQuad(t) {
return -t * (t - 2);
}
function easeInOutQuad(t) {
t /= 0.5;
if (t < 1) return t * t / 2;
t--;
return (t * (t - 2) - 1) / 2;
}
function easeInCuaic(t) {
return t * t * t;
}
function easeOutCuaic(t) {
t--;
return t * t * t + 1;
}
function easeInOutCuaic(t) {
t /= 0.5;
if (t < 1) return t * t * t / 2;
t -= 2;
return (t * t * t + 2) / 2;
}
function easeInQuart(t) {
return t * t * t * t;
}
function easeOutQuart(t) {
t--;
return -(t * t * t * t - 1);
}
function easeInOutQuart(t) {
t /= 0.5;
if (t < 1) return 0.5 * t * t * t * t;
t -= 2;
return -(t * t * t * t - 2) / 2;
}
function easeInQuint(t) {
return t * t * t * t * t;
}
function easeOutQuint(t) {
t--;
return t * t * t * t * t + 1;
}
function easeInOutQuint(t) {
t /= 0.5;
if (t < 1) return t * t * t * t * t / 2;
t -= 2;
return (t * t * t * t * t + 2) / 2;
}
function easeInSine(t) {
return -Mathf.Cos(t / (Mathf.PI / 2)) + 1;
}
function easeOutSine(t) {
return Mathf.Sin(t / (Mathf.PI / 2));
}
function easeInOutSine(t) {
return -(Mathf.Cos(Mathf.PI * t) - 1) / 2;
}
function easeInExpo(t) {
return Mathf.Pow(2, 10 * (t - 1));
}
function easeOutExpo(t) {
return -Mathf.Pow(2, -10 * t) + 1;
}
function easeInOutExpo(t) {
t /= 0.5;
if (t < 1) return Mathf.Pow(2, 10 * (t - 1)) / 2;
t--;
return (-Mathf.Pow(2, -10 * t) + 2) / 2;
}
function easeInCirc(t) {
return -Mathf.Sqrt(1 - t * t) - 1;
}
function easeOutCirc(t) {
t--;
return Mathf.Sqrt(1 - t * t);
}
function easeInOutCirc(t) {
t /= 0.5;
if (t < 1) return -(Mathf.Sqrt(1 - t * t) - 1) / 2;
t -= 2;
return (Mathf.Sqrt(1 - t * t) + 1) / 2;
}
Very long page.Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page.Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page. Very long page.
<button type="button">To the top</button>
最佳答案
首先,让我引用Robert Penners运动的定义
a motion is a numerical change in position over time.
上面可以写成:
pf = p0 + motion(time)
where:
- p0 = initial position
- pf = final position
运动只有在时间存在时才会存在,因此它是时间的函数。由于任何位置都是通过从初始位置开始的运动到达的,我们也可以说位置是时间的函数,所以
position = f(time)
以上是典型的y = f(x)
2d 函数,让我们暂时假设该函数是一条线并且运动发生超过 1 秒,该函数将绘制为
slope该行由
给出m = (pf - p0) / (1 - 0) = pf - p0 (1)
斜率会帮助我们找到px
什么时候0 < t < 1
因为属于直线的任意两点的斜率都相同,所以
m = (px - p0) / (t - 0) = (px - p0) / t (2)
替换 (1)
在 (2)
并找到 px
的值
pf - p0 = (px - p0) / t
t * (pf - p0) = px - p0
px = p0 + (pf - p0) * t
这完全符合您的原始方程式(减号因某种原因在您的方程式中因式分解)
另请注意:
t = 0
然后 px = p0
t = 1
然后 px = pf
现在的值为t
,我们知道 0 <= t <= 1
, 我们可以创建另一个依赖于 t
的函数例如motion(t)
具有以下要求:
motion(0) = 0
motion(1) = 1
这些条件必须适用于原始 px
方程具有正确的值,最简单的运动函数是线性或恒等函数:
const linear = t => t
另一个函数是二次函数:
const quadratic = t => t * t
位置函数将是
px = p0 + (pf - p0) * motion(t)
你会发现很多 easing functions over the internet ,最后,我们知道 t
的值(value)给定初始时间,将与耗时成比例地增加 t0
我们可以计算下一次tf
具有相同的运动定义
tf = t0 + delta(t)
现在如果你想加快/减慢你的动画,你只需要乘以delta(t)
编号 k
, 如果 k > 1
那么动画会发生得更快k < 1
动画会发生得更慢,因此:
px = p0 + (pf - p0) * motion(t0)
tf = t0 + k * delta(t)
// the final time will be the initial time for the next iteration
t0 = tf
例如,如果您希望动画在 0.5 秒内发生(快两倍)k = 2
, 如果持续时间为 2.0 秒(慢两倍)k = 0.5
, 计算 k
值的公式然后是:
k = 1 / duration
关于javascript - 理解为什么滚动动画有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39398254/
我遇到了一个奇怪的问题。我有这个: $(document).ready(function () {
我正在编写一个程序,它从列表中读取一些 ID,从中找出不同的 URL,然后将图像保存到我的 C: 驱动器中。 如果我在浏览器中导航到图像 URL,它们就会起作用。此外,如果我尝试从不同的服务器获取图像
我编写了一个 REST WCF RIA Silverlight 4.0 兼容服务,我可以从 javascript + jQuery.1.4.2.js + JSON2.js(当然,还可以从 .NET 4
我很确定这个网站实际上还没有得到回答。一劳永逸地,与 32 位有符号整数范围内的数字字符串匹配的最小正则表达式是什么,范围是 -2147483648至 2147483647 . 我必须使用正则表达式进
我有两个data.table;我想从那些与键匹配的元素中随机分配一个元素。我现在这样做的方式相当慢。 让我们具体点;这是一些示例数据: dt1<-data.table(id=sample(letter
我已经安装了 celery 、RabitMQ 和花。我可以浏览到花港。我有以下简单的工作人员,我可以将其附加到 celery 并从 python 程序调用: # -*- coding: utf-8 -
我正在使用 ScalaCheck 在 ScalaTest 中进行一些基于属性的测试。假设我想测试一个函数,f(x: Double): Double仅针对 x >= 0.0 定义的, 并返回 NaN对于
我想检查文件是否具有有效的 IMAGE_DOS_SIGNATURE (MZ) function isMZ(FileName : String) : boolean; var Signature: W
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我在工作中查看了一些代码,发现了一些我以前没有遇到过的东西: for (; ;) { // Some code here break; } 我们一直调用包含这个的函数,我最近才进去看看它是
在 Herbert Schildt 的“Java:完整引用,第 9 版”中,有一个让我有点困惑的例子。它的关键点我无法理解可以概括为以下代码: class Test { public stat
我试图编写一个函数,获取 2D 点矩阵和概率 p 并以概率 p 更改或交换每个点坐标 所以我问了一个question我试图使用二进制序列作为特定矩阵 swap_matrix=[[0,1],[1,0]]
这个问题在这里已经有了答案: Using / or \\ for folder paths in C# (5 个答案) 关闭 7 年前。 我在某个Class1中有这个功能: public v
PostgreSQL 10.4 我有一张 table : Column | Type ------------------------- id | integer| title
我正在 Postgresql 中编写一个函数,它将返回一些针对特定时区(输入)计算的指标。 示例结果: 主要问题是这只是一个指标。我需要从其他表中获取其他 9 个指标。 对于实现此目标的更简洁的方法有
我需要在 python 中模拟超几何分布(用于不替换采样元素的花哨词)。 设置:有一个装满人口许多弹珠的袋子。弹珠有两种类型,红色和绿色(在以下实现中,弹珠表示为 True 和 False)。从袋子中
我正在使用 MaterializeCSS 框架并动态填充文本输入。我遇到的一个问题是,在我关注该字段之前,valid 和 invalid css 类不会添加到我的字段中。 即使我调用 M.update
是否有重叠 2 个 div 的有效方法。 我有以下内容,但无法让它们重叠。 #top-border{width:100%; height:60px; background:url(image.jpg)
我希望你们中的一位能向我解释为什么编译器要求我在编译单元中重新定义一个静态固定长度数组,尽管我已经在头文件中这样做了。这是一个例子: 我的类.h: #ifndef MYCLASS_H #define
我正在使用旧线程发布试图解决相同问题的新代码。什么是安全 pickle ? this? socks .py from socket import socket from socket import A
我是一名优秀的程序员,十分优秀!