js:scroll平滑滚动页面或元素到顶部或底部的方案汇总

这篇具有很好参考价值的文章主要介绍了js:scroll平滑滚动页面或元素到顶部或底部的方案汇总。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

准备知识:

  • scrollWidth: 是元素全部内容的宽度,包括由于overflow溢出而在屏幕上不可见的内容
  • scrollHeight: 是元素全部内容的高度,包括由于overflow溢出而在屏幕上不可见的内容
  • scrollTop: 纵向滚动条距离元素最顶部的距离
  • scrollLeft: 横向滚动条距离元素最左侧的距离

1、CSS的scroll-behavior

语法

scroll-behavior: auto | smooth | inherit | unset

参数
- smooth:表示滚动很平滑,有过渡效果
- auto:没有过渡效果,一闪而过。

关键代码

html, body {
  scroll-behavior: smooth;
}

示例代码

<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  html {
    scroll-behavior: smooth;
  }

  .btn {
    margin-bottom: 20px;
  }

  .box {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }
</style>

<div class="box" id="box">Box</div>

<a href="#box" class="btn">回到顶部</a>

在线演示:https://mouday.github.io/front-end-demo/scroll/scroll-behavior.html

可以通过js判断是否支持css scroll-behavior属性

function isSuportScrollBehavior() {
    return !(typeof window.getComputedStyle(document.body).scrollBehavior === 'undefined');
}

2、Element.scrollTop

设置属性scrollTop为元素的scrollHeight 即可滚动到元素底部,不过没有动画效果

示例

<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  .btn {
    margin-top: 20px;
  }

  .box {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }
</style>

<button id="btn" class="btn">滚动到底部</button>

<div class="box">Box</div>

<script>
  // 滑动底部
  let btn = document.querySelector("#btn");

  btn.addEventListener("click", function () {
    document.documentElement.scrollTop = document.documentElement.scrollHeight;
  });
</script>

兼容性写法

let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

在线演示:https://mouday.github.io/front-end-demo/scroll/scrollTop.html

3、Element.scroll()/Window.scroll()

scroll() 方法是用于滚动元素 到某个特定坐标

文档: https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scroll

语法

scroll(x-coord, y-coord)
scroll(options)

参数

  • x-coord 你想要显示在左上角的元素沿水平轴的像素。

  • y-coord 你想要显示在左上角的元素沿垂直轴的像素。

  • options

    • top: 指定沿 Y 轴滚动窗口或元素的像素数。
    • left: 指定沿 X 轴滚动窗口或元素的像素数。
    • behavior:
      • smooth 表示平滑滚动并产生过渡效果,
      • auto 或缺省值会直接跳转到目标位置,没有过渡效果。

示例

<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  .btn {
    margin-top: 20px;
  }

  .box {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }
</style>

<button id="to-bottom" class="btn">滚动到底部</button>

<div class="box">Box</div>

<button id="tp-top" class="btn">滚动到顶部</button>

<script>
  // 滑动底部
  let toBottom = document.querySelector("#to-bottom");

  toBottom.addEventListener("click", function () {
    window.scrollTo({
      top: document.documentElement.scrollHeight,
      behavior: "smooth",
    });
  });

  // 滑动顶部
  let toTop = document.querySelector("#tp-top");

  toTop.addEventListener("click", function () {
    window.scrollTo({
      top: 0,
      behavior: "smooth",
    });
  });
</script>

在线演示:https://mouday.github.io/front-end-demo/scroll/scroll.html

4、Element.scrollBy()/Window.scrollBy()

scrollBy() 方法是使得元素滚动一段特定距离的 Element 接口。

使用方法同Element.scroll()/Window.scroll()

注意:scrollBy (回滚滚动条需要写负数,其它与scroll一致)

<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  .btn {
    margin-top: 20px;
  }

  .box {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }
</style>

<button id="to-bottom" class="btn">滚动到底部</button>

<div class="box">Box</div>

<button id="tp-top" class="btn">滚动到顶部</button>

