最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How scroll end page with vue js - Stack Overflow

programmeradmin3浏览0评论

How do I scroll to the bottom of the page?

scroll(){
    let container = this.$el.querySelector('#scrollingChat')
    container.scrollTop = container.scrollHeight
}

I'm doing this, and always calling my api answers, but it does not go to the bottom of the page

How do I scroll to the bottom of the page?

scroll(){
    let container = this.$el.querySelector('#scrollingChat')
    container.scrollTop = container.scrollHeight
}

I'm doing this, and always calling my api answers, but it does not go to the bottom of the page

Share Improve this question asked May 7, 2018 at 13:52 Rafael AugustoRafael Augusto 3974 gold badges13 silver badges29 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12
window.scrollTo(0, document.body.scrollHeight || document.documentElement.scrollHeight);

This is will instantly scroll to the bottom of any page.

If you create an anchor in your page like so:

<div id="top"></div>

you can use:

let elmnt = document.getElementById('top');
elmnt.scrollIntoView(false);

This page explains what the different alignment parameters do for scrollIntoView:

true - scrolls to top of element

false - scrolls to bottom of element

default it true

https://www.w3schools.com/jsref/met_element_scrollintoview.asp

window.scrollTo(0,document.body.scrollHeight);

or you can use smooth scroll: https://github.com/alamcordeiro/vue-smooth-scroll

You may take a look at Mozilla Docs as reference.

Code wise safest use with VueJS is, using nextTick() and ref attribution, especially if execution is triggered following an event (Example: Button Click). + works best if you use some VueJS framework.

Scroll can be applied with smooth behavior for a nicer UX.

Example for specific div

<template><div ref="myScrollTarget">...</div></template>
<script>
...
methods: {
  scrollToBottom() {
    const targetRef = this.$refs.myScrollTarget;
    this.$nextTick(() => {
      targetRef.scrollTo(
        {
          top: targetRef.scrollHeight,
          left: 0,
          behavior: "smooth"
        }
      );
    });
  }
}
...
</script>

Example for full page (window)

<template><div>...</div></template>
<script>
...
methods: {
  scrollToBottom() {
    this.$nextTick(() => {
      window.scrollTo(
        {
          top: document.body.scrollHeight,
          left: 0,
          behavior: "smooth"
        }
      );
    });
  }
}
...
</script>
发布评论

评论列表(0)

  1. 暂无评论