Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
For my ReactJS project I have a simple text list (using
<ul>
and
<li>
tags), which has it's own scroll bar to navigate through the list, but not the whole page. I was able to do this with this css code:
#List-container {
max-height: 425px;
overflow: hidden;
overflow-y: scroll;
For my project, I need it to automatically scroll down to the bottom of the list. I tried using window.scrollTo()
but it's not working, which I'm pretty sure is because of the fact that there is a secondary scroll bar.
Does anyone know how I could use window.ScrollTo()
, or know any alternatives?
Thanks in advance!
I tried your code and I'm getting a scrollbar like it's supposed to.
To get something to automatically scroll to bottom I would do something like:
let wHeight = window.innerHeight;
window.scrollY = wHeight;
This is the simplest way I can think of to scroll to the bottom of the page. You can replace window with whatever you want to select in the DOM.
To instead scroll to the bottom of your list simply write:
let divHeight = document.getElementById('List-container');
window.scrollY = divHeight.offsetHeight;
–
–
You need to get reference for the ListContainer
and then set scrollTop
on it like
<ListContainer ref={ref => this.listContainer = ref} />
and use it like
const container = ReactDOM.findDOMNode(this.listContainer);
container.scrollTop = "125px"
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.