<script>
  // 滑动底部
  let toBottom = document.querySelector("#to-bottom");

  toBottom.addEventListener("click", function () {
    window.scrollBy({
      top: document.documentElement.scrollHeight,
      behavior: "smooth",
    });
  });

  // 滑动顶部
  let toTop = document.querySelector("#tp-top");

  toTop.addEventListener("click", function () {
    window.scrollBy({
      top: -document.documentElement.scrollHeight,
      behavior: "smooth",
    });
  });
</script>

在线演示:https://mouday.github.io/front-end-demo/scroll/scrollBy.html

5、Element.scrollTo()/Window.scrollTo()

Element 的 scrollTo() 方法可以使界面滚动到给定元素的指定坐标位置。

使用方法同Element.scroll()/Window.scroll()

6、Element.scrollIntoView()

Element 接口的 scrollIntoView() 方法会滚动元素的父容器,使被调用 scrollIntoView() 的元素对用户可见。

文档:https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollIntoView

语法

scrollIntoView()
scrollIntoView(alignToTop)
scrollIntoView(scrollIntoViewOptions)

参数

  • alignToTop可选(布尔值)

    • true,元素的顶端将和其所在滚动区的可视区域的顶端对齐。相应的 scrollIntoViewOptions: {block: “start”, inline: “nearest”}。这是这个参数的默认值。
    • false,元素的底端将和其所在滚动区的可视区域的底端对齐。相应的 scrollIntoViewOptions: {block: “end”, inline: “nearest”}。
  • scrollIntoViewOptions 可选 实验性

    • behavior 可选 定义动画过渡效果,auto 或 smooth 之一。默认为 auto。
    • block 可选 定义垂直方向的对齐,start(上)、center(中)、end(下) 或 nearest(距离最近的点) 之一。默认为 start。
    • inline 可选 定义水平方向的对齐,start、center、end 或 nearest 之一。默认为 nearest。
<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  .btn {
    margin-top: 20px;
  }

  pre,
  code {
    text-align: unset;
  }

  .top {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }

  .box {
    margin-top: 20px;
    height: 200px;
    line-height: 200px;
    background-color: #eaffd0;
  }
</style>

<button id="btn" class="btn">滚动到Box</button>

<div class="top">TOP</div>

<div class="box">Box</div>

<script>
  let btn = document.querySelector("#btn");

  btn.addEventListener("click", function () {
    let top = document.querySelector(".box");
    top.scrollIntoView({
      block: 'start',
      behavior: "smooth",
    });
  });
</script>

在线演示:https://mouday.github.io/front-end-demo/scroll/scrollIntoView.html

7、自定义兼容性方案

可能在部分老旧设备上存在兼容性,从网上收集了一个多设备支持的缓冲方案

<style>
  * {
    margin: 0;
    padding: 0;
    text-align: center;
  }

  .btn {
    margin-top: 20px;
  }

  .box {
    margin-top: 20px;
    height: 1500px;
    line-height: 1500px;
    background-color: #95e1d3;
  }
</style>

<button id="to-bottom" class="btn">滚动到底部</button>

<div class="box">Box</div>

<button id="tp-top" class="btn">滚动到顶部</button>

<script>
  // 封装一个回到底部或者顶部的函数
  function scrollToTop(position) {
    // 使用requestAnimationFrame,如果没有则使用setTimeOut
    if (!window.requestAnimationFrame) {
      window.requestAnimationFrame = function (callback) {
        return setTimeout(callback, 20);
      };
    }

    // 获取当前元素滚动的距离
    let scrollTopDistance =
      document.documentElement.scrollTop || document.body.scrollTop;

    function smoothScroll() {
      console.log('smoothScroll');
      // 如果你要滚到顶部,那么position传过来的就是0,下面这个distance肯定就是负值。
      let distance = position - scrollTopDistance;
      // 每次滚动的距离要不一样,制造一个缓冲效果
      scrollTopDistance = scrollTopDistance + distance / 5;
      // 判断条件
      if (Math.abs(distance) < 1) {
        window.scrollTo(0, position);
      } else {
        window.scrollTo(0, scrollTopDistance);
        requestAnimationFrame(smoothScroll);
      }
    }

    smoothScroll();
  }

  // 滑动顶部
  let toTop = document.querySelector("#tp-top");

  toTop.addEventListener("click", function () {
    // 回到顶部
    scrollToTop(0);
  });

  // 滑动底部
  let toBottom = document.querySelector("#to-bottom");

  toBottom.addEventListener("click", function () {
    // 滚到底部
    scrollToTop(document.documentElement.scrollHeight - window.innerHeight);
  });
