【发布时间】:2018-05-17 15:42:28
【问题描述】:
我想使用我在 github 上看到的这段代码,但我不知道如何在我的 HTML 上应用这段代码,以产生滚动效果。
关键是,我不知道如何使用这段代码运行
来源https://gist.github.com/andjosh/6764939
document.getElementsByTagName('button')[0].onclick = function () {
scrollTo(document.body, 0, 1250);
}
function scrollTo(element, to, duration) {
var start = element.scrollTop,
change = to - start,
currentTime = 0,
increment = 20;
var animateScroll = function(){
currentTime += increment;
var val = Math.easeInOutQuad(currentTime, start, change, duration);
element.scrollTop = val;
if(currentTime < duration) {
setTimeout(animateScroll, increment);
}
};
animateScroll();
}
//t = current time
//b = start value
//c = change in value
//d = duration
Math.easeInOutQuad = function (t, b, c, d) {
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
};
【问题讨论】:
-
您的 HTML 中有
<button>元素吗?点击它会发生什么? -
我试过了兄弟,但没有任何反应。
-
谁能提供一个例子?关于这是如何工作的。这将是一个很大的帮助
-
投票结束,因为您没有包含重现您遇到的问题所需的最少代码。
标签: javascript html animation