大家好!
我们今天要实作让视窗能平滑地滚动到锚点。
我们进入今天的主题吧!
(function (duration) {
Felix('a[href^="#"]').on('click', scroll);
function ease(t, b, c, d) {
if ((t /= d / 2) < 1) return c / 2 * t * t + b;
return -c / 2 * ((--t) * (t - 2) - 1) + b;
}
function scroll(e) {
e.preventDefault();
const anchor = Felix(encodeURI(this.hash))[0];
const aPos = anchor.getBoundingClientRect().top;
const wPos = window.pageYOffset;
let startTime = null;
function animation(currentTime) {
if (startTime === null) startTime = currentTime;
const time = currentTime - startTime;
window.scrollTo(0, ease(time, wPos, aPos, duration));
if (time < duration) requestAnimationFrame(animation);
else {
anchor.tabIndex = -1;
anchor.focus();
}
}
requestAnimationFrame(animation);
}
})(1000);
上方的 ease
函式是用来调整页面滚动的加速度,每次呼叫 animation
函式时就会提供 Y 轴的座标。
关於更多加速度函式,可参考 easings.net 和 spicyyoghurt.com 上的说明。
<header id="header">
<a href="#footer">Footer</a>
</header>
<footer id="footer">
<a href="#header">Header</a>
</footer>
差不多也到尾声了。
如果对文章有任何疑问,也欢迎在下方提问和建议!
我是 Felix,我们明天再见!
>>: Day 28 - [Android APP] 06-RecyclerView与资料显示
前面我们介绍了影像辨识的资料前处理方法,今天就要开始教大家架设一个神经网路,并将资料丢入来看看实际的...
何谓JavaScript? 根据MND定义,JavaScript 是一种脚本,也能称它为程序语言,可...
keyframes 定义关键影格的各自状态,不同关键影格组成动态变化,但我们在定义keyframes...
接续上一章,Governance and Management of Enterprise IT (...
昨天教大家怎麽简单的在 LINE Notify 上面看到 CloudWatch 的 Alarm,不过...