Comments (3)
Thanks, that works very well! didn't think about using getBoundingClientRect().top for the clicked on element! Definitely making adding a method to capture the current scroll position would be great, but this solution works for now
from react-viewport-list.
Hi, @MOHIDQ.
You soultion with scrollTop
can work correctly only for fixed item height.
The best solution at this moment is manually compute index
and offset
on item's click (if you open some "item view" from "list" by clicking at item). offset
should be the difference between viewport getBoundingClientRect().top
and clicked item getBoundingClientRect().top
. offset
is just a value which will be added to scrollTop (or scrollLeft) after scroll to element by index.
I think I can add getScrollPosition()
method in the next minor release to get index
and offset
for scroll restoration without pain.
from react-viewport-list.
@MOHIDQ Anyway I added getScrollPosition
method in 7.1.0 release. You can use it for computing index
and offset
for initialIndex
/initialOffset
or scrollToIndex
on component unmount/route change/scroll etc.
something like this:
// save last scroll position somewhere. It can be a session storage or some variable outside your component for example.
lastScrollPosition = viewportListRef.current.getScrollPosition();
// now we can use index and offset in out ViewportList for scroll restoration
<ViewportList>
...
initialIndex={lastScrollPosition.index}
initialOffset={lastScrollPosition.offset}
</VIewportList>
from react-viewport-list.
Related Issues (20)
- scrollToIndex is only scrolling when the index is for an item that is already in the viewport HOT 4
- Wrong peerDependency version HOT 3
- Safari skips elements and scroll with lags HOT 21
- useEffect values are loading up on every re-render. Is there anyway to preserve the new changes? HOT 2
- When using react-viewport-list, children not being rendered in test. HOT 3
- Demo application drag based scrolling issues with Chrome and Edge on Mac HOT 9
- Spacer Element Types HOT 6
- Thank you HOT 3
- New items added to bottom of list. After refresh, appeared first. HOT 6
- Incorrect page scrolling behavior when `initialIndex` or `scrollToIndex` is used HOT 4
- Why does it work when height varies? HOT 2
- List items rendered with undefined HOT 2
- Endless Scrolling Loop HOT 28
- scroll to bottom while having async rendering items HOT 2
- Scroll to bottom on initial render HOT 2
- flew wrap not work HOT 1
- Is infinite scroll pagination supported? HOT 1
- remove spacer in list HOT 3
- Scroll X and Scroll Y and sticky rows and columns HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-viewport-list.