Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

css - How to stop scrollbar from auto scrolling when component updates

I am using this library https://www.npmjs.com/package/react-perfect-scrollbar the scroll bar is set up around a comment section where new comments are shown at the top. When a new comment is added to the top of the component it updates and the scroll bar automatically goes to the bottom of the component. Any idea how i can stop it from updating it's position when the component updates?

here's how the section of the function that sets up the container is set-up, children object contains the list which is managed in a different component.

  const Container: FC<HTMLAttributes<HTMLDivElement>> = useCallback(
    props =>
      match ? (
        <PerfectScrollbar className={classes.scrollContainer} {...props} />
      ) : (
        <div className={classes.staticContainer} {...props} />
      ),
    [match]
  )

  return (
    <Grid item {...gridProps} classes={{ item: classNames({ [classes.gridScroll]: match }) }}>
      <Container>{children}</Container>
    </Grid>
  )

below is the css for classes.scrollContainer and classes.gridScroll

    scrollContainer: {
      padding: spacing(1),
      paddingTop: spacing(4),
      height: '100%'
    },
    gridScroll: {
      height: '100%',
      '&:last-child': {
        '& $scrollContainer': {
          paddingBottom: spacing(4)
        }
      }
    },

I am trying to get it to not scroll to the bottom when the component is updated.

Thank You


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.6k users

...