let jump = document.querySelector("#map");//定义起跳点div
      let total = jump.offsetTop;
      let distance =
        document.documentElement.scrollTop || document.body.scrollTop;
      // 平滑滚动,时长500ms,每10ms一跳,共50跳
      let step = total / 80;
      if (total > distance) {
        smoothDown();
      } else {
        let newTotal = distance - total;
        step = newTotal / 80;
        smoothUp();
      }
      function smoothDown() {
        if (distance < total) {
          distance += step;
          document.body.scrollTop = distance;
          document.documentElement.scrollTop = distance;
          setTimeout(smoothDown, 10);
        } else {
          document.body.scrollTop = total;
          document.documentElement.scrollTop = total;
        }
      }
      function smoothUp() {
        if (distance > total) {
          distance -= step;
          document.body.scrollTop = distance;
          document.documentElement.scrollTop = distance;
          setTimeout(smoothUp, 10);
        } else {
          document.body.scrollTop = total;
          document.documentElement.scrollTop = total;
        }
      }
    },

步骤如下:

# 给元素绑定点击事件

## 给跳转至哪个元素设置id名

### 在methods:中定义点击事件 function(){ document.querySelector("#header").scrollIntoView(true);}

Logo

前往低代码交流专区

更多推荐