</script>

在线演示:https://mouday.github.io/front-end-demo/scroll/smoothScroll.html文章来源地址https://www.toymoban.com/news/detail-705857.html

8、参考文章

  1. JS滑动到页面顶部(或底部)
  2. 2020-06-23原生js使dom自动滚动到最底部
  3. s 中关于操纵 Element 进行滚动的方法,都在这了‍‍‍
  4. 平滑滚动到顶部或底部的几种方案
  5. 完美实现一个“回到顶部”
  6. [前端]通过图片懒加载引出来的知识点

到了这里,关于js:scroll平滑滚动页面或元素到顶部或底部的方案汇总的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • js使页面滚动到特定元素上

    1. 使用Element.scrollIntoView()方法

    2024年02月07日
    浏览(30)
  • uniapp中页面和scrollview两种滚动到顶部原来这么优雅

    页面 scrollview

    2024年02月16日
    浏览(29)
  • 【uniapp】scroll-view 实现自动滚动到最底部

    在做uniapp项目中,有个滚动视图组件 scroll-view ,跟微信小程序里的组件一样的,想要实现自动滚动到最底部,是一件容易忽略的,小事情。 官网uniapp文档上说可以控制滚动条,并没有自动滚动到底部的设置选项,请看布局源代码,如下,大多数可能都是这样写的 🤔 虽然可

    2024年02月11日
    浏览(48)
  • 实现对一个元素的滚动条进行平滑滚动至顶部的动画效果

    1.elementUI中的平滑滚动至顶部的动画效果代码 2.将上面的代码简化 3.继续简化代码 使用 Element.scrollTo 方法并使用 scroll-behavior: smooth 的简化代码示例: 首先,在你的CSS样式表中加入以下代码: 这会将平滑滚动的效果应用到整个页面。 然后,使用 scrollTo 方法在JavaScript中触发滚

    2024年02月11日
    浏览(31)
  • #vue3# el-table-horizontal-scroll 让 el-table 在底部显示横向滚动条

    一、需求: 当 el-table 的宽度超出浏览器宽度时,尽管 el_table 底部会出现滚动条,但使用起来不太便捷,因为每次需要先滚动到底部才能使用 el-table 的滚动体,这显得相当繁琐。 为了提升体验,希望在 el-table 的 宽度超出屏幕宽度时,即使没有滚动到底部,也能够在可视范围

    2024年01月18日
    浏览(29)
  • uniapp scroll-view横向滚动无效,scroll-view子元素flex布局不生效

    要素排查: 1.scroll-x属性需要开启,官方类型是Boolean,实际字符串也行。   2scroll-view标签需要给予一个固定宽度,可以是百分百也可以是固定宽度或者100vw。    3.子元素需要设置display: inline-block(行内块元素)属性,scroll-view需要设置white-space: nowrap(不换行) 当使用scroll

    2024年02月12日
    浏览(33)
  • css样式-内容固定在页面底部,不随滚动条滚动

    目录 1、内容固定在页面底部,不随着滚动条滚动 2、添加内容,简单测试是否固定  position:                 1、【relative】相对定位;2、【absolute】绝对定位;                  3、【fixed】固定定位;4、【static】默认值;5、【sticky】粘性定位。 这里设置为fixed,

    2024年02月11日
    浏览(38)
  • 【JS】设置滚动属性默认自动滚动到底部(overflow:scroll;)

    设置滚动属性默认自动滚动到底部: 场景:实现对话框,默认展示最新的对话内容 react 实现: 所需用到的css样式

    2024年02月07日
    浏览(33)
  • 如何使用CSS实现一个平滑滚动到页面顶部的效果(回到顶部按钮)?

    前端入门之旅:探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚踏入前端领域的朋友们量身打造的。无论你是完全的新手还是有一些基础的开发者,这里都将为你提供一

    2024年02月11日
    浏览(35)
  • js 滚动条自动滚动到最底部

    简介:         功能简介,js 滚动条自动滚动到最底部 效果展示: 核心代码: 代码示例: 代码示例2:

    2024年02月11日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包