Home > Web Front-end > JS Tutorial > Example analysis of implementing page anchor positioning animated scrolling based on JavaScript

Example analysis of implementing page anchor positioning animated scrolling based on JavaScript

黄舟
Release: 2017-08-10 10:57:33
Original
3236 people have browsed it

Personally, I didn’t want to use jquery to achieve the desired effect on the project. I thought about using js to try it myself. It took me a while and finally achieved it..

Here’s some useful information..

function scrollTo(y, duration) {
   /*y:目标纵坐标,duration:时间(毫秒)*/
   var scrollTop = document.body.scrollTop;/*页面当前滚动距离*/
    var distance = y - scrollTop;/*结果大于0,说明目标在下方,小于0,说明目标在上方*/
    var scrollCount = duration / 10;/*10毫秒滚动一次,计算滚动次数*/
    var everyDistance = distance / scrollCount/*滚动距离除以滚动次数计算每次滚动距离*/
    for (var index = 1; index <= scrollCount; index++) /*循环设置scrollBy事件,在duration之内,完成滚动效果*/
      setTimeout(function () { window.scrollBy(0, everyDistance) }, 10 * index);
 }
Copy after login

The above is the detailed content of Example analysis of implementing page anchor positioning animated scrolling based on JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template