简体   繁体   English

使我的JavaScript滚动功能流畅

[英]Making my JavaScript scroll function smooth

Hello my current JavaScript is this, to scroll the page to an id tag further down my website page: 您好,我当前的JavaScript是这样,将页面滚动到一个ID标签,位于我的网站页面下方:

function scroll(element){   
    var ele = document.getElementById(element);  
    window.scrollTo(ele.offsetLeft,ele.offsetTop);
    window.scrollBy(0, -100);
}

How can I make the function scroll smoothly ? 如何使功能平滑滚动?

First of all, this is not Java, this is JavaScript, and those two are different, they are not just two names for one programming language... For your problem, I would do something like getting the current position of the scroll by using document.documentElement.scrollTop (but this one returns 0 on chrome) and the wanted position of the scroll by getting your element's coordinates, and then just move the scroll for a bit of the distance every frame by using window.scrollBy() method. 首先,这不是Java,这是JavaScript,这两个是不同的,它们不仅是一种编程语言的两个名称...对于您的问题,我会做一些事情,例如通过使用获取滚动条的当前位置document.documentElement.scrollTop (但此元素在chrome上返回0)和滚动的所需位置,方法是获取元素的坐标,然后使用window.scrollBy()方法将滚动每帧移动一段距离。 However, I would not suggest this method, because there are other, better solution would be to use this 但是,我不建议使用此方法,因为还有其他更好的解决方案是使用此方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM