javascript - 如何在纯 JavaScript 中添加平滑滚动动画?
问题描述
我正在使用纯 JavaScript 进行垂直滚动。我需要这个滚动才能有流畅的行为。
function move_up(scroll_nav)
{
var container = document.getElementById(scroll_nav);
upScroll(container,'up',40,50,10);
}
function move_down(scroll_nav)
{
var container = document.getElementById(scroll_nav);
upScroll(container,'down',40,50,10);
}
function upScroll(element,direction,speed,distance,step)
{
scrollAmount = 0;
var slideTimer = setInterval(function(){
if(direction == 'up'){
element.scrollTop -= step;
} else {
element.scrollTop += step;
}
scrollAmount += step;
if(scrollAmount >= distance){
window.clearInterval(slideTimer);
}
}, speed);
}
我需要一个类似于此链接中的动画函数,但没有 jquery。 https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_smooth_scroll_jquery
我需要一个动画功能,请提供解决方案。
解决方案
element.scrollTo({ top: ..., behavior: 'smooth' });
推荐阅读
- android - 获取 METHOD GET JSON 改造的问题
- microsoft-graph-api - /security/alerts 未从 Azure 身份保护 (IPC) 返回
- docker - 当我在 dockerfile 中公开一个端口并且它成功构建但在服务器中没有响应时
- r - 如何紧凑地定义一个列表?
- typescript - 如何通过 vscode 扩展为特定文件提供智能感知?
- plsql - sys_refcursor 结果不可见
- laravel - 设置用户访问资源的权限 laravel spatie
- c# - Automapper -> 将对象的命名属性映射到数组属性
- angular - 卡片对象在放入自己的组件后发生变化
- spring-boot - 在 spring boot 中禁用 open-in-view 会使 CORS 停止